When the cursor is hovered over first level navigation (about, what we do, projects, contact) a second level navigation is shown. In this case the whole ul (from first level navigation) and the navigation field is extended (green and blue background)
I would like to extend first navigation by second after hover but the ul and navigation shouldn't expand (green and blue background shouldn't expand)
In the picture below is result what I want to accomplish using only flexbox method
I accomplished that by position absolute property but I learn flexbox method and I want to accomplish it using only flex-box method without changing HTML
/* global */
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-size: 16px;
line-height: 1.7;
font-family: "Inconsolata", monospace;
color: #341f97;
}
.container {
max-width: 1140px;
padding: 0 20px 0 20px;
}
.section {
padding: 96px 0 96px 0;
}
/* navigation */
.navigation {
background-color: #2e86de;
padding-top: 12px;
padding-bottom: 12px;
}
.navigation ul {
list-style-type: none;
background-color: green;
/* padding: 0; */
/* margin: 0; */
}
/*
// .navigation li {
// display: inline-block;
// position: relative;
// margin-right: 36px;
// }
// .navigation li ul {
// position: absolute;
// text-align: center;
// }
*/
.navigation a {
text-decoration: none;
color: #c8d6e5;
}
.navigation a:hover {
color: yellow;
}
/*
// .navigation ul ul li {
// // display: block;
// margin-bottom: 12px;
// }
// .navigation ul ul {
// display: none;
// }
// .navigation li:hover ul {
// display: block;
// background-color: green;
// // text-align: center;
// }
*/
.navigation ul {
display: flex;
background-color: green;
}
.navigation ul li {
flex-basis: 10%;
text-align: center;
/* background-color: gray; */
}
.navigation ul ul {
display: none;
flex-direction: column;
background-color: red;
}
.navigation ul li:hover ul {
display: flex;
}
<link rel="stylesheet" href="stylesheet/style.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Inconsolata&family=Roboto:wght#300;400;700&family=Saira+Condensed:wght#300;400;700&family=Saira:wght#300;400;700&family=Signika:wght#700&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" />
<nav class="navigation">
<div class="container">
<ul>
<li>
about
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
what we do
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
projects
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
contact
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
</ul>
</div>
</nav>
It expands your nav element because the second ul element is positioned statically which means when it appears on the hover event it pushes the nav down because the nav element tries to contain the child ul elements. If you use position: relative on the first menu li element then use position absolute on the 2nd, the rendering of it is taken out of the flow of the parent and it appears over the top. I've annotated the code so you can see what I've changed. There's a good explainer by Kevin Powell
/* global */
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-size: 16px;
line-height: 1.7;
font-family: "Inconsolata", monospace;
color: #341f97;
}
.container {
max-width: 1140px;
padding: 0 20px 0 20px;
}
.section {
padding: 96px 0 96px 0;
}
/* navigation */
.navigation {
background-color: #2e86de;
padding-top: 12px;
padding-bottom: 12px;
}
.navigation ul {
list-style-type: none;
background-color: green;
/* padding: 0; */
/* margin: 0; */
}
/*
// .navigation li {
// display: inline-block;
// position: relative;
// margin-right: 36px;
// }
// .navigation li ul {
// position: absolute;
// text-align: center;
// }
*/
.navigation a {
text-decoration: none;
color: #c8d6e5;
}
.navigation a:hover {
color: yellow;
}
/*
// .navigation ul ul li {
// // display: block;
// margin-bottom: 12px;
// }
// .navigation ul ul {
// display: none;
// }
// .navigation li:hover ul {
// display: block;
// background-color: green;
// // text-align: center;
// }
*/
.navigation ul {
/* added this purely for cosmetic reasons to keep your menu text on one line and put a gap between them */
white-space: nowrap;
gap: 1rem;
display: flex;
background-color: green;
}
.navigation ul li {
/* added this */
position: relative;
flex-basis: 10%;
text-align: center;
/* background-color: gray; */
}
.navigation ul ul {
display: none;
/* added this */
position: absolute;
flex-direction: column;
background-color: red;
}
.navigation ul li:hover ul {
/* added this */
width: 100%;
display: flex;
}
<link rel="stylesheet" href="stylesheet/style.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Inconsolata&family=Roboto:wght#300;400;700&family=Saira+Condensed:wght#300;400;700&family=Saira:wght#300;400;700&family=Signika:wght#700&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" />
<nav class="navigation">
<div class="container">
<ul>
<li>
about
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
what we do
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
projects
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
contact
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
</ul>
</div>
</nav>
Related
I have menu bar which need to be margin-top: 150px;
But visually in Firefox looking different as on Chrome.
Header code: https://codepen.io/bugerman21/pen/rNxvyOv
Chrome:
Correct display
Firefox:
Incorrect display
HTML:
<nav>
<ul class="nav">
<li class="category"><span>Category <i class="fas fa-sort-down"></i></span>
<ul>
<li>Qwerty 1</li>
<li>Qwerty 2</li>
<li>Qwerty 3</li>
<li>Qwerty 4</li>
</ul>
</li>
<li>Cuntact us</li>
<li>FAQ</li>
</ul>
CSS:
* {
margin: 0;
pading: 0;
}
.nav li ul {
position: absolute;
margin-top: 150px;
min-width: 150px;
list-style-type: none;
display: none;
}
How to do margin-top only for the Firefox browser?
Unsuccessful attempt:
#-moz-document url-prefix() {
.nav li ul {
margin-top: 150px;
}
}
Here ya go buddy, sorry I left for the day yesterday but see the changes made and I left outlines on the elements to give a better visual reference. As it is now it will display as expected on all browsers even old internet explorer. Although you could accomplish the same thing cleaner overall, this at least gets you back on track. Cheers and welcome to StackOverflow! :)
PS : since the nav menu items don't have a fixed height you might want to consider making that something static so you can change the top: 56px to a value that places the drop down consistently no matter the width of the screen. If you make the example full screen you'll see what I mean.
header {
display: flex;
margin: 0;
padding: 0 20px;
justify-content: space-between;
align-items: center;
background-color: silver;
}
.header {
grid-area: header;
background-color: #1f1f1f;
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
}
/*---------- Logo ----------*/
.logo {
font-family: 'Gentium Book Basic', serif;
font-size: 2.5em;
color: #808080;
}
/*---------- Nav menu ----------*/
.nav {
list-style-type: none;
display: flex;
justify-content: flex-start;
margin: 0;
}
.nav > li {
text-decoration: none;
font-family: 'Roboto', sans-serif;
color: #ffffff;
transition: background-color .25s ease;
}
.nav a {
display: block;
padding: 20px;
color: #ffffff;
font-size: 1em;
}
.category {
padding: 0 20px;
display: flex;
align-items: center;
position: relative;
overflow: visible;
border: red 1px solid;
}
/*---------- Sub menu ----------*/
.nav li ul {
position: absolute;
top: 56px;
left: 0;
min-width: 150px;
margin: 0;
padding: 0;
list-style-type: none;
display: none;
border: green 1px solid;
}
.nav li > ul li {
border-bottom: 1px solid #ffffff;
background-color: #1f1f1f;
}
.nav li > ul li a {
text-transform: none;
}
.nav li:hover > ul {
display: block;
}
.nav > li:hover {
background-color: #404040;
/* box-shadow: -5px 5px #1f1f1f; */
}
.nav li ul > li:hover {
background-color: #404040;
}
/*---------- Search & Profile----------*/
.search_and_profile {
display: flex;
align-items: center;
}
.search_and_profile > p {
margin: 0;
color: #ffffff;
}
.search-container button {
float: right;
padding: 6px 10px;
background: #e0e0e0;
font-size: 17px;
border: none;
cursor: pointer;
}
.search-container input[type=text] {
padding: 6px;
font-size: 17px;
border: none;
}
<header class="header">
<span class="logo">Qwerty</span>
<nav>
<ul class="nav">
<li class="category"><span>Category <i class="fas fa-sort-down"></i></span>
<ul>
<li><a href=#>Qwerty 1</a></li>
<li>Qwerty 2</li>
<li>Qwerty 3</li>
<li>Qwerty 4</li>
</ul>
</li>
<li>Cuntact us</li>
<li>FAQ</li>
</ul>
</nav><!-- .nav -->
<div class="search_and_profile">
<div class="search-container">
<form action="#">
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div><!-- .search-container -->
</div><!-- .search_and_profile -->
</header>
It will work for me, additionally i included color too to make sure.
Also you try this option too
#media screen and (-moz-images-in-menus:0) {
/* your style */
}
* {
margin: 0;
pading: 0;
}
.nav li ul {
position: absolute;
margin-top: 150px;
min-width: 150px;
list-style-type: none;
display: none;
}
/* Added */
#-moz-document url-prefix('') {
.nav li ul {
margin-top: 150px;
color: orange;
}
}
<div class="nav">
<ul>
<li>Home</li>
<li>About
<ul>
<li>Some text</li>
<li>Some more text</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
I need some help. I am working on this navigation bra that has a list of menu in it, and one of them is Award. Award has submenu that is hidden, and if I hover it, it submenu should be visible to the user. However, when I hover over the Award, it submenu not being visible to the user. I don't what I am doing wrong. I was hopping that someone could help me here, Please.
Here is my code
function openNav() {
document.getElementById("nav_sidebar").style.width = "142px";
document.getElementById("main").style.height = "142px";
}
function closeNav() {
document.getElementById("nav_sidebar").style.width = "0";
document.getElementById("main").style.height= "0";
}
.sidebar {
width: 0;
list-style: none;
position: absolute;
height: 100%;
z-index: 0;
left: 2px;
background: #38424f;
overflow-x: hidden;
transition: 0.5s;
padding-top: 10px;
}
.sidebar ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
.sidebar ul li:hover {
background:#f6f6f6
}
.sidebar ul {
display:none;
position: absolute;
left: 5%;
background: #fff;
padding:0
}
.sidebar ul li {
float:none;
width:200px
}
.sidebar li ul:hover
{
display: block;
}
.sidebar li a {
padding: 5px 8px 8px 15px;
text-decoration: none;
font-size: 22px;
color: #4ba6c1;
display: block;
transition: 0.3s
}
.sidebar a:hover, .offcanvas a:focus{
color: #ffffff;
}
.sidebar .close {
position: absolute;
top: 0;
right: 10px;
font-size: 20px;
margin-left: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin</title>
<meta charset="utf-8">
<script src="../src//jquery/jquery.min.js"></script>
<link href="../css/Main.css" rel="stylesheet">
<script src="../javascript/Scritp.js"></script>
</head>
<body>
<div class ="side_nave">
<label class = "nav" onclick="openNav()">☰ Menu</label>
<div class="main_panel" id = "main">
<div class="sidebar" id = "nav_sidebar">
<li><a href="javascript:void(0)" class="close" onclick=
"closeNav()">×</a></li>
<li><a class ="active" href="#">Employee</a></li>
<li>Awards
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
</ul>
</li>
<li>Certificate</li>
<li>Update</li>
<li>Sent Email</li>
<li>Settings</li>
<li>Logout</li>
</div>
</div>
</div>
</body>
</html>
You're targeting .sidebar li ul:hover to show the hidden ul. That won't work because the ul you want to hover is hidden - you can't hover the element if it's hidden. Instead, you need to target .sidebar li:hover > ul which means that when you hover a li it will show any direct child ul's
function openNav() {
document.getElementById("nav_sidebar").style.width = "142px";
document.getElementById("main").style.height = "142px";
}
function closeNav() {
document.getElementById("nav_sidebar").style.width = "0";
document.getElementById("main").style.height= "0";
}
.sidebar {
width: 0;
list-style: none;
position: absolute;
height: 100%;
z-index: 0;
left: 2px;
background: #38424f;
overflow-x: hidden;
transition: 0.5s;
padding-top: 10px;
}
.sidebar ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
.sidebar ul li:hover {
background:#f6f6f6
}
.sidebar ul {
display:none;
position: absolute;
left: 5%;
background: #fff;
padding:0
}
.sidebar ul li {
float:none;
width:200px
}
.sidebar li:hover > ul
{
display: block;
}
.sidebar li a {
padding: 5px 8px 8px 15px;
text-decoration: none;
font-size: 22px;
color: #4ba6c1;
display: block;
transition: 0.3s
}
.sidebar a:hover, .offcanvas a:focus{
color: #ffffff;
}
.sidebar .close {
position: absolute;
top: 0;
right: 10px;
font-size: 20px;
margin-left: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin</title>
<meta charset="utf-8">
<script src="../src//jquery/jquery.min.js"></script>
<link href="../css/Main.css" rel="stylesheet">
<script src="../javascript/Scritp.js"></script>
</head>
<body>
<div class ="side_nave">
<label class = "nav" onclick="openNav()">☰ Menu</label>
<div class="main_panel" id = "main">
<div class="sidebar" id = "nav_sidebar">
<li><a href="javascript:void(0)" class="close" onclick=
"closeNav()">×</a></li>
<li><a class ="active" href="#">Employee</a></li>
<li>Awards
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
</ul>
</li>
<li>Certificate</li>
<li>Update</li>
<li>Sent Email</li>
<li>Settings</li>
<li>Logout</li>
</div>
</div>
</div>
</body>
</html>
You can even solve this without JavaScript:
<li>Awards
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
</ul>
</li>
#awards ul{
display:none;
}
#awards:hover ul{
display:block;
}
I'm brand new to HTML and CSS, and right now I'm following this tutorial. My problem see picture is that the items in my drop-down list are not in a vertical line, but all cluttered together.
I'm sure it's a simple problem with a simple solution but I am not making any progress finding one...
I would be so grateful for some suggestions! :)
* {
margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
margin-bottom: 10px;
}
/* ----- NAVIGATION BAR ----- */
#nav {
width:500px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
margin-bottom: 20px;
text-align: center;
background-color: none;
}
#nav ul {
padding: 0;
list-style: none;
position: relative;
display: inline-table;
}
#nav ul:after{
content: "";
clear: both;
display: block;
}
#nav ul li {
float: left;
}
#nav ul li a {
display: block;
padding: 10px 20px;
color: black;
text-decoration: none;
font-family: "Trocchi", serif;
}
#nav ul li:hover {
background: #76C5CF;
}
#nav ul li:hover a {
color: white;
}
#nav ul ul {
display: none;
background: none;
position: absolute;
top: 100%;
}
#nav ul li:hover > ul {
display: block;
}
/* ----- HEADER ----- */
#header {
width: 340px
margin: auto;
margin-top: 10px;
text-align: center;
}
#header h1 {
height: 50px;
font-family: "Trocchi", serif;
background-color:
}
#header h2 {
font-family: "Trocchi", serif;
position: relative;
display: inline;
}
/* ----- BACKGROUND ----- */
body {
background-color: #A6D7F3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Harley's Haunts </title>
<link rel="stylesheet" type="text/css" href="harleysHaunts.css">
<style>
#import url('https://fonts.googleapis.com/css?family=Trocchi');
</style>
</head>
<body>
<div id="header">
<h1> HARLEY'S HAUNTS </h1>
<h2> It's a dog's world after all </h2>
</div>
<div id="nav">
<ul>
<li> About </li>
<li> Pet-Friendly Venues
<ul>
<li> Cafes </li>
<li> Restaurants </li>
<li> Pubs/Bars </li>
<li> Shops </li>
</ul>
</li>
<li> Contact </li>
</ul>
</div>
</body>
</html>
All you have to do is add position:relative to #nav ul li :
#nav ul li {
float: left;
position: relative;
}
but you should use this only for the li parent and you shouldn't use float:left in the li of the ul child.
So your css will become like this
#nav > ul > li {
float: left;
position: relative;
}
Add a specific width to #nav ul ul, gave it 200px as example considering parent ul width
* {
margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
margin-bottom: 10px;
}
/* ----- NAVIGATION BAR ----- */
#nav {
width:500px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
margin-bottom: 20px;
text-align: center;
background-color: none;
}
#nav ul {
padding: 0;
list-style: none;
position: relative;
display: inline-table;
}
#nav ul:after{
content: "";
clear: both;
display: block;
}
#nav ul li {
float: left;
}
#nav ul li a {
display: block;
padding: 10px 20px;
color: black;
text-decoration: none;
font-family: "Trocchi", serif;
}
#nav ul li:hover {
background: #76C5CF;
}
#nav ul li:hover a {
color: white;
}
#nav ul ul {
display: none;
background: none;
position: absolute;
top: 100%;
width: 200px;
}
#nav ul li:hover > ul {
display: block;
}
/* ----- HEADER ----- */
#header {
width: 340px
margin: auto;
margin-top: 10px;
text-align: center;
}
#header h1 {
height: 50px;
font-family: "Trocchi", serif;
background-color:
}
#header h2 {
font-family: "Trocchi", serif;
position: relative;
display: inline;
}
/* ----- BACKGROUND ----- */
body {
background-color: #A6D7F3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Harley's Haunts </title>
<link rel="stylesheet" type="text/css" href="harleysHaunts.css">
<style>
#import url('https://fonts.googleapis.com/css?family=Trocchi');
</style>
</head>
<body>
<div id="header">
<h1> HARLEY'S HAUNTS </h1>
<h2> It's a dog's world after all </h2>
</div>
<div id="nav">
<ul>
<li> About </li>
<li> Pet-Friendly Venues
<ul>
<li> Cafes </li>
<li> Restaurants </li>
<li> Pubs/Bars </li>
<li> Shops </li>
</ul>
</li>
<li> Contact </li>
</ul>
</div>
</body>
</html>
You have applied rules CSS for all children of the ul parent, so all chidren of ul ul they will be inherited the rule of parent CSS.
Try to use selector reference '>' when you want to target only parent element
For this exemple you just change
#nav ul li {
float: left;
}
by
#nav > ul >li {
float: left;
}
Bit new at CSS and been looking around at various sites and bits of code trying to get a centered drop down menu which I managed to get :).
However as soon as I added some images to make up the heading the menu shifted off to the left and I have not been able to budge it ever since, any help? Code is below.
<style type="text/css">
<!--
body {
background-image: url();
background-color: #0099FF;
}
.style1 {color: #FFCC00}
.style2 {color: #FF9900}
.style3 {
color: #FFCC00;
font-weight: bold;
font-size: 12px;
}
.style4 {
color: #000099;
font-weight: bold;
}
.style5 {color: #000099; font-weight: bold; font-size: 12px; }
.style6 {color: #000099}
* { padding: 0; margin: 0; }
body { padding: 5px; }
ul { list-style: none; margin: auto}
ul li { float: left; padding-right: 0px; position: relative; }
ul a { display: table-cell; vertical-align: middle; width: 100px; height: 50px; text-align: center; background-color: #0099EE; color: #000000; text-decoration: none; border: 1px solid #000000;}
ul a:hover { background-color: #0066FF; }
li > ul { display: none; position: absolute; left: 0; top: 100%; }
li:hover > ul { display: inline; }
li > ul li { padding: 0; padding-top: 0px; }
#menu-outer {
height: 84px;
background: url(images/bar-bg.jpg) repeat-x;
}
.table {
display: table;
margin: 0 auto;
}
ul#horizontal-list {
min-width: 696px;
list-style: none;
padding-top: 20px;
}
ul#horizontal-list li {
display:inline
}
-->
</style></head>
<body>
<div id="menu-outer"></div>
<div class="table"></div>
<div align="center"><img src="logo_with_words_3.jpg" width="172" height="145"><img src="heading.gif" width="557" height="69"><img src="logo_with_words_3.jpg" width="172" height="145">
</div>
<ul id="horizontal-list">
<li>
Home
</li>
<li>About Us
<ul>
<li>History</li>
<li>Guest Comments</li>
</ul>
<li>News</li>
<li>Accommodation
<ul>
<li>Rooms</li>
<li>Bedrooms</li>
<li>St Joseph's Annexe</li>
</ul>
<li>Visiting St Katharine's
<ul>
<li>Retreats</li>
<li>B&B</li>
<li>Events</li>
<li>Conferences</li>
<li>Catering</li>
</ul>
<li>Contact Us
<ul>
<li>Find Us</li>
</ul>
<li>Walled Garden</li>
<li>Sue Ryder Legacy
<ul>
<li>Sue Ryder</li>
<li>Prayer Fellowship</li>
<li>LRWMT</li>
</ul>
</ul>
</body>
You aren't really using a table, but you can add something like this: #horizontal-list {margin: auto; width: 850px;}
JS Fiddle with your code
I have an existing website with the menu code below and I want to add more stuff to my site but users need to nevigate to those pages please help me to add a sub menu to the code I have. Please help?
HTML
<div id="menu">
<ul>
<li>Home</li>
<li>Services</li>
<li>FAQ</li>
<li class="active">About</li>
<li>Contact Us</li>
</ul>
</div>
CSS
/** HEADER */
#header-wrapper
{
overflow: none;
height: 100px;
margin-bottom: 20px;
background: rgba(0,0,0,0.70);
}
#header {
overflow: none;
}
/** LOGO */
#logo {
float: left;
width: 10px;
height: 100px;
}
#logo h1, #logo p {
margin: 0px;
line-height: normal;
}
#logo h1 a {
padding-left: 1px;
text-decoration: none;
font-size: 1.50em;
font-weight: 600;
font-family: 'Archivo Narrow', sans-serif;
color: #FFFFFF
}
/** MENU */
#menu {
float: right;
height: 110px;
}
#menu ul {
margin: 0px;
padding: 0px;
list-style: none;
line-height: normal;
}
#menu li {
float: left;
margin-right: 1px;
}
#menu a {
display: block;
height: 100px;
padding: 0px 30px;
line-height: 100px;
text-decoration: none;
text-transform: uppercase;
color: #FFFFFF;
}
#menu a:hover {
text-decoration: none;
background: rgba(0,0,0,0.70);
}
#menu .active
{
background: rgba(0,0,0,0.70);
}
Working menu:
a quick fiddle for you
//HTML
<div id="menu">
<ul>
<li>Home
</li>
<li>Services
<ul>
<li>Something You do
</li>
<li>TODO
</li>
</ul>
</li>
<li>FAQ
</li>
<li class="active">About
</li>
<li>Contact Us
</li>
</ul>
</div>
//CSS
/** MENU */
#menu {
position:relative;
height: 110px;
}
#menu ul {
margin: 0px;
padding: 0px;
float:left;
line-height: 110px;
}
#menu li {
list-style:none;
}
#menu>ul>li {
float: left;
margin-right: 1px;
position:relative;
}
#menu>ul>li ul {
height:100%;
position:absolute;
bottom:-100%
}
#menu>ul>li ul>li {
bottom:0px;
display:none;
width:15em;
float:left;
}
#menu>ul>li:hover ul>li {
display:block
}
#menu a {
display:block;
padding: 0px 30px;
text-decoration: none;
text-transform: uppercase;
color: #FFFFFF;
background:rgba(200, 200, 200, 0.5);
}
#menu a:hover {
text-decoration: none;
cursor:pointer;
background:rgba(200, 200, 200, 1);
}
#menu .active {
}
Better to use jQuery plugin like Superfish
http://users.tpg.com.au/j_birch/plugins/superfish/
//link to the CSS files for this menu type
<link rel="stylesheet" media="screen" href="superfish.css">
// link to the JavaScript files (hoverIntent is optional)
// if you use hoverIntent, use the updated r7 version (see FAQ)
<script src="hoverIntent.js"></script>
<script src="superfish.js"></script>
// initialise Superfish
<script>
jQuery(document).ready(function(){
jQuery('#menu ul').superfish();
});
</script>
Well, first add your submenu in the markup, for example:
<li>Services
<ul>
<li> sub menu item 1 </li>
<li> sub menu item 2 </li>
</ul>
</li>
....
Position your list items:
#menu li{
position: relative;
}
Style your sub menu:
#menu ul ul{
position:absolute;
left: 0;
top: 100px; /* height of the parent list item */
display: none; /* hide it */
}
#menu li:hover > ul{ /* show it when mouse is over the parent list item */
display:block;
}
Try http://jsfiddle.net/9LcfX/