How to get chevron to align to center of link tab - html

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>

Related

Bootstrap dropdown hover issue

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;
}

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 :)

List item background changed color while hover

Hi I have a problem using list item on the side bar.
I use image inside the list item for navigation bar and whenever I hover over the image, there is a white background appear behind the image and I don't know how to fix this.
Please help me.
Here is the screenshot of the screen
Screenshot
And here is the code
<ul class="nav nav-sidebar">
<li><img src="Side_Bar_BackToTop.png"></li>
<li><img src="Side_Bar_About.png"></li>
<li><img src="Side_Bar_History.png"></li>
<li><img src="Side_Bar_Movie.png"></li>
</ul>
CSS:
.sidebar{
position: fixed;
top: 0;
bottom: 0;
left: 0;
z-index: 1000;
display: block;
padding: 20px;
overflow-x: hidden;
overflow-y: auto;
background-color: #905c2a;
border-right: 1px solid #eee;
width: 200px;
[}
.nav-sidebar{
position: fixed;
left: -20px;
}
.nav-sidebar li{
margin-bottom: 10px;
}
.nav-sidebar li a{
text-decoration: none;
}
.nav-sidebar li img:hover{
border-top: 2px solid #f4d37f;
border-bottom: 2px solid #f4d37f;
}][1]
I guess you are using bootstrap. Please let me know whether this is what you expected. Thanks!
Here is the fiddle: fiddle
Added the following style to remove the background behind the image:
.nav-sidebar li a:focus, .nav-sidebar li a:hover {
text-decoration: none;
background-color: transparent;
}

Override parent's border when hovering

I have a ul menu inside a div .menu. The parent .menu has a top and bottom 1px solid border. When hovering over an li element another border get added but the parent border still appear above it.
I want to not display the .menu top border when hovering over an li I don't want to use js unless it's the only solution.
Here is my code and my temporary fix.
HTML:
<div class="menu">
<ul>
<li>Home</li>
<li>Fun</li>
<li>Work</li>
</ul>
</div>
CSS:
.menu{
width: 100%;
background-color: black;
/* I want this border to... ↓ */
border-top: 1px solid black;
border-bottom: 1px solid black;
box-sizing: border-box;
}
.menu ul{
list-style: none;
overflow: hidden;
margin: 0px;
height:35px;
}
.menu ul li{
display: block;
float:left;
border-left: 1px solid gray;
position: relative;
}
.menu ul a{
color: #FFF;
padding: 10px;
display: block;
}
.menu ul a:hover{
text-decoration: none;
background-color: white;
color: pink;
/* ...not be displayed above this border when hovering */
border-top: 3px solid pink;
}
I only found this inefficient solution:
.menu ul:hover{
position: relative;
top: -1px;
}
Codepen link:http://codepen.io/eldev/pen/YPYLQz?editors=110
Any thoughts ?
Edit:
backgound-color isn't the same as border-color I made it by mistake. Codepen link updated.
there are few solutions at the same time.
like this? (or i don't understand correctly) http://codepen.io/anon/pen/XJVYMW
i have added margin-top: -1px for ul
There had to be some modification don't to the CSS; to make it easier to understand, I have added all my changes to the HTML section. I have NOT modified the CSS section.
Here you go http://codepen.io/anon/pen/rapKxE
And the beautified version http://codepen.io/anon/pen/MYrXQN
< Refer to CodePen >
NOTE Check the code on codepen for the latest updates.
There are four solutions :
margin-top: -1px; ;
Javascript ;
position: relative; top: -1px; ;
transform: translateY(-1px).

How to make a multi-tiered dropdown menu with HTML and CSS only (maybe tiny JavaScript)

Newbie to CSS and can only find single level menus. Here's the Menu and the list items:
<ul>
<li>
Home
</li>
<li>
Forums
<ul>
Basketball
<ul>
<li>
Trading
</li>
<li>
Personal Collections
</li>
<li>
Box Breaks
</li>
</ul>
</ul>
</li>
</ul>
As you can see, it would be multi-tiered. Now, with the CSS I have, only Home and Forums are displayed first, and when I hover over Forums, Basketball is displayed...but so are the subsequent menu items. I want those to stay hidden until I hover over basketball. Anyone know how to do this with just CSS or as little JavaScript as possible? Thanks. Here's the CSS code I have:
ul
{
margin: 0;
padding: 0;
list-style: none;
width: 150px;
border-bottom: 1px solid #ccc;
}
ul li
{
position: relative;
}
li ul
{
position: absolute;
left: 149px;
top: 0;
display: none;
}
ul li a
{
display: block;
text-decoration: none;
color: #777;
background: #bad8f8;
padding: 2px 0 2px 10px;
border: 1px solid #ccc;
border-bottom: 0;
}
li:hover ul
{
display: block;
}
/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
border: 1px solid #ff0000;
background-color: #ffeeee;
}
.validation-summary-errors
{
font-weight: bold;
color: #ff0000;
}
.validation-summary-valid
{
display: none;
}
Here is a working demo:
http://jsfiddle.net/rcravens/aqz8q/
Two things I did.
A bit of restructuring the ul/li list. There were some elements not in the li.
Used 'li:hover > ul' to select the direct children only.
Hope that helps.
Bob
Try to add
ul li ul{position: absolute; top: 0; left: 0; width: 250px; height: 250px; background-color: #EEE;}
And go from there :)
The way you have structured your css causes all of "Basketball"'s descendants to inherit its css. You should be using a child(">") or :first-child selector instead. Look at sections 5.5 and 5.6 here to know what I am talking about: http://www.w3.org/TR/CSS2/selector.html
If you are looking to do dynamic menus I'd strongly recommend using javascript over relying purely on css, unless you are certain a lot of people viewing your website are going to have their javascript turned off.