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>
WitHeld
  • 305
  • 3
  • 8
  • Actually found the same question here: https://stackoverflow.com/questions/17882384/bootstrap-dropdown-how-to-make-text-wrap-nicely – WitHeld Sep 24 '18 at 12:59

3 Answers3

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

ShaSha-Codes
  • 71
  • 1
  • 4
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>
WitHeld
  • 305
  • 3
  • 8
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

Mannaroth
  • 470
  • 5
  • 13
  • 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. – WitHeld Sep 24 '18 at 12:17
  • 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 – Mannaroth Sep 24 '18 at 12:18
  • Yes your answer is correct from that perspective - I apologize for the ambiguous question . – WitHeld Sep 24 '18 at 12:53