Bootstrap split button dropdown not styled properly using default styles? - html

Is there a bug in the bootstrap CSS? I am trying to use the default bootstrap dropdown menu with the split button, but the down-arrow button to the side is much shorter than the actual menu.
I thought this was some overlapping style issue within my code but I have managed to reproduce this using only the boostrap code in JSFiddle.
<div class="btn-group">
<button type="button" class="btn btn-danger">Action</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<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 class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</div>

The default structure of a dropdown uses an unordered list (UL) and you're also missing the caret class.
See the docs
<ul class="dropdown-menu">
<li>Action</li>
</ul>
Working Example:
body {
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<!-- Split button -->
<div class="btn-group">
<button type="button" class="btn btn-danger">Action</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li>Action
</li>
<li>Another action
</li>
<li>Something else here
</li>
<li role="separator" class="divider"></li>
<li>Separated link
</li>
</ul>
</div>

this happened because of your span tag has class sr-only and this class has fixed width and height 1px in booststrap.css
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}

Related

bootstrap 4 dropdown menu navbar user preferences "Welcome, User"

I'm newbie to css and bootstrap and can not figure out how to solve this. Any help are welcome.
I have two problems with my code.
1) When I click on the 'Welcome, User' menu in small screens (mobile), the dropdown menu opens inside the navigation bar, distorting the bar and changing its height. I want it to behave in the same way as on larger screens (desktops, for example). When I click, I want it to open the menu items without changing the navigation bar, in front of the navigation bar but not inside.
2) Another problem: even on larger screens, I can not see the menu items completely. Some of the text of the items is hidden at the edge of the screen. I would like the menu to appear completely on the screen, no matter if it is desktop or mobile.
To make it clear, I record a very small video demonstrating the problem:
https://puu.sh/Bq34w/ac56908be0.mp4
The complete code:
https://jsfiddle.net/fredslz/0v7qwjdm/12/
<!doctype html>
<html lang="en">
<body class="text-center">
<nav class="navbar navbar-expand-md navbar-light fixed-top" style="background-color: #FFFFFF;">
<button class="navbar-toggler navbar-nav mr-auto" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mx-auto" href="#">My Brand</a>
<ul class="nav navbar-nav ml-md--auto">
<li class="dropdown">
<a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" data-toggle="dropdown" aria-expanded="false">
Welcome, User <b class="caret"></b>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
</body>
</html>
And the CSS:
.navbar {
-webkit-box-shadow: 0 8px 6px -6px #999;
-moz-box-shadow: 0 8px 6px -6px #999;
box-shadow: 0 8px 6px -6px #999;
}
You need to use dropdown-menu-right on the dropdown if you want the menu items properly aligned.
What's not clear is why you're using the toggler, because you don't have any collapsible menu items (navbar-collapse). Just remove the toggler and use navbar-expand so that the Navbar doesn't collapse in the mobile view.
<nav class="navbar navbar-expand navbar-light fixed-top" style="background-color: #FFFFFF;">
<a class="navbar-brand mx-auto" href="#">My Brand</a>
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" data-toggle="dropdown" aria-expanded="false">
Welcome, User <b class="caret"></b>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
https://www.codeply.com/go/j1HZLEdyrC
The dropdown will always open inside the mobile collapsed Navbar which is by-design. If you do want to keep the mobile toggler, and intend to add collapsible items, you can use position-absolute on the dropdown-menu to override the default behavior.
<nav class="navbar navbar-expand-md navbar-light" style="background-color: #FFFFFF;">
<button class="navbar-toggler navbar-nav mr-auto" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mx-auto" href="#">My Brand</a>
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" data-toggle="dropdown" aria-expanded="false">
Welcome, User <b class="caret"></b>
</a>
<div class="dropdown-menu dropdown-menu-right position-absolute" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
Demo of both options: https://www.codeply.com/go/j1HZLEdyrC
Related: Bootstrap 4 Navbar Dropdown Menu Items Right
For the issue with the mobile dropdown, you can use this to position it absolutely in the mobile view too:
.navbar-nav .dropdown-menu {
position: absolute;
}
For the alignment of the dropdown, just add the class dropdown-menu-right to your dropdown-menu.
See demo below (I have given !important to cope with the cascading order in the snippet) and working fiddle here:
.navbar {
-webkit-box-shadow: 0 8px 6px -6px #999;
-moz-box-shadow: 0 8px 6px -6px #999;
box-shadow: 0 8px 6px -6px #999;
/* the rest of your styling */
}
.navbar-nav .dropdown-menu {
position: absolute !important;
}
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-md navbar-light fixed-top" style="background-color: #FFFFFF;">
<button class="navbar-toggler navbar-nav mr-auto" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mx-auto" href="#">My Brand</a>
<ul class="nav navbar-nav ml-md--auto">
<li class="dropdown">
<a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" data-toggle="dropdown" aria-expanded="false">
Welcome, User <b class="caret"></b>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<!-- ADDED CLASS -->
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>

How to move the sub-menu drop-down items towards right on hover in Bootstrap 4.1?

I am working on a website in which the submenu dropdown items appears at the bottom as shown below.
The HTML/CSS code which I have used for that are as follows:
HTML Code:
<div class="navbar-collapse text-center" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<!--
<button type="submit" onclick="location.href='/prostore';" class="btn btn-default">Hello World</button>
-->
<li class="menu-item nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
main menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">about</a>
<a class="dropdown-item" href="#">blog</a>
<a class="dropdown-item" href="#">contact us</a>
<div class="submenu-item">
<a class="dropdown-item dropdown-toggle dropright" href="#" id="navbarDropdownSubMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Social
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownSubMenuLink">
<a class="dropdown-item" href="#">T</a>
<a class="dropdown-item" href="#">U</a>
<a class="dropdown-item" href="#">V</a>
<a class="dropdown-item" href="#">W</a>
</div>
</div>
</div>
CSS code:
.menu-item:hover > .dropdown-menu {
display: block;
}
/* makes sub-menu S open on hover */
.submenu-item:hover > .dropdown-menu {
display: block;
}
Problem Statement:
I am wondering what changes I should make in the HTML/CSS above so that if we hover on Social, the drop-down items T, U, V and W are displayed towards the right similar to this
Prior to bootstrap 4 you would use pull-right, but since 4 they changed the pull-right to float-(sm,md,lg,xl)-(left,right,none). So I would try float-sm-right. https://getbootstrap.com/docs/4.0/migration/#utilities
I copied the code from bootstrap website.
<div class="bd-example">
<div class="btn-group dropright">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropright
</button>
<div class="dropdown-menu" x-placement="right-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(108px, 0px, 0px);">
<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 class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</div>
<div class="btn-group dropright show">
<button type="button" class="btn btn-secondary">
Split dropright
</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="sr-only">Toggle Dropright</span>
</button>
<div class="dropdown-menu show" x-placement="right-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(153px, 0px, 0px);">
<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 class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</div>
</div>
I see this is not in your code
x-placement="right-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(108px, 0px, 0px);"
and
<div class="btn-group dropright show">
try adding these to your code.

Dropdown submenu on hover in Bootstrap 4.1

I am working on a website in which I want to create a dropdown submenu on hover in Bootstrap 4.1
The HTML code which I have used in order to create a dropdown menu on hover are:
<div class="navbar-collapse text-center" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
main menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">P</a>
<a class="dropdown-item" href="#">Q</a>
<a class="dropdown-item" href="#">R</a>
<a class="dropdown-item" href="#">S</a>
</div>
</li>
<button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>
</ul>
</div>
The above HTML code is the working code which displays P, Q, R, and S on hover.
Problem Statement:
I am wondering what changes I should make in the code above now so that on hover of S; dropdown items T, U, V, and W are shown.
You could just apply the inception concept and put back another dropdown-toggle element inside the menu.
To open the sub-menu on hover you have to wrap the content inside a container to apply the :hover css styling display:block on the .dropdown-menu child element that contains the submenu links.
/* makes main-menu open on hover */
.menu-item:hover > .dropdown-menu {
display: block;
}
/* makes sub-menu S open on hover */
.submenu-item:hover > .dropdown-menu {
display: block;
}
<link href="https://getbootstrap.com/docs/4.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<div class="navbar-collapse text-center" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="menu-item nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
main menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">P</a>
<a class="dropdown-item" href="#">Q</a>
<a class="dropdown-item" href="#">R</a>
<div class="submenu-item">
<a class="dropdown-item dropdown-toggle dropright" href="#" id="navbarDropdownSubMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
S
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownSubMenuLink">
<a class="dropdown-item" href="#">T</a>
<a class="dropdown-item" href="#">U</a>
<a class="dropdown-item" href="#">V</a>
<a class="dropdown-item" href="#">W</a>
</div>
</div>
</div>
</li>
<button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>
</ul>
</div>
Well, I think you've already figured out that isn't possible with the default bootstrap 4.1 classes.
You can achieve the desired effect using some jQuery and CSS.
First, create a div with a custom class and a list with the items you need, TUVW in your case.
After in CSS, you can hide your list container and handle its position.
In jQuery you can track mouse hover event('mouseenter mouseleave') puting your logic to show/hide the list container.

Make Bootstrap dropdown button center align

How colud i make bootstrap dropdown button on center.
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
keep searching our site listening audio episodes
<span class="fa fa-play pull-right"></span>
</button>
<ul class="dropdown-menu col-md-6" aria-labelledby="dropdownMenu1">
<li>Another action</li>
<li>Something else here</li>
<li>Separated link</li>
</ul>
</div>
Thanks in advance.
Use this code in your code
.dropdown-toggle{position: relative;}
.fa-play{position: absolute;
margin: 0px auto;
left: 0;
right: 0;}
You can go through below code.
I have added style="line-height: 20px;vertical-align:middle;" to span
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
keep searching our site listening audio episodes
<span class="fa fa-play pull-right" style="line-height: 20px;vertical-align:middle;"></span>
</button>
<ul class="dropdown-menu col-md-6" aria-labelledby="dropdownMenu1">
<li>Another action</li>
<li>Something else here</li>
<li>Separated link</li>
</ul>
</div>

Bootstrap dropdown menu does not position correctly

When I place a bootstrap dropdown inside a div that centers the dropdown using CSS "text-align: center", the dropdown menu appears in the original un-centered position of the dropdown. The dropdown doesn't seem to know that its triggering button has been moved.
The issue is represented in this jsfiddle:
https://jsfiddle.net/dkent600/wyos4ukt
The fiddle contains the following code:
<div style="background-color:grey; text-align:center">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
</ul>
</div>
</div>
Thanks for any clues for how to fix this.
If you add the class btn-group to the .dropdown element, then the dropdown-menu will be positioned properly.
Updated Exmple
<div class="btn-group dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
The reason this works is because the class adds the following CSS. In doing so, the dropdown-menu is positioned absolutely relative to the parent element.
.btn-group > .btn,
.btn-group-vertical > .btn {
position: relative;
float: left;
}
Alternatively, you could also add the following:
Updated Example
.dropdown {
position: relative;
display: inline-block;
vertical-align: middle;
}