issues with horizontal menu - html

I have a weird problem with my horizontal drop down menu. Please find the code below.
My HTML Menu part looks like follows -
<div id="page-wrap">
<ul class="dropdown">
<li><img src="images/home.png" border="0"></li>
<li><img src="images/aboutus.png" border="0"></li>
<li><img src="images/profile.png" border="0">
<ul id="submenu">
<li id="firstItem"><img src="images/submenu_center_top.png"></li>
<li>Support Team</li>
<li id="lastItem"><img src="images/submenu_bottom.png"></li>
</ul>
</li>
<li><img src="images/activities.png" border="0">
<ul id="sub_menu">
<div><img src="images/submenu_center_top.png">
<li>Nutrition & Tips</li>
<div><img src="images/submenu_bottom.png"></div>
</ul>
</li>
<li><img src="images/shiningstars.png" border="0"></li>
<li><img src="images/testimonials.png" border="0"></li>
<li><img src="images/gallery.png" border="0">
<ul id="sub_menu">
<div><img src="images/submenu_center_top.png">
<li>Photo Gallery</li>
<li>Audio Visual Gallery</li>
<li>In Press</li>
<li>Articles</li>
<div><img src="images/submenu_bottom.png"></div>
</ul>
</li>
<li><img src="images/eventsnews.png" border="0">
<ul id="submenu">
<div><img src="images/submenu_center_top.png">
<li>Upcoming Events</li>
<div><img src="images/submenu_bottom.png"></div>
</ul>
</li>
<li><img src="images/contactus.png" border="0"></li>
</ul>
</div>
And my CSS file contains the following code:
#page-wrap {
width: 1004px; margin: 0;
}
a {
text-decoration: none;
}
ul {
list-style: none;
}
ul.dropdown {
position: relative;
}
ul.dropdown li {
font-weight: normal; float: left; zoom: 1; /*background: #ccc; */
}
ul.dropdown a:hover {
color: #fff;
}
ul.dropdown a:active {
color: #ffa500;
}
ul.dropdown li a {
display: block;
padding: 0px 0px 0px 0px;
/*border-right: 1px solid #333;
color: #000;*/
}
ul.dropdown li:last-child a {
border-right: none;
} /* Doesn't work in IE */
ul.dropdown li.hover, ul.dropdown li:hover {
background: #1C1C1C;
/*color: white;*/
position: relative;
}
ul.dropdown li.hover a {
color: black;
} /*LEVEL TWO*/
ul.dropdown ul {
width: 200px;
visibility: hidden;
position: absolute;
width: 100%;
top: 100%;
/*margin-left: -90px;*/
left: 0;
}
ul.dropdown ul li {
font-weight: normal;
font-size: 12px;
background: #333333;
color: #969696;
/*border-bottom: 1px solid #ccc;*/
border-left: 5px solid #969696;
border-right: 5px solid #969696;
/*float: left;*/
}
/* IE 6 & 7 Needs Inline Block */
ul.dropdown ul li a {
border-right: none;
width: 100%;
display: inline-block;
} /*LEVEL THREE*/
ul.dropdown ul ul {
left: 100%;
top: 0;
}
ul.dropdown li:hover > ul {
visibility: visible;
}
#firstItem, #lastItem {
background: none;
border-left: none;
border-right: none;
}
.sub_menu {
float: center;
}
I have been getting weird results both in IE 8 and Chrome too. The alignment is just not happening. Can somebody pleas ehlp? I am relatively new to HTMl and CSS.
Thanks in advance!

There are a lot of different things going on here.
The simplest answer for you though, for your alignment issues, is that your ul#sub_menu has padding.
Zero out the padding with this:
.sub_menu { padding: 0px; }
[Update: Your html should use class="sub_menu" instead of id="sub_menu". Your html is invalid if you have more than 1 element with the same id]
A common practice these days is to start your CSS file with a bunch of "resets". This will change the default padding and margin for a bunch of common tags to 0, so that you don't end up with spacing issues when you don't desire them. For example, some resets I normally employ:
body, div, ul, ol, li, p, h1, h2, h3
{ padding: 0px; margin: 0px; }
Other things that might help you, that I notice in your code:
float: center is not valid. The only valid values are: left, right, both, none, inherit
ul.dropdown ul ul does not reference anything. You only have 2 levels of ul, but this selector references 3 ul deep. You probably got this CSS from a drop down menu example. If you don't need a 3rd level of drop downs, it's best to remove this CSS so as not to confuse yourself in the future.
visibility: hidden, visibility: visible, I suggest using display: none and display: block. The reason is, that visibility: hidden still takes up space on the page, even though it is invisible. display: none however, does not render the DOM element at all, and it will not take up page space
You have <div> elements as direct child elements of the <ul> tag in some of your sub-menu's. Only <li> elements are allowed as direct children
You have some elements named sub_menu and some named submenu. You need to have consistent names if you want the styles to be applied correctly.
Here is a jsFiddle with your code, and all the changes I mentioned. I added width to your li and background colors to help see what was going on.

Related

Dropdown Submenu Disappears on Hover

While I know there are several discussions regarding this issue, none of the solutions fixed my problem. No matter what I do, the CSS submenu I'm trying to use disappears after you stop hovering over the parent li. I haven't the slightest idea what could be causing this, and I've really been staring at this forever trying to find a solution and just can't. I tried adding in a top: px; to the submenu in the CSS, which allowed me to select the submenu options, however it also moved the menu so that it would appear covering and centered over the parent li, which is also no good to me because I need it to appear directly below. Could the header be clipping it and if so what would I need to add to change that? All assistance is so greatly appreciated!
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-size: 1.2em;
line-height: 40px;
text-align: left;
display: none;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
padding-left: 15px;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #aaa;
color: #444;
cursor: default;
}
/* Sub Menus */
.nav li li {
font-size: .8em;
}
#media screen and (min-width: 650px) {
.nav li {
width: 130px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1.4em;
display: inline-block;
margin-right: -4px;
}
.nav a {
border-bottom: none;
}
.nav > ul > li {
text-align: center;
}
.nav > ul > li > a {
padding-left: 0;
}
/* Sub Menus */
.nav li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul {
display: block;
}
.nav li ul li {
display: block;
}
}
#header {
float: left;
background-color: #ffffff;
cursor: default;
padding: 1.75em 2em 0em 0em;
position: relative;
}
<header>
<img id="logo" src="images/logo.jpg" alt="logo">
<div class="nav">
<ul>
<li class="home">Home
</li>
<li class="tutorials">Tutorials
<ul>
<li>Tutorial #1##
</li>
<li>Tutorial #2
</li>
<li>Tutorial #3
</li>
</ul>
</li>
<li class="about"><a class="active" href="#">About</a>
</li>
<li class="news">Newsletter
<ul>
<li>News #1
</li>
<li>News #2###
</li>
<li>News #3
</li>
</ul>
</li>
<li class="contact">Contact
</li>
</ul>
</div>
</header>
I did figure this out eventually but thought I should come back and update with my solution, in case it is helpful to anyone who is having a similar issue. It was actually really simple.
I had to add a z-index here:
.nav li:hover ul {
display: block;
z-index: 99999;
}
This was recommended to other users, and I did try it initially but did not place it in li:hover thus it didn't work. I guess because the high z-index forces it to the top, it stopped whatever was causing the clipping by placing the submenu above it. I must have misread something somewhere along the line and placed the z-index in the wrong section. The real solution here is probably to read your code carefully!

Spacing on nav bar when hover

I have a problem with the navigation bar. When I hover over About or Text on the nav bar it shows a spacing on the left side of the button, I want it the hover colour to contain the full width of the button.
https://jsfiddle.net/jdd3h0sf/3/
HTML:
<div id="nav">
<ul>
<li class="home">Home</li>
<li>About</li>
<li>Text ⌄
<ul class="submenu">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
<li>Work</li>
<li>Contact ⌄
<ul class="submenutwo">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
</ul>
CSS:
#nav {
background-color: #333;
height: 52px;
text-align: center;
margin: 0 auto;
letter-spacing: 1px;
text-transform: uppercase;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
}
#nav li {
border-right: 1.8px solid #191919;
height: auto;
width: 156.5px;
padding: 0;
margin: 0;
}
.home {
border-left: 1.8px solid #191919;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #444;
}
#nav ul li a, visted {
color: #ccc;
display: block;
padding: 15px;
margin: 0;
text-decoration: none;
}
#nav ul li a:hover {
color: #ccc;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #444;
border: 1px solid #333;
border-top: 0;
max-width: 169px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:visited {
color: #ccc;
}
#nav ul ul li a:hover {
color: #2980B9;
}
This is a part of display:inline-block;. If you want to keep them displayed inline-block, there are several different solutions (Read a css-Tricks article about it):
1 - Change your HTML format:
Change your <li>'s html like this:
<ul>
<li>
one</li><li>
two</li><li>
three</li>
</ul>
Or this:
<ul>
<li>one</li
><li>two</li
><li>three</li>
</ul>
Or even with comments, like this:
<ul>
<li>one</li><!--
--><li>two</li><!--
--><li>three</li>
</ul>
Or, just place all the li's on a single line:
<ul><li>one</li><li>two</li><li>three</li></li>
It is messy, yet effective.
2 - Negative margins:
Pretty straightforward:
li{
display: inline-block;
margin-right: -4px;
}
3 - Skip the closing tag:
This is actually perfectly fine in HTML5, li's do not have to have a closing tag.
<ul>
<li>one
<li>two
<li>three
</ul>
4 - Set the <ul>'s font size to 0:
ul {
font-size: 0;
}
ul li {
font-size: 16px;
}
5 - Or, just float the <li>'s:
Whatever floats your boat.
You are experiencing the dreaded inline-block spacing issue. In your fiddle, if you condense all of your li elements to be on the same line, the hover works as expected. The linked article outlines a few other options.
You can also just float the elements and that would resolve the issue.
#nav ul li {
float: left;
}

How do i tackle cross-browser display problems with this button menu?

I set up a menu that uses buttons with links, ul's, and li's inside them. It works fine in Chrome, Android, Safari, and Opera. In Firefox, when the ul's appear the nav jumps down. In IE, the ul's don't display. In both, the links don't appear.
Edit: I chose to do this with buttons because i thought it gave me flexibility a regular ul menu wouldn't - background images, images inside them, attaching javascript events. It also of course creates a layout that is a row of buttons without any extra styling.
http://codepen.io/briligg/pen/emwXaw?editors=110
nav { position: fixed;
top: 0px;
right: 0px;
width: 70%;
float: right;
padding: 2%;
height: 34px;
max-height: 34px;
margin: 5px 0;
}
nav button {
border: 1px solid #666666;
border-radius: 10px;
background-color: #3b4c6d;
color: white;
padding: 0 4px;
height: 32px;
font: 16px;
}
nav button ul {
position: relative;
display: none;
}
nav button:hover ul, nav button:focus ul {
display: block;
z-index: 7;
list-style: none;
background-color: #3b4c6d;
border: 1px solid #666666;
border-radius: 10px;
margin-top: 9px;
padding: 6px 2px;
}
nav button:hover li, nav button:focus li {
padding: 8px 2px;
}
nav a {
text-decoration: none;
color: white;
}
nav a:hover, nav a:focus {
color: #52cbff;
}
Then in the html, the ul's are nested in the buttons, with links, like this:
<button tabindex="4">Being There
<ul tabindex="5">
<li>World Domination</li>
<li>Chickens</li>
<li>Down with Gravity</li>
<li>The Moonstar</li>
</ul>
</button>
In even creating this thing i was already at the limits of my knowledge. I don't know how to go about finding work-arounds, or if that is even possible in this case. Help with even knowing where to go to figure this out would be appreciated, never mind an actual solution to the problem. I've been looking for information and haven't found any.
IE has button {overflow:hidden;} style by default, You can rest that as follows.
nav button {
overflow: visible;
}
Edit: In order to get the links working we'll have to redo the markup, I also adjusted the CSS for the HTML changes. see the following code snippet.
nav {
position: fixed;
top: 0px;
right: 0px;
width: 70%;
float: right;
padding: 2%;
height: 34px;
max-height: 34px;
margin: 5px 0;
white-space: nowrap;
}
nav > ul > li {
display: inline-block;
position: relative;
font-size: 16px;
height: 32px;
line-height: 32px;
border: 1px solid #666666;
border-radius: 10px;
background-color: #3b4c6d;
color: white;
padding: 0 4px;
}
nav > ul > li > ul {
display: none;
list-style: none;
background-color: #3b4c6d;
border: 1px solid #666666;
border-radius: 10px;
padding: 6px;
position: absolute;
z-index: 7;
top: 32px;
left: 0;
}
nav > ul > li:hover > ul {
display: block;
}
nav a {
text-decoration: none;
color: white;
}
nav a:hover {
color: #52cbff;
}
<nav>
<ul>
<li tabindex="1">Purpose</li>
<li tabindex="2">
Moon vs Mars
<ul tabindex="3">
<li>Ambiance</li>
<li>Communication</li>
<li>There and Back</li>
</ul>
</li>
<li tabindex="4">
Being There
<ul tabindex="5">
<li>World Domination</li>
<li>Chickens</li>
<li>Down with Gravity</li>
<li>The Moonstar</li>
</ul>
</li>
</ul>
</nav>
The problem must be caused by this Link inside a button not working in Firefox (and IE).
Full Demo: http://codepen.io/anon/pen/KwOqKv
Instead of putting <a> in <button>, put all <a> inside <li>. Also, as you had, put the secondary links inside another <ul> in the <li>.
<ul class='primary-links'>
<li class='primary'><a href='#'>Primary link</a></li>
<li class='primary'>
<a href='#'>Another primary link</a>
<ul class='secondary-links'>
<li class='secondary'><a href='#'>Secondary Link</a></li>
<li class='secondary'><a href='#'>Another secondary link</a></li>
</ul>
</li>
</ul>
The primary links are display:inline-block in order for them to display horizontally while the secondary links are display:none to initially hide them. The secondary links become visible when the primary links are hovered over. position:absolute removes the secondary links from the document flow preventing the primary links from jumping down when the secondary links become visible.
.primary {
display: inline-block;
}
.secondary-links {
display: none;
position: absolute;
}
.primary:hover > .secondary-links {
display: block;
}
body {
font: 1em/1.5 sans-serif;
}
a:link,
a:visited {
color: #08f;
text-decoration: none;
}
a:hover,
a:active,
a:focus{
color: #f80;
}
ul {
list-style: none;
margin: 0;
padding: .25em;
border-radius: .25em;
background: #fff;
border: thin solid #ccc;
box-shadow: 0 0 .25em #ccc;
}
li {
margin: .5em;
}
nav > ul > li {
display: inline-block;
}
li > ul {
display: none;
position: absolute;
}
li:hover > ul {
display: block;
}
<nav>
<ul>
<li><a href='#'>One</a></li>
<li>
<a href='#'>Two</a>
<ul>
<li><a href='#'>Two One</a></li>
<li><a href='#'>Two Two</a></li>
<li><a href='#'>Two Three</a></li>
</ul>
</li>
<li>
<a href='#'>Three</a>
<ul>
<li><a href='#'>Three One</a></li>
<li><a href='#'>Three Two</a></li>
<li><a href='#'>Three Three</a></li>
<li><a href='#'>Three Four</a></li>
</ul>
</li>
</ul>
</nav>

My horizontal drop down with CSS, sub navigation menu items are being displayed on top of each other

my sub navigation menu items are being displayed on top of each other
here is the code:
/* NAVIGATION */ .nav-bar {width: 100%; height: 80px; background-image:url(../images/bg-menu80.jpg);}
.nav-hold {overflow: hidden;}
.nav-list {float: right;}
.nav-list li {float: left; width: auto; position: relative;}
.nav-list li a {text-decoration: none; display:block; padding: 30px 7px 20px 7px; color: #f9f9f9; font-size: .9em; font-weight: bold;}
.nav-list li ul {display: none;}
.nav-list li a:hover {text-decoration: none; display: block; padding: 30px 7px 20px 7px; color: #000; font-size: .9em; font-weight: bold; background-color: #e7e4e4;}
.nav-list li a:hover li{display: block; position: absolute; margin: 0; padding: 0;}
.nav-list li a:hover li{float: left;}
.nav-list li:hover li a{ background-color: #333; border-bottom: 1px solid #fff; color: #FFF;}
<ul class="nav-list" id="navigation"><!--Menu list-->
<li>Home</li>
<li>About Us</li>
<li> <ul>Members
<li>Board of Directors</li>
<li>Committee</li>
</ul></li> <li>Join Us</li> <li>Events</li>
<li>Rules & Guidelines</li> <li>Archive</li> <li>Contact Us</li> <li>Login</li> </ul><!--ENDS Menu list-->
Your code doesn't really do much when you copy it directly into a jsfiddle (a website used to 'mock up' and test your HTML, CSS and JS), and there's not much description about what you are expecting/wanting to happen, so there's going to be some guess work here...
I think there are 2 problems here.
The HTML
First, the <li> that has the subnavigation <ul> inside it has some incorrect html.
The Members needs to be before the <ul>, not inside it.
So it should look like this:
<li>
Members
<ul>
<li>Board of Directors
</li>
<li>Committee
</li>
</ul>
</li>
The CSS
Second, you are trying to display the subnavigation <ul> (by default this is display: none) when you hover on the <a> tag, but you need to do it when you hover the <li>, because it's that element that contains the subnavigation <ul>.
So it should look like this:
.nav-list li ul {
display: none;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
}
.nav-list li:hover ul {
display: block;
}
Here's a fiddle with my tweaks, hopefully that'll get you back on track.
http://jsfiddle.net/davidpauljunior/ve9Ub/

drop down menu won't go horizontal

I'm making my first website with HTML and CSS and it's all gone fine, but now I'm stumped. I made a pretty typical horizontal menu under my header using the inline command in my CSS. Then I decided I wanted drop down elements on my menu, so deleted all my menu css and copied in some new from a tutorial for horizontal drop down menus online. The drop downs work perfectly, but now, for some reason, my menu is vertical! Any ideas how I can make it horizontal again? Hoping it should be relatively easy! Thanks so much.
Here's my html (in my .html sheet)
<div class="header">
<div class="topimagebox">
<img src="images/header1.jpg" class="topimage"; alt="Nets drying at Folkestone harbour";>
</div> <!--topimagebox-->
<ul id="Menu">
<li >Home</li>
<li >About
<ul>
<li >Context</li>
<li >The Project</li>
<li >People</li>
</ul>
</li>
<li >News</li>
<li >Publications</li>
<li >Case Studies</li>
<li >Contact
<ul>
<li >Contact</li>
<li >Get Involved</li>
</ul>
</li>
<li class="menu" >Impact</li>
<li class="menu">Links</li>
</ul>
</div> <!--header-->
And here's my CSS (in a seperate stylesheet)
div.header {
background-color: white;
width: 1000px;
margin-bottom: 10px;
}
ul {
padding: 0px;
padding-top: 20px;
padding-bottom: 20px;
margin: 0px;
}
li {
width: 850px;
margin:0px auto 0px auto;
}
#Menu {
margin: 0;
padding: 0;
height: 1em; }
#Menu li {
list-style: none;
float: left; }
#Menu li a {
display: block;
padding: 3px 8px;
background-color: #5e8ce9;
color: #fff;
text-decoration: none; }
#Menu li ul {
display: none;
width: 10em; /* Width to help Opera out */
background-color: #69f;}
#Menu li:hover ul {
display: block;
position: absolute;
margin: 0;
padding: 0; }
#Menu li:hover li {
float: none; }
#Menu li:hover li a {
background-color: #69f;
border-bottom: 1px solid #fff;
color: #000; }
#Menu li li a:hover {
background-color: #8db3ff; }
I put in my generic ul and li in case that had something to do with it. If anyone can help, I'd really appreciate it.
li {
width: 850px; //<-- This is your problem.
margin:0px auto 0px auto;
}
If you remove the width the li-elements will float next to each other, until there isnt anymore room for it to float, then it will break onto a new row.