Bootstrap dropdown hover issue - html

I have a bootstrap default dropdown on my website. The issue I am having is that I want the dropdown to show up on hover. It is working as intended but has a small issue. It goes away unless I go on it through the dropdown arrow if I go on it from anywhere beside the arrow but directly below the dropdown item.. it goes away so it is very inconsistent. How can I make it better? I have tried adding padding to the dropdown item but it didn't help at all.
HTML:
<nav class="navbar navbar-inverse" role="navigation">
<div class="collapse navbar-collapse text-right">
<ul class="nav navbar-nav pull-left">
<li>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Testing</a>
<ul class="dropdown-menu dropdown-menu-arrow">
<li>Testing</li>
<li>Testing</li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
CSS:
a {
color: #fff
}
a:focus,
a:hover {
color: #fff!important;
border-bottom: 1px solid #fff;
text-decoration: none
}
#media (min-width:768px) {
.dropdown:hover .dropdown-menu {
display: block;
}
.dropdown-menu-arrow:before {
border-bottom: 7px solid rgba(0, 0, 0, 0.2);
border-left: 7px solid transparent;
border-right: 7px solid transparent;
content: "";
display: inline-block;
left: 9px;
position: absolute;
top: -7px;
}
.dropdown-menu-arrow::after {
border-bottom: 6px solid #FFFFFF;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
content: "";
display: inline-block;
left: 10px;
position: absolute;
top: -6px;
}
}
JSFiddle Demo

Adding padding to the .dropdown-toggle item fixes the issue as the padding is part of the hoverable area of the item.
This is the code I added:
.dropdown-toggle {
padding: 10px;
}
Link to updated JSFiddle Demo
You said you added padding to the "dropdown item" which I am guessing means you tried to add it to the actual dropdown rather than the toggle for the dropdown. This wouldn't work as the dropdown toggle is the trigger that has the :hover pseudo class, therefore you must extend this item's hoverable area (with padding) to cover the gap between the trigger and the dropdown itself.

JJ's answer didn't work for me. The issue I had was a very small gap between the dropdown-toggle, and the dropdown-menu. This caused the dropdown to disappear if I moved the cursor down slowly from the menu item to the dropdown.
I added this to my css:
.dropdown-menu {
margin: 0px;
}

Related

include a pseudoelement inside a anchor hover state

I have pseudoelements next to my links in a navigation menu. There are small downward arrows indicating a dropdown and On hover, the background changes. However, the only area that is covered is the active link and not the downward pointing arrow.
A sample of that is below:
.item > a {
color: #000;
padding-top: 1.5rem;
padding-bottom: 1.1rem;
}
.item > a:hover {
background-color: blue;
color: #fff;
}
.arrow-nav-item:after {
content: '';
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid #5a5a5a;
clear: both;
position: absolute;
top: 25px;
right: 625px;
}
<ul id="items">
<li class="item"><a class="arrow-nav-item" href="#">Main Item</a>
<ul class="subitem">
<li>Chapter 1</li>
</ul>
</li>
</ul>
Basically I have an ::after on my anchor tag which is positioned absolutely and is styled to look like a down arrow. On hover, a background appears, and I want the arrow included inside the colored hover area.
The reason it's not included I think is because of the absolute positioning - because when the arrow is relative, I can include it in the hover area. I don't think I can do that because giving the :after a relative positioning loses control of placement.
A couple of things I tried: add more right padding to the anchor, setting a fixed width on anchor and changing placement of pseudo-element (moving it to <li> tag) etc.
Should this be refactored to change? Is absolute positioning not the best way to handle these pseudoelements?
Using right in that way will cause problems as the screen resizes. Instead, you can remove absolute positioning and position the anchor with margin instead...
.item>a {
color: #000;
padding-top: 1.5rem;
padding-bottom: 1.1rem;
}
.item>a:hover {
background-color: blue;
color: #fff;
}
.arrow-nav-item:after {
content: '';
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid #5a5a5a;
margin-left: 1em;
display: inline-block;
vertical-align: middle;
margin-right: 1em;
}
.arrow-nav-item:hover:after {
border-top-color: #FFF;
}
<ul id="items">
<li class="item"><a class="arrow-nav-item" href="#">Main Item</a>
<ul class="subitem">
<li>Chapter 1</li>
</ul>
</li>
</ul>
While absolute-positioning would be a good way to handle this, you certainly don't want to be using giant offsets relative to the right. What I would recommend is to make use of ::before, and simply set a small negative margin-left on the dropdown:
.item>a {
color: #000;
padding-top: 1.5rem;
padding-bottom: 1.1rem;
}
.item>a:hover {
background-color: blue;
color: #fff;
}
.arrow-nav-item:before {
content: '';
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid #5a5a5a;
position: absolute;
margin-left: -35px;
margin-top: 5px;
}
<ul id="items">
<li class="item"><a class="arrow-nav-item" href="#">Main Item</a>
<ul class="subitem">
<li>Chapter 1</li>
</ul>
</li>
</ul>
Note that this makes the dropdown relative to the element's left-hand side, so it will always appear in the same place, regardless of the content of the <li>. However, it still has the dropdown arrow outside of the`. The problem is that in order to have the background cover both components, you need to move the arrow inside the bullet points.
This can then be offset with padding-left on the <a> tag itself, so that the dropdown remains within the blue background:
.item>a {
color: #000;
padding-top: 1.5rem;
padding-bottom: 1.1rem;
}
.item>a:hover {
background-color: blue;
color: #fff;
}
.arrow-nav-item:before {
content: '';
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid #5a5a5a;
position: absolute;
margin-left: -15px;
margin-top: 5px;
}
.arrow-nav-item {
padding-left: 20px; /* Larger than margin-left */
}
<ul id="items">
<li class="item"><a class="arrow-nav-item" href="#">Main Item</a>
<ul class="subitem">
<li>Chapter 1</li>
</ul>
</li>
</ul>
I don't think it's possible to have the dropdown arrow outside the bullet point and still retain the background, but hopefully this will suffice :)

Drop down menu - Successful in trying the code but does not appear on Weebly live site

I am working on a navigation bar. I have a drop down effect working successfully in my test environment. It doesn't work on my target website (the menu "Archive" should show drop down effect).
Here's a URL to where it does not work properly.
http://quantsrecord.weebly.com/back-stage.html
Here is the markup that I am using, which does work in test, but not at the URL above.
<style type="text/css">
body {padding: 0; margin: 0;}
#wrap {
width: 100%;
height: 50px;
margin: 0;
z-index: 99;
position: relative;
background-color: #366b82;
}
.navbar {
height: 50px;
padding: 0;
margin: 0;
position: absolute;
border-right: 1px solid #54879d;
}
.navbar li {
height: auto;
width: 147px;
float: left;
text-align: center;
list-style: none;
font: normal bold 12px/1.2em Arial, Verdana, Helvetica;
padding: 0;
margin: 0;
background-color: #366b82;
}
.navbar a {
padding: 18px 0;
border-left: 1px solid #54879d;
border-right: 1px solid #1f5065;
text-decoration: none;
color: white;
display: block;
}
.navbar li:hover, a:hover {background-color: #54879d;}
.navbar li ul {
display: none;
height: auto;
margin: 0;
padding: 0;
}
.navbar li:hover ul {
display: block;
}
.navbar li ul li {background-color: #54879d;}
.navbar li ul li a {
border-left: 1px solid #1f5065;
border-right: 1px solid #1f5065;
border-top: 1px solid #74a3b7;
border-bottom: 1px solid #1f5065;
}
.navbar li ul li a:hover {background-color: #366b82;}
</style>
</head>
<body>
<div id="wrap">
<ul class="navbar">
<li>Home</li>
<li>Live
<li>News</li>
<li>Calculators</li>
<li>Knowledge</li>
<li>Archive
<ul>
<li>Jan 2017</li>
<li>Feb 2017</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
</body>
You have an overflow-y set, you need to remove it. Change this:
<div id="611390739829550513" style="width: 100%; overflow-y: hidden;" class="wcustomhtml" align="center">
To this:
<div id="611390739829550513" style="width: 100%;" class="wcustomhtml" align="center">
You can do like this:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">click here</a>
<ul class="dropdown-menu">
<li>sub Item 1</li>
<li><a tabindex="-1" href="#">sub Item 2</a></li>
</ul>
</li>
Teaching you how to troubleshoot
You can test / prove these things with your browser's developer tools. They are SUPER powerful and permit you to not only see the markup and CSS affecting any element, they also allow you to "play" with the styles so that you can see if different changes fix the problem. Here's one of many articles that might help you learn to troubleshoot: http://bigemployee.com/4-simple-techniques-to-quickly-debug-and-fix-your-css-code-in-almost-any-browser/
Explaining the problem
The problem is due to some markup that (presumably) Weebly adds to your site. The nav markup is wrapped in this element:
<div id="611390739829550513" style="width: 100%; overflow-y: hidden;" class="wcustomhtml" align="center"><!-- your nav here --></div>
Due to this element, which has the inline-style of overflow-y: hidden;, your drop-down does not show up (even though your styles make it display: block, it cannot show because it is below / outside the wrapper).
If you can find a way to modify that markup, or add a style to address that element and override the overflow-y: hidden, you will be able to make the element appear.
ONE THING that you could do potentially is get this in some styles:
div#611390739829550513 {
overflow-y: visible !important;
}
However, it would be preferable to remove the style from the element if possible.
Lastly, if you do this, you'll need to fix your navigation, because at narrower widths it "wraps" and the "Contact" menu item appears below the rest of the nav instead of inline.

How to get chevron to align to center of link tab

I'm using Bootstrap and have a navbar with several links. The first few links have dropdown menus. On top of each dropdown menu I added a triangle turned upward pointing to the link you just clicked. The problem is, the links are of different lengths (BRANDS vs MEN'S WATCHES) and I want the triangle centered with the text. Since the triangle is done with CSS via the :before attribute on the dropdown-menu, I don't know how to center it according to the li with the dropdown class.
Bootply of my navbar: http://www.bootply.com/ebKpdebUKh
CSS for the triangle:
#main_navbar2 .dropdown-menu:before { position: absolute;
top: -10px;
left: 55px;
display: inline-block;
border-right: 9px solid transparent;
border-bottom: 9px solid #000;
border-left: 9px solid transparent;
content: '';}
Centering of "chevron" has been done by using JQuery.
Check complete example at CODEPEN
HTML:
<li class="dropdown">
BRANDS
<ul class="dropdown-menu multi-column columns-6" role="menu">
---- Multi column section ----
</ul>
</li>
JS:
$('.dropdown-menu , .dropdown > a').hover(function() {
$(this).parent().find('a:first-child').addClass('menu-pointer')
}, function() {
$(this).parent().find('a:first-child').removeClass('menu-pointer');
});
CSS:
#main_navbar2 .dropdown > a.menu-pointer:after {
top: 34px;
margin: auto;
position: absolute;
display: block;
border-right: 9px solid transparent;
border-bottom: 9px solid #000;
border-left: 9px solid transparent;
content: '';
left: 0;
right: 0;
z-index: 999;
width: 9px;
}
I hope this helps you
Enjoy :)
I know that this has an accepted answer - but you do not need jquery for it - just CSS and you can get a triangle under he center of the link text. I created a dodgy little navmenu - and have an active link which shows a little triangle under the active (selected) menu link - and then on the hover over each of the links - the triangle will show under the link text - and centered to the li. All without jQuery - and a little css magic. I also put in a longer than usual link text to show that the css tringle is autiomatically centered on the li.
.navLinks li{display:inline-block;list-style:none; text-decoration: none;padding:0 10px}
.navLinks li a, .navLinks li a:hover{text-decoration: none}
.navLinks li:hover:after,
.activeLink:after {
content: '';
position: relative;
border-style: solid;
border-width:0 9px 9px;
border-color: #006400 transparent;
display: block;
width: 0;
z-index: 1;
margin-left: -8px;
top:5px;
left: 50%;
background: none;
}
<ul class="navLinks">
<li class="activeLink">Home</li>
<li>About</li>
<li>About</li>
<li>Store</li>
<li>Contact</li>
<li>An Example of a long link text.</li>
</ul>

Navbar: background on active Site

i want a navbar that mark for example on page index.html the navbar link with the text Home with a backgroud identical with the hover effect.
As follow my code..
CSS:
.header {
border-bottom: 6px solid black;
padding: 20px 10px 20px 10px;
background: #D11B1B;
position:fixed;
top:0;
left:0;
z-index:2000;
width: 100%;
height: 60px;
}
.nav ul a {
background-color: none!important;
padding: 20px 2px 20px 0px;
font-size: 40px;
}
.nav ul a:hover {
background-color: black!important;
border: 1px solid white;
padding: 40px 0px 0px 0px;
width: 100px;
}
HTML:
<header class="header">
<div class="nav">
<ul>
Home
Werbung
</ul>
</div>
</header>
Thx.
If you want to make a specific button/link in the navbar on each page, then you can specify give the button that links to the current page an "active" class.
<body>
<div class="nav">
Home
Home
Home
</div>
</body>
And then in your css, specify specific styles for ".nav a.hover" to make the active page look different on the navbar. You can add whatever styles you want, for example adding a bottom border, changing the color, or whatever you want to do.
.nav a.active{
color:red;
}
If you want to make the buttons change color when you hover the mouse over them, then use ".nav a:hover" and add whatever styles you want.
.nav a:hover {
color:dark-blue;
}

Not able to position <button> inside a <li>

So I basically want to do a list, each li containing an anchor and a dropdown menu (using bootstrap). The li are contained into a div, itself contained into a ul. I successfully aligned the anchors on the left, and I would like to do the same thing for the dropdowns on the right, but it's not working. Here's the result, each element with a border to visualize its behaviour.
Here's the html...
<ul>
<li class="list_items">'.$location.'
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
More info <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
<!-- items of the dropdown menu -->
</ul>
</div>
</li>
<!-- repeat... -->
</ul>
... and the css
ul
{
display: inline-block;
padding: 0;
margin-left: 20px;
margin-right:0;
vertical-align: top;
width:240px;
font-family: Verdana;
text-align: justify;
list-style-type: none;
}
#list_box // div containing the whole list
{
border-right: 2px solid #b2dba1;
border-bottom: 2px solid #b2dba1;
border-left: 2px solid #b2dba1;
border-radius: 0 0 6px 6px;
width: 230px;
margin-left: 5px;
padding-bottom: 5px;
padding-top: 5px;
background: rgba(206,232,196,0.7);
padding-right:0;
}
.list_items // all the <li>
{
padding-bottom: 5px;
margin-left: 5px;
width: 100%;
border: 1px dashed red;
}
ul .list_items .dropdown button
{
position: relative;
right: 2px;
margin-right: 0;
border: 1px solid black;
}
.dropdown // div containing the button
{
display: inline-block;
margin: 0;
padding: 0;
position: relative !important;
right: 0 !important;
border: 1px solid blue;
}
What else could be done to solve this problem? Thanks!
You could add the standard bootstrap class 'pull-right' at the dropdown div.
Then it will look like this:
<div class="dropdown pull-right">
I think that should do the trick.