HTML 5 navigation menu - html

This is my HTML5 for nav bar. However, the submenu will not hide and the menu will not display horizontally. Home, American and Foreign and the topmenu options and speed,usability and price are submeny. However Foriegn is displaying below and not next to American. Thank you in advance.
<nav>
<ul class="topmenu">
<li> Home</li>
<li>American</li>
<ul class="submenu">
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
<li>Foreign</li>
<ul class="submenu">
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
</ul>
</nav>
CSS
nav{
position: relative;
background-color: #00baff;
width: 50%;
margin-left: auto;
margin-right: auto;
}
.topmenu li{
position: relative;
list-style-type: none;
font-size: 20px;
display: inline-block;
margin-right: 30px;
color: white;
}
.topmenu li a{
padding: 5px;
}
.topmenu a:link{
text-decoration: none;
color: #ffffff;
}
.topmenu a:visited{
text-decoration: none;
color: white;
}
.topmenu a:hover{
background-color: #ffffff;
}
.topmenu li .submenu li{
display: none;
list-style-type: none;
}
.topmenu li:hover .submenu li{
display: block;
position: relative;
}
Fiddle

SOLVED:
CSS:
nav{
position: relative;
background-color: #00baff;
width: 50%;
margin-left: auto;
margin-right: auto;
}
/* This is your main UL resize to fit */
.topmenu{
width: 100%;
height: 150px;
}
/* I used > li to target all elements of .topmenu */
.topmenu > li{
font-size: 20px;
margin-right: 10px;
color: white;
list-style:none;
display:inline;
float: left;
}
.topmenu > li a{
padding: 5px;
}
.topmenu a:link{
text-decoration: none;
color: #ffffff;
}
.topmenu a:visited{
text-decoration: none;
color: white;
}
.topmenu a:hover{
background-color: #ffffff;
}
/* new code
What this does is it targets all child UL elements on li:hover
if they have them they will become VISIBLE if not nothing happens */
.topmenu > li:hover >ul{
visibility:visible;
}
/* your two sub menus set to hidden */
.american-submenu{
visibility:hidden;
}
.foreign-submenu{
visibility:hidden;
}
HTML
<nav>
<ul class="topmenu">
<li> Home</li>
<li>American
<ul class="american-submenu">
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
</li>
<li>Foreign
<ul class="foreign-submenu">
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
</li>
</ul>
</nav>

HMTL
<nav>
<ul class="topmenu">
<li> Home</li>
<li>American
<ul>
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
</li>
<li>Foreign
<ul>
<li> Speed</li>
<li> Price</li>
<li> Usability</li>
</ul>
</li>
</ul>
</nav>
CSS
nav{
position: relative;
background-color: #00baff;
width:100%;
}
.topmenu ul {
list-style:none;
}
.topmenu li {
float:left;
position:relative;
list-style-type: none;
font-size: 20px;
display: inline-block;
background-color: #00baff;
color: white;
text-align:center;
padding:5px;
}
.topmenu li ul {
display:none;
position:absolute;
text-decoration:none;
}
.topmenu li:hover ul{
display:block;
background-color: #00baff;
color:white;
height:auto;
width:8em;
}
.topmenu li ul li{
clear:both;
border-style:none;}
.topmenu li a{
padding: 5px;
}
.topmenu a:link{
text-decoration: none;
color: #ffffff;
}
.topmenu a:visited{
text-decoration: none;
color: white;
}
.topmenu a:hover{
background-color:white ;
color:#00baff;
}
Fiddle Demo

Related

CSS for direction of list elements in nested Drop-Down Menu?

I don't know how to change the code to handle arbitrary nested items? I have no clue how to this to apply for multiple levels. The following works just for 2 levels. But if i add a 3. level it fails. I think this can be done cleverly without writing manually each level?
I want that it can be adjusted to arbitrary many childs and it should be work with hover and clicks.
Thank you very much.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover ul {
display:block;
}
.nav ul li ul li {
display: block;
float:left; /* newly added */
height:auto; /* newly added */
line-height:34px; /* newly added */
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Is this something you wanted? I just added a > in .nav li:hover ul.
See comments in code below. ZZZZ, XXXX, and YYYY are added by me.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
padding: 0.5rem;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
/*.nav li:hover ul { OLD CODE
display:block;
}*/
.nav li:hover > ul { /* ADDED the charcter '>' */
display:block;
}
.nav ul li ul li {
display: block;
float:left;
height:auto;
line-height:34px;
}
<div class="nav">
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>ZZZZ
<ul> <!-- ADDED -->
<li>XXXX</li>
<li>YYYY</li>
</ul>
</ul>
</li>
</ul>
</li>
</ul>
</div>
You can use flex-inline and > for this approach of making menus.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover > ul {
display: inline-flex !important;
}
.nav ul li ul li {
/*display: block;*/
float:left;
height:auto;
line-height:34px;
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
I think what you're looking for is a multi-level dropdown. This is easier to do with separate classes rather than all one class (nav). I rewrote it for the sake of organization, but the design tweaks are really up to you! When running this snippet, make sure to use full screen because StackOverflow's snippet will distort the display a little bit.
.menu1 li {
list-style: none;
text-align: center;
position: relative;
float: left;
height: 30px;
width: 150px;
background-color: white;
}
.menu1 li:hover {
background-color: #5c89c7;
}
.menu1 li:hover>ul {
display: inline;
}
.menu1 a {
border-bottom: none;
font-family: Roboto, sans-serif;
color: black;
text-decoration: none;
padding: 0 0 0 10px;
display: block;
line-height: 30px;
}
.menu1 a:hover {
color: white;
}
.menu2 {
position: absolute;
top: 30px;
left: 0;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu2>li {
position: relative;
height: 30px;
background: #999999;
}
.menu2>li:hover {
background: #CCCCCC;
}
.menu3 {
position: absolute;
top: 0;
right: -150px;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu3>li {
height: 30px;
background: #999999;
}
.menu3>li:hover {
background: #CCCCCC;
}
<ul class="menu1">
<li>HOME</li>
<li>ABOUT</li>
<li>
SERVICES
<ul class="menu2">
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>
CONTACT US
<ul class="menu2">
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul class="menu3">
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>

Dropmenu Problem is not show with html and css

ul#menu li ,ul.sub-menu li { list-style-type: none; float:left; }
ul#menu li a ,ul.sub-menu li a
{ display: inline-block; width: 150px; height: 40px; text-decoration: none; line-height: 40px; text-align: center; color:rgb(235, 139, 13); background-color:black; border-radius: 20px; }
ul#menu li a:hover ,ul.sub-menu li a:hover { background-color: crimson; font-weight: bold; color:white; display: block; border-radius: 20px;
}
ul#menu li { position: relative;
} ul#menu li ul.sub-menu { display:none; position: absolute; top: 30px; left: 0px; width: 100px;
} ul#menu li :hover ul.sub-menu { display: block;
}
Dropdown Menu
menu1
menu2
submenu1
submenu2
submenu3
submenu4
</li>
</ul>
Change this ul#menu li :hover ul.sub-menu to ul#menu li:hover ul.sub-menu. That is the only issue I can see assuming your HTML might be like below.
<ul id="menu">
<li>
menu1
</li>
<li>
menu2
<ul class="sub-menu">
<li>
submenu1
submenu2
submenu3
submenu4
</li>
</ul>
</li>
</ul>

What do i have to change in my CSS so that when hover over the navigation menu, the hover is not all over the place like it is now?

So we have to create our own webpage using css and html in IT class and I decided that I wanted my navigation menu a little bit shorter than I had at the beginning but now I can't figure out how to change the css so that when I hover over my navigation menu the hover isn't all over the place like it is now.
Can someone help me out please.?
#nav {
height:28px;
background-color:#222;
position: fixed;
top: 92px;
left: 0;
margin-bottom:0;
box-shadow: 10px 2px 5px #888;
blur: 3px;
spread: 2px;
}
#nav_wrapper {
width: 1185px;
height:28px;
margin-left: 260px;
text-align: left;
margin-top:-10px;
}
#nav ul{
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
}
#nav ul li{
font-family:arial;
font-size: 15px;
display: inline-block;
}
#nav ul li:hover{
background-color: #333;
}
#nav ul li img{
width: 8px;
height:8px;
vertical-align:middle;
padding-left: 10px;
}
#nav ul li a,visited{
color: #ccc;
display:block;
padding: 15px;
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: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -1px;
min-width: 80px;
padding:0px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a,visited{
color: #ccc;
}
#nav ul ul li a:hover{
color: #099;
}
<div id="nav">
<div id="nav_wrapper">
<ul>
<li>
<a href="home.html">
<img src="pictures/home.png" alt="" style="width:18px;height:18px;vertical-align:middle;padding-right:11px;"/>
</a>
</li>
<li>HTML <img src="pictures/arrow.png" alt="" />
<ul>
<li>Intro</li>
<li>Basic</li>
<li>Tags</li>
<li>Links</li>
<li>Classes</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
</ul>
</li>
<li>CSS <img src="pictures/arrow.png" alt="" />
<ul>
<li>Intro</li>
<li>Basics</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
</ul>
</li>
</ul>
</div>
</div>
I would suppose this is what you want? (remove negative margin in #nav_wrapper and reduce padding in #nav ul li a)
#nav {
height:28px;
background-color:#222;
position: fixed;
top: 92px;
left: 0;
margin-bottom:0;
box-shadow: 10px 2px 5px #888;
blur: 3px;
spread: 2px;
}
#nav_wrapper {
width: 1185px;
height:28px;
margin-left: 260px;
text-align: left;
}
#nav ul{
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
}
#nav ul li{
font-family:arial;
font-size: 15px;
display: inline-block;
}
#nav ul li:hover{
background-color: #333;
}
#nav ul li img{
width: 8px;
height:8px;
vertical-align:middle;
padding-left: 10px;
}
#nav ul li a,
#nav ul li a.visited{
color: #ccc;
display:block;
padding: 4px;
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: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -1px;
min-width: 80px;
padding:0px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a,visited{
color: #ccc;
}
#nav ul ul li a:hover{
color: #099;
}
<div id="nav">
<div id="nav_wrapper">
<ul>
<li>
<a href="home.html">
<img src="pictures/home.png" alt="" style="width:18px;height:18px;vertical-align:middle;padding-right:11px;"/>
</a>
</li>
<li>HTML <img src="pictures/arrow.png" alt="" />
<ul>
<li>Intro</li>
<li>Basic</li>
<li>Tags</li>
<li>Links</li>
<li>Classes</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
</ul>
</li>
<li>CSS <img src="pictures/arrow.png" alt="" />
<ul>
<li>Intro</li>
<li>Basics</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
<li>Intro</li>
</ul>
</li>
</ul>
</div>
</div>

Can't get vertical submenu to display under corresponding horizontal navigation

Hi I am having trouble getting my vertical submenu to align directly under the parent horizontal menu. I only want the submenus to appear when they are hovered over. I have probably over complicated the entire CSS for myself. Any help would we very much appreciated
Here is the HTML code
<div id="nav_bar">
<ul>
<li>Home </li>
<li> About Us </li>
<li> Training
<ul>
<li> Funded Training</li>
<li> Traineeships</li>
<li> Fee for Service</li>
<li> Enterprise Specific Skill Sets</li>
<li> RPL Assessment</li>
<li> International Training</li>
</ul>
</li>
<li>
Employers
<ul>
<li> Existing Workers</li>
<li> New Workers</li>
</ul>
</li>
<li>Contact Us </li>
<li>Links</li>
</ul>
</div>
Here is the CSS
#nav_bar {
font-family: Verdana, Helvetica, Arial, sans-serif, serif;
font-size:1.2em;
font-weight:bold;
float: left;
height: 28px;
width: 689px;
margin-top: 40px;
margin-right: 20px;
margin-bottom: 0px;
margin-left: 10px;}
#nav_bar ul {
list-style-type:none;
margin:0px;
padding:0px;
overflow:hidden;}
#nav_bar li a:link, #nav_bar li a:visited {
float:left;
color: #000;
text-decoration: none;
display:block;
width: 106px;
text-align:center;
padding: 4px;
}
#nav_bar li a:hover, #nav_bar li a:active {
color: #FFF;
background-color: #184B8D;
}
#nav_bar li ul {
display: none;
position:absolute;
}
#nav_bar li ul a:link, #nav_bar li ul a:visited {
color:#000;
text-decoration:none;
display:inline-block;
width:auto;
text-align:center;
padding:4px;
}
#nav_bar li ul a:hover, #nav_bar li ul a:active {
display:block;
position: absolute;
}
#nav_bar li:hover ul {
display:block;
clear:both;
}
hey i can give example that i make refer this :)
add a class to the sub menu and apply style as follows
wish you good luck (Y)
<h2>Dropdown menu example</h2>
<nav class="cf">
<nav class="cf">
<ul class="topmenu">
<li>Home</li>
<li>Products
<ul class="submenu">
<li>Laptops</li>
<li>Tablets</li>
<li>Smartphones</li>
<li>Accessories</li>
</ul>
</li>
<li>Blog
<ul class="submenu">
<li>Recent articles</li>
<li>Archives</li>
<li>Hall of fame</li>
</ul>
</li>
<li>About</li>
<li>Contact
<ul class="submenu">
<li>Customer service</li>
<li>Register</li>
<li>Technical support</li>
</ul>
</li>
</ul>
</nav>
</nav>
CSS
/*micro-clearfix by Nicolas Gallagher http://nicolasgallagher.com/micro-clearfix-hack/*/
/* For modern browsers */
.cf:before, .cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
/* For IE 6/7 (trigger hasLayout) */
.cf {
zoom:1;
}
/*horizontal menu styles*/
nav {
background: #916A31;
height: 2.3em;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
ul {
background: #D5973C;
height: 2em;
width: 100%;
}
li {
position: relative;
}
li a {
display: block;
line-height: 2em;
padding: 0 1em;
color: white;
text-decoration: none;
}
li a:hover, .topmenu li:hover > a {
background: #916A31;
height: 2em;
padding-top: .3em;
position: relative;
top: -.3em;
border-radius: .3em .3em 0 0;
}
.current, a:hover.current, .topmenu li:hover a.current {
background: #AD9B7F;
color: #eee;
padding-top: .3em;
border-radius: .3em .3em 0 0;
position: relative;
top: -.3em;
border-bottom: .3em solid #917F63;
cursor: default;
}
/*dropdown menu styles*/
ul.submenu {
float: none;
background: #916A31;
width: auto;
height: auto;
position: absolute;
top: 2em;
left: -9000em;
max-height: 0;
-moz-transition:max-height 0.5s ease-in-out;
-webkit-transition:max-height 0.5s ease-in-out;
-o-transition:max-height 0.5s ease-in-out;
transition:max-height 0.5s ease-in-out;
overflow: hidden;
}
ul.submenu li {
float: none;
}
.topmenu li:hover ul {
left: 0;
max-height: 10em;
}
ul.submenu li a {
border-bottom: 1px solid white;
padding: .2em 1em;
white-space: nowrap;
}
ul.submenu li:last-child a {
border-bottom: none;
}
ul.submenu li a:hover {
background: #D5973C;
height: 2em;
padding-top: .2em;
top: 0;
border-radius: 0;
}

Add border-bottom: 1px solid #FF to Sub Menu on hover in CSS Horizontal Drop Down Menu

I can't seem to figure as to how to add a border-bottom: 1px solid #FFF to all sub menu items when hovered?
#menu{
padding:0;
margin:0;
position: fixed;
top: 30px;
left: 0px;
font-size: 8pt;
}
#menu ul{
padding:0;
margin:0;
}
#menu li{
position: relative;
float: left;
list-style: none;
margin: 0;
padding:0;
}
#menu li a{
width:120px;
height: 20px;
display: block;
text-decoration:none;
line-height: 20px;
background-color: #A9BBD3;
color: #FFF;
}
#menu li a:hover{
background-color: #446087;
}
#menu ul ul{
position: absolute;
top: 20px;
visibility: hidden;
}
#menu ul ul li a {
width: 115px;
padding-left: 5px;
}
#menu ul li:hover ul{
visibility:visible;
}
#menu > ul > li > a{
text-align:center;
}
<div id="menu">
<ul>
<li>File
<ul>
<li>Save</li>
<li>Link 1-2</li>
<li>Link 1-3</li>
</ul>
</li>
<li>Edit
<ul>
<li>Add</li>
<li>Delete</li>
</ul>
</li>
<li>Reports
<ul>
<li>Link 3-1</li>
<li>Link 3-2</li>
<li>Link 3-3</li>
</ul>
</li>
</ul>
</div>
Add this to your CSS and see if this helps:
​ #menu ul li > ul a:hover {
border-bottom:1px solid #fff;
} ​