CSS horizontal menu links issue - css

I've got a simple (horizontal) CSS menu list with a problem. The links in the list are only active up to a certain point, for example menu item four in my list doesn't work, 1, 2, and 3 are fine. By not working I mean both 'hover' and hyperlinking actions are gone.
It's got something to do with the 'float' and 'overflow' attributes, if I don't float the #nav element and remove 'overflow:hidden', I can get all the links working but the formatting is screwed, to a greater or lesser degree, depending on the browser.
The code is below, any help would be great. Oh and the commented left and right 50% attributes were there because if I centred the menu none of the links worked :-(
<div id="nav">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</div>
#nav
{
float: left;
height:30px;
width: 940px;
margin:0;
position: relative;
overflow: hidden;
white-space: nowrap;
font-family: Helvetica Neue, Helvetica, Verdana, sans-serif;
font-size: 20px;
font-weight: 200;
background-color: #333333;
list-style-type: none;
}
#nav ul
{
margin:0;
padding-left: 0;
/*left: 50%;*/
}
#nav ul li
{
display: inline;
list-style: none;
padding: 2px 6px 2px 6px;
/*right:50%;*/
}
#nav ul li a
{
float: left;
display: block;
display: inline;
text-decoration: none;
color: #ffffff;
padding:3px;
text-align: left;
}
#nav li a:hover { color: #6698FF;}
#nav li a.active { color: #6698FF;}

The style for #nav ul li a seems weird: using float:left with display:inline is asking for trouble. Since you are already floating the parent element (li) , i don't think you need to float the a element. I'm not sure which kind of menu you wish to achieve, but usually, you float the ul li, and display:block the A inside.
So, try changing this:
#nav ul li a
{
float: left;
display: block;
display: inline;
text-decoration: none;
color: #ffffff;
padding:3px;
text-align: left;
}
to this
#nav ul li a
{
display: block;
text-decoration: none;
color: #ffffff;
padding:3px;
text-align: left;
}

Try making the following adjustments to your css below.
#nav ul li a
{
float: left;
display: inline-block;
text-decoration: none;
color: #ffffff;
padding:3px;
text-align: left;
}
#nav ul li a:hover, #nav ul li a.active
{
color: #6698FF;
}

I'm not sure I understand exactly what you want this to look like. But, by "simple (horizontal) CSS menu list" I assume you are looking for all the links lined up on the background?
Try this:
#nav {
height:30px;
width: 940px;
margin:0;
white-space: nowrap;
font-family: Helvetica Neue, Helvetica, Verdana, sans-serif;
font-size: 20px;
font-weight: 200;
background-color: #333333;
}
#nav ul {
margin:0;
padding-left: 0;
list-style: none;
}
#nav ul li {
float: left;
display: inline;
padding: 2px 6px 2px 6px;
}
#nav ul li a {
display: block;
text-decoration: none;
color: #ffffff;
padding:3px;
}
#nav li a:hover { color: #6698FF;}
#nav li a.active { color: #6698FF;}
All you need to do is float the lis whichever direction you want them. For more flexibility, remove the height from #nav and put overflow: hidden; back in it's place. That way the height will be determined by the contents: a more solid solution.
If you want the list centered, make these changes to it:
#nav ul {
margin:0;
padding-left: 0;
list-style: none;
text-align: center;
}
#nav ul li {
display: inline;
padding: 2px 6px 2px 6px;
}
#nav ul li a {
display: -moz-inline-box;
-moz-box-orient: vertical;
display: inline-block;
vertical-align: middle;
#display: inline;
#vertical-align: auto;
text-decoration: none;
color: #ffffff;
padding:3px;
}
(all that junk under #nav ul li a is simply a cross-browser method for reliable inline-block declarations)

Related

Pure CSS horizontal drop menu, show in click instead of hover [duplicate]

This question already has answers here:
Can I have an onclick effect in CSS?
(14 answers)
Closed 7 years ago.
Is it possible with pure CSS? That instead of showing a dropdown when I hover over "Test", show it when I click on "Test".
Also keep the yellow link color on "Test" when I have that dropdown open.
My current CSS
nav {
text-transform: uppercase;
text-align: center;
margin-bottom: 20px;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
text-align: left;
}
nav a {
display:block;
padding:0 20px;
transition: color .2s;
color: black;
font-size: 16px;
font-weight: 600;
line-height: 40px;
text-decoration: none;
}
nav ul ul li a { color: #FFFF64; }
nav ul ul li a:hover { color: #99A7EE; }
nav a:hover { color: #FFFF64; }
nav ul ul { display: none; position: absolute; }
nav ul li:hover > ul { display: inherit; }
nav ul ul li { background: #000; display: list-item; position: relative; }
nav ul ul li:hover { background: #333; }
http://jsfiddle.net/hn93jyvc/
You can change :hover to :active like this: http://jsfiddle.net/hn93jyvc/1/
nav ul ul li a { color: #FFFF64; }
nav ul ul li a:active { color: #99A7EE; }
nav a:active { color: #FFFF64; }
nav ul ul { display: none; position: absolute; }
nav ul li:active ul { display: inherit; }
nav ul ul li { background: #000; display: list-item; position: relative; }
nav ul ul li:active { background: #333; }
However, this will not keep state and will only work on mouse down, once you release the mouse button, the menu will disappear.
As far as I know here, you would have to use a bit of javascript to attach active to this element until another click, or the desired logic.
You can see how achieve that from this answer: https://stackoverflow.com/a/20343068/5242026
Albeit, that answer uses jQuery, you could just as well make use of Javascript click event: https://developer.mozilla.org/en/docs/Web/Events/click which makes more sense if there are no other usages for jQuery.
Like i said in my comment. This can be done using the checkbox combined with css pseudo ':checked' selector.
HTML
<ul>
<li>Home</li>
<li><label for="dd">Services</label>
<input type="checkbox" id="dd" hidden>
<ul class="dropdown">
<li>clean</li>
<li>fix</li>
<li>paint</li>
</ul>
</li>
<li>contact</li>
</ul>
CSS
ul{
background: blue;
display:block;
list-style-type: none;
text-align:center;
}
ul li{
display: inline-block;
color: white;
font-family: sans-serif;
font-weight: 800;
font-size: 20px;
padding: 10px 15px;;
position:relative;
// keep users from highlighting text if they click it on/off too fast
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
ul li:hover{
background: #f9a1c6;
color: #000;
}
ul li .dropdown{
display:none;
width: 200px;
padding:0; margin:0;
background: green;
position: absolute;
top: 45px;
left:0;
}
ul li .dropdown li{
width:100%;
display:block;
padding:10px 0px; margin:0px;
}
#dd:checked ~ .dropdown{
display:block;
}
jsfiddle

Sub-menu will not stack vertically

I am trying to vertically stack a sub-menu, however, I am having trouble getting it to stack possibly due to some of the parent styling. The goal is for the sub-menu to stack vertically underneath the very first link. Any advice?
JSFIDDLE
CSS
nav {
height: 70px;
background: transparent;
}
nav ul {
display: inline-block;
list-style: none;
height: 70px;
float:right;
}
nav ul li {
display: inline-block;
background: transparent;
float: left;
padding: 0px 10px;
padding-top: 23px;
height: 43px;
border-top: 4px solid $White;
}
nav ul li:first-child {
border-top: 4px solid $White;
}
nav ul li a {
font-family: 'Lato', serif;
font-size: 14px;
color: $TextColor;
line-height: 122%;
letter-spacing: 1.5px;
padding-left: 0;
}
nav ul ul {
display: block;
}
nav ul ul li {
display: block;
}
nav ul ul li a {
display: block;
}
Man, you really need to learn what you are doing. It was quite some effort to correct your code. For a start, you seem to be unaware of the fact that
nav ul { ... }
will not only affect your outer <ul>, but also your inner <ul id="submenu">.
The fact that you use id="submenu" strongly suggests that the next thing you might be unaware of is that per HTML document, any given id value must be unique, that is it can only be used on one element.
https://jsfiddle.net/gmtugks5/2/

Menu items displaying behind others on Wordpress

I'm having some issues with my menu being displayed properly. My 3rd level menu items are hidden behind the others for some reason. I've gone over my template I'm using and things seem to be alright on that end, otherwise the menu option wouldn't even be displayed. Thus I'm thinking something is wrong with my CSS, though I can't see what that would be. Does anyone have any ideas as to what's going on here? You can see an example at http://www.bpwsaskatoon.com and then hovering on the "Membership" option at the top.
The problem is in the css. First you should use classes or id:s on your navigation styles. Example ul li is now targeting every ul li. By using #nav ul li will target only all ul li inside #nav. In html the ul in .main_nav_menu should be div. You have now ul directly under ul.
I would recommend that you try some jquery plugin for dropdown menus. Example Superfish is pretty good, it takes care of many thigs that you should consider in dropdown menus, example touch events.
Here is a quick css that should display the 3rd level menu items.
/*Navigation styles*/
#nav{
display:table;
margin:0 auto 0 auto;
position:relative;
padding:5px;
}
#mnwrpr{
height:48px;
box-shadow: 0 1px 2px rgba(0,0,0,0.5);
clear: both;
display: block;
position: relative;
width: 100%;
z-index:1;
}
#nav ul {
font-family:'Lato', sans-serif;
font-size: 15px;
margin: 0;
padding: 0;
}
#nav ul li {
display: block;
position: relative;
float: left;
}
#nav li ul {
display: none;
}
#nav ul li a {
display: block;
text-decoration: none;
color: #8b8b8b;
padding: 5px 15px 5px 15px;
margin-left: 1px;
white-space: nowrap;
}
#nav li:hover ul {
display: block;
position: absolute;
}
#nav li:hover li {
float: none;
font-size: 14px;
}
#nav li:hover li { background: #ececec; }
#nav li:hover li a:hover {
background: #bcbcbc;
}
#nav ul li ul li ul {
top: 0;
left: 100%;
z-index: 99;
min-width: 12em;
position: absolute;
margin: 0;
padding: 0;
list-style: none;
display: none!important;
}
#nav ul li ul li:hover ul {
display: block!important;
}
#nav ul li ul li ul li {
position: relative;
margin: 0;
padding: 0;
list-style: none;
}

Hover Style only to appear on main menu not submenu?

I have a main menu that i have styled with an underline appearing on the hover state.
However I don't want this to appear on the submenu categories.
This is my code:
#menu_container { padding: 0px 0; text-align: center; }
#menu_container a:hover{background: url("/main/nav.gif") repeat-x scroll 0px 20px transparent;}
#menu_container ul { list-style-type: none; margin: 0; padding: 0; text-align: center; }
#menu_container ul li { margin-left: 15px; font-family: 'Lato', sans-serif; font-weight: 900; font-size: 1.2em; color: #000; position: relative; padding-bottom: 10px; text-transform: uppercase; display: inline-block; letter-spacing:1px; }
#menu_container ul li:first-child { margin-left: 0; }
#menu_container ul li a { color: #000; text-decoration: none; display:inline-block; padding-bottom: 5px; padding:10px; border-radius: 0px;}
#menu_container ul li a:hover { color: #000; -webkit-transition: background 0.5sease;-moz-transition: background 0.5sease;-ms-transition: background 0.5sease;-o-transition: background 0.5sease;transition: background 0.5sease;}
#menu_container ul li.current-menu-item a,
#menu_container ul li.current_page_item a { color: #b9b9b9;}
#menu_container ul li ul { position: absolute; top: 35px; left: 0; padding: 1px 1px; z-index: 25; display: none; border-radius: 3px; text-align: left; }
I think you're asking for this:
#menu_container ul li a { text-decoration: none; ...}
#menu_container ul li a:hover { text-decoration: underline; ... }
#menu_container ul li li a:hover { text-decoration: none; ... } /* no hover underline on sublinks */
The underlying idea is selector specificity. A specific contextual selector (#menu_container ul li li a) overwrites a more generic definition (#menu_container ul li a).
See CSS priority scheme:
http://en.wikipedia.org/wiki/Cascading_Style_Sheets#CSS_Priority_scheme_.28highest_to_lowest.29

Editing Nav Menu - How Do I Wrap display:block, float:left to have a Centered Menu?

Right, so, I've been self-editing a rather complicated site and have been stuck on this last part involving the menu. It has a float:left command (the navbar displays alined to the left of the screen) and I've been trying to get it to text-align:center (appear centered within the screen) with a wrapper but have been unsuccessful.
Below is the coding:
#nav a {
display: block;
padding: 5px;
color: #aaa;
text-decoration: none;
outline: none;
font-weight: normal;
font-family: "Droid Serif", Georgia ,serif;
}
#nav > .current-menu-item > a,
#nav > li a:hover { color: #4C4C4C }
.plus { color: #aaa }
#nav ul {
position: center;
display: none;
}
#nav li {
float: left;
position: relative;
list-style-type: none;
padding-bottom: 5px;
}
#nav li a {
letter-spacing: 3px;
font-size: 14px;
text-transform: uppercase;
padding: 8px 15px;
I'm a programming noob and pretty much survive on combination of trial and error and the "inspect element" command in google chrome. Please be gentle and rather explanatory :)
cheers,
Jess
If you want to center the ul in the nav, just add width to ul and margin: 0 auto;
If you do not want to add a width check out How do I center align horizontal <UL> menu?
instead of float give display:inline-block to your LI . Write like this:
#nav li {
display:inline-block;
*display:inline;/* For IE*/
*zoom:1;
position: relative;
list-style-type: none;
padding-bottom: 5px;
}

Resources