1

With bootstrap 4 and popper.js, whenever I add a dropdown to navbar-right and click on the button the dropdown then adds a horizontal scroll to the page and you will have to scroll right to see the whole dropdown.

How can I fix this to not have the horizontal scroll whenever the dropdown is displayed?

Here is an example of the code that I am using:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
        <title id="title">Title</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </head> 
    <body>
        <nav class="navbar navbar-expand-sm navbar-light">
            <a class="navbar-brand" href="/home/">
                Brand
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarex1" aria-controls="navbarex1" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav mr-auto navbarex1">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 1</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 2</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 3</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 4</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <div class="dropdown show">
                        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Dropdown link
                        </a>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </div>
                </ul>
            </div>
        </nav>            
    </body>
</html>

1

3 Answers 3

5

Change this:

<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">

to:

<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuLink">

In short just add dropdown-menu-end class

Sign up to request clarification or add additional context in comments.

Comments

1

I have overwritten the bootstrap defaults to set the min-width of the dropdown-menu to 0 and set white-space to normal for the dropdown-item. This makes the text wrap nicely and does not overflow the body.

.dropdown-menu{min-width:0;}
.dropdown-menu .dropdown-item{white-space:normal;}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
        <title id="title">Title</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </head> 
    <body>
        <nav class="navbar navbar-expand-sm navbar-light">
            <a class="navbar-brand" href="/home/">
                Brand
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarex1" aria-controls="navbarex1" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav mr-auto navbarex1">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 1</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 2</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 3</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 4</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <div class="dropdown show">
                        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Dropdown link
                        </a>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </div>
                </ul>
            </div>
        </nav>            
    </body>
</html>

Comments

0

Add the overflow: scroll-y property to your body

body {
    overflow: scroll-y;
    overflow-x: hidden;
}

this way no element will overflow body's width unless specified, but still allow vertical scrolling

3 Comments

By doing this it will cut the content of the dropdown thereby limiting the user experience. I have also seemed to have fixed it in a way that works for me - I will add an answer shortly that will show how I found a work around.
Yes that is not an optimal solution in this case but I assumed you knew you could just move the object to the left to fix it and as such wanted a specific solution that allowed no horizontal scrolling that is, after all, what you asked
Yes your answer is correct from that perspective - I apologize for the ambiguous question .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.