I want to dsiplay menu when hover but not working.
HTML code pour afficher:
<nav>
<ul class="menu">
<li>home</li>
<li class="current">other</a></li>
<li>
services
<ul class="drop">
<li>s1</li>
<li>s2</li>
</ul>
</li>
</ul>
</nav>
CSS code to display menu and drop:
nav .menu li:hover > a {
background: #e2782e;
transition: all 0.5s;
color: white;
font-weight:bold;
}
.drop{
display:none;
}
li :hover > .drop{
display:black;
}
Please check below code for your reference.
ul.drop{display:none;}
nav ul li a:hover{background:#ccc;}
nav ul li:hover ul.drop{display:block;}
<nav>
<ul class="menu">
<li>home</li>
<li>other</li>
<li>services
<ul class="drop">
<li>s1</li>
<li>s2</li>
</ul>
</li>
</ul>
</nav>
<nav>
<ul class="menu">
<li>home</li>
<li class="current">other</a></li>
<li>
services
<ul class="drop">
<li>s1</li>
<li>s2</li>
</ul>
</li>
</ul>
</ul>
</nav>
nav .menu li:hover .drop li a {
background: #e2782e;
transition: all 0.5s;
color: white;
font-weight:bold;
}
.drop{
display:none;
}
li :hover > .drop{
display:block;
}
Please try this code.
Several issues:
unnested </a> tag for "other" menu item.
extraneous </ul> tag at the end
black is not a valid value for the display property, use list-item instead.
Edit: see Nick De Jaeger's answer above for the corrected code. Using display: block also works, but list-item is more appropriate in this case (<li>).
Bart look here this is all css when I hover on drop all auther menu item hided
nav .menu {
height: 60px;
background: #8d8989;
border-radius: 5px;
padding: 0;
margin: 0;
text-align: center; /* center the li */
}
nav .menu li {
display: inline-block;
}
nav .menu li a{
font-family: arial, sans-serif;
color: #fff;
text-decoration: none;
text-transform: uppercase;
line-height: 60px;
font-weight:bold;
vertical-align: middle;
padding: 20px;
transition: all 0.5s;
}
nav .menu li:hover > a {
background: #e2782e;
transition: all 0.5s;
color: #FFF;
font-weight: bold;
}
.drop{
display: none;
}
li:hover > .drop{
display: list-item;
background-color:red;
}
html
<nav>
<ul class="menu">
<li>home</li>
<li>contact</li>
<li>
<?php echo $lang['MENU_OUR_PRODUCTS']; ?>
<ul class="drop">
<li><?php echo $lang['MENU_History']; ?></li>
<li><?php echo $lang['MENU_CONTACT_US']; ?></li>
<li><?php echo $lang['MENU_History']; ?></li>
<li><?php echo $lang['MENU_CONTACT_US']; ?></li>
</ul>
</li>
</ul>
</ul>
</nav>
see here jsfiddle
you have some problems in your code
first in your HTML you have a closing tag </a> on the li.current but you don't have any <a> there. so that closing tag is not required
also you have and extra closing tag </ul> , you have only 2 ul to close , ul.menu and ul.drop so you need only 2 </ul> not 3.
second in your CSS , you have a space between li and :hover , you need to write them together like li:hover .
also display:black doesn't exist, i guess you wanted to write display:block
Updated code HTML :
<ul class="menu">
<li>home</li>
<li class="current">other</li>
<li>
services
<ul class="drop">
<li>s1</li>
<li>s2</li>
</ul>
</li>
</ul>
CSS :
nav .menu li:hover > a {
background: #e2782e;
transition: all 0.5s;
color: white;
font-weight:bold;
}
.drop{
display:none;
}
li:hover > .drop{
display:block;
}
let me know if it helps.
NOTE : there are so many questions about this on stackoverflow, you should be able to find your answer very quickly , also you might want to check this CSS Dropdowns
nav .menu li:hover > a {
background: #e2782e;
transition: all 0.5s;
color: #FFF;
font-weight: bold;
}
.drop{
display: none;
}
li:hover > .drop{
display: block;
}
<nav>
<ul class="menu">
<li>home</li>
<li class="current">other</li>
<li>services
<ul class="drop">
<li>s1</li>
<li>s2</li>
</ul>
</li>
</ul>
</nav>
Related
I am learning HTML5 and CSS. So my question is probably very basic and very naive. My apology for that.
To practice I am developing a header menu with drop down sub menu. I primarily hide the drop down menu by setting its display property to none, after hovering on the parent I set the display to block. But it seems like hover can't change the display value. Also it is worth mentioning that my html page is using flex box in order to have grid layout.
here is the html file:
<div class="menue">
<nav>
<ul>
<li>Home</li>
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
<li>Woman</li>
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
<li>Men</li>
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
<li>Kids</li>
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
<li>Flyers</li>
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
<li>Contact Us</li>
</ul>
</nav>
</div>
and here is the css file:
.menue {
background: #fc575e;
}
nav{
height:40px;
width: 960px;
display: block;
margin: 0,auto;
text-align: center;
text-transform: uppercase;
}
nav a{
display: block;
text-decoration: none;
font-size: 13px;
color: #112233;
}
nav ul{
list-style: none;
}
nav ul li{
float:left;
width:140px;
height:40px;
line-height: 40px;
background: #fc575e;
}
nav ul ul li{
position: relative;
display: none;
}
nav ul li:hover ul li{
display: block;
}
nav ul li:hover{
background-color: #223433;
color:#f0f1f5;
}
It seems like my hover action does not doing its job to change the display value of sub-menu to block.
I was wondering if some one could give me a hint?
It is really appreciated.
your css seems to be working fine. what you want to do is wrap both the <a> and <ul> tag inside an <li>
<li>
Home
<ul>
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
#Davi has it right, but you could also change
nav ul li:hover ul li{
display: block;
}
for
nav ul li:hover + ul li {
display: block;
}
Heres a jfiddle of it working
By the way, when you hover over a menu button, whenever the submenu wants to show up, it will displace all other elements in the original menu
Also, here is a nice tutorial on what you want to do specifically
I know #JSelser has already provided a working answer. Yet, I felt mine could be helpful to another person. I went through this tutorial http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css and adjusted to satisfy your menu.
<!DOCTYPE html>
<html>
<head>
<style>
.clearfix:after {
display:block;
clear:both;
}
/*----- Menu Outline -----*/
.menu-wrap {
width:100%;
box-shadow:0px 1px 3px rgba(0,0,0,0.2);
background:#3e3436;
}
.menu {
width:1000px;
margin:0px auto;
}
.menu li {
margin:0px;
list-style:none;
font-family:'Ek Mukta';
}
.menu a {
transition:all linear 0.15s;
color:#919191;
}
.menu li:hover > a{
text-decoration:none;
color:#ffffff;
background:#696969;
}
.menu .arrow {
font-size:11px;
line-height:0%;
}
/*----- Top Level -----*/
.menu > ul > li {
float:left;
display:inline-block;
position:relative;
font-size:19px;
}
.menu > ul > li > a {
padding:10px 40px;
display:inline-block;
text-shadow:0px 1px 0px rgba(0,0,0,0.4);
}
.menu > ul > li:hover > a{
background:#2e2728;
}
/*----- Bottom Level -----*/
.menu li:hover .sub-menu {
z-index:1;
opacity:1;
}
.sub-menu {
width:160%;
padding:5px 0px;
position:absolute;
top:100%;
left:0px;
z-index:-1;
opacity:0;
transition:opacity linear 0.15s;
box-shadow:0px 2px 3px rgba(0,0,0,0.2);
background:#2e2728;
}
.sub-menu li {
display:block;
font-size:16px;
}
.sub-menu li a {
padding:10px 30px;
display:block;
text-decoration: none;
}
.menu-title{
text-decoration: none;
background-color: #fc575e;
color: #000000;
}
a.menu-title{
color: #000000;
}
</style>
</head>
<body>
<div class="menu_wrap">
<nav class="menu">
<ul class="clearfix">
<li><a class="menu-title" href="#">Home <span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
<li><a class="menu-title" href="#">Woman <span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
<li><a class="menu-title" href="#">Men <span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
<li><a class="menu-title" href="#">Kids <span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
<li><a class="menu-title" href="#">Flyers <span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>All</li>
<li>New Arrival</li>
<li>Casual</li>
<li>Gown</li>
<li>Bridesmade</li>
</ul>
</li>
<li><a class="menu-title" href="#">Contact Us</a></li>
</ul>
</nav>
</div>
</body>
</html>
<ul id="login">
<li>User></li>
<li>Logout></li>
</ul>
I did make my menu and sub menu work ok. But the last item came from a Partial view doesnt show because this line
/* hide the second level menu */
.menu ul {
display: none;
I try putting using :not selector, but then show in a wrong format and first sub menu stop working.
.menu ul:not(#login)
On this sample, all four <LI> element should have same format and first one display the submenu.
Also try creating a different class for the second UL and ignore me.
body {
background: black;
}
.menu {
display: block;
}
.menu li {
display: inline-block;
position: relative;
z-index: 100;
}
.menu li a {
font-weight: 600;
text-decoration: none;
padding: 11px;
display: block;
color: #ffffff;
-webkit-transition: all 0.2s ease-in-out 0s;
-moz-transition: all 0.2s ease-in-out 0s;
-o-transition: all 0.2s ease-in-out 0s;
-ms-transition: all 0.2s ease-in-out 0s;
transition: all 0.2s ease-in-out 0s;
}
.menu li a:hover, .menu li:hover > a {
color: #ffffff;
background: #9CA3DA;
}
/* hide the second level menu */
.menu ul {
display: none;
margin: 0;
padding: 0;
width: 150px;
position: absolute;
top: 43px;
left: 0px;
background: #ffffff;
}
/* display second level menu on hover */
.menu li:hover > ul {
display: block;
}
.menu ul li {
display: block;
float: none;
background: black;
margin: 0;
padding: 0;
}
.menu ul li a {
font-size: 12px;
font-weight: normal;
display: block;
color: #797979;
border-left: 3px solid #ffffff;
background: #ffffff;
}
.menu ul li a:hover, .menu ul li:hover > a {
background: #f0f0f0;
border-left: 3px solid #9CA3DA;
color: #797979;
}
<nav>
<ul class="menu">
<li>
<i class="icon-home"></i>HOME
<ul class="sub-menu">
<li>Sub-Menu 1</li>
<li>Sub-Menu 2</li>
<li>Sub-Menu 3</li>
</ul>
</li>
<li><i class="icon-user"></i>ABOUT</li>
<li>
<ul id="login">
<li>User</li>
<li>Logout</li>
</ul>
</li>
</ul>
</nav>
Can you explain why you didn't put Userand Logout at the same level of Home and About like that:
<nav>
<ul class="menu">
<li>
<i class="icon-home"></i>HOME
<ul class="sub-menu">
<li>Sub-Menu 1</li>
<li>Sub-Menu 2</li>
<li>Sub-Menu 3</li>
</ul>
</li>
<li>
<i class="icon-user"></i>ABOUT
</li>
<li>User</li>
<li>Logout</li>
</ul>
</nav>
It should work !
EDIT
With the following modification of css, your menu should work.
.menu ul.sub-menu li a { ... }
.menu ul.sub-menu li a:hover, .menu ul.sub-menu li:hover > a { ... }
#login {padding: 0; text-transform: uppercase;}
codepen
Here's the code:
<nav>
<ul class="menu">
<li>
<i class="icon-home"></i>HOME
<ul class="sub-menu">
<li>Sub-Menu 1</li>
<li>Sub-Menu 2</li>
<li>Sub-Menu 3</li>
</ul>
</li>
<li><i class="icon-user"></i>ABOUT</li>
<li id="user">USER</li>
<li id="logout">LOGOUT</li>
</ul>
</nav>
That should do it!
You've placed the ABOUT link in a list item by itself.
Here's the proper HTML:
<nav>
<ul class="menu">
<li>
<i class="icon-home"></i>HOME
<ul class="sub-menu">
<li>Sub-Menu 1</li>
<li>Sub-Menu 2</li>
<li>Sub-Menu 3</li>
</ul>
</li>
<li>
<i class="icon-user"></i>ABOUT
<ul id="login">
<li>User</li>
<li>Logout</li>
</ul>
</li>
</ul>
</nav>
I know how to display submenu. I need that li a home link, would be different color when my cursor is on submenu ... Hope you will understand what I am saying.
I Thought and tried that #menu li a:hover >ul but not working.
Sample of html menu:
<li><a href=# >home</a>
<ul class="submenu">
<li><a href=# >sub</a></li>
<li><a href=# >sub</a></li>
</ul>
</li>
You need to use the parent li's :hover pseudo class.
jsFiddle
CSS:
#homeMenu:hover #homeLink {
color: blue;
}
HTML:
<ul>
<li id="homeMenu">
home
<ul class="submenu">
<li>sub
</li>
<li>sub
</li>
</ul>
</li>
</ul>
Well I think this does what you want. Can't imagine how it would be useful, though.
HTML:
<a href="#" >home</a>
<ul>
<li>sub</li>
<li>sub</li>
</ul>
CSS:
ul{
display: none;
}
a:hover + ul{
display: block;
}
Fiddle: http://jsfiddle.net/hmvw4tjc/
You were on the right way:
#menu li ul { display: none; }
#menu li:hover ul { display: block; }
#menu li > a { color: black; }
#menu li > a:hover { color: blue; }
I want to make my navigation bar with dropdown, but there is always distance on left side of the div dropdown. How do I remove that distance?
Here is the code of the navigation bar:
.navbar {
background-color: #6b6b6b;
margin:10px;
}
.navbar ul {
display: inline;
color: white;
font-family: "Agency FB";
font-weight: bold;
text-transform: uppercase;
list-style-type: none;
font-size: 24px;
}
.navbar ul li {
display: inline-block;
padding-right: 10px;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
}
.navbar ul li:hover {
background-color: #cccccc;
}
.navbar ul li ul {
display: none;
position: absolute;
margin-left:-10px;
margin-top:5px;
background-color:white;
color:#6b6b6b;
padding-left:-20px;
font-size:20px;
}
.navbar ul li ul li {
display: block;
}
.navbar ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
And here is the code of html navigation bar (without link):
<div class="navbar">
<ul>
<li>
Home
</li>
<li>
Category
<ul>
<li>Batik</li>
<li>Party</li>
<li>Office</li>
<li>Casual</li>
<li>Sport</li>
</ul>
</li>
<li>
Information
<ul>
<li>
About Us
</li>
<li>
Cara Belanja
</li>
<li>
Our Location
</li>
</ul>
</li>
<li>
Contact Us
</li>
</ul>
</div>
Thanks before :)
I remade it for you kinda, I'm sure you'll get the jist of it
<div id="nav">
<ul>
<li>Home</li>
<li>Category
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Information
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
</div>
http://jsfiddle.net/un64F/3/ for css
I'm not sure I understand what you are asking for exactly but does adding padding-left: 0; to the .navbar ul style have the effect you want?
I have got a submenu which expands from a nav menu type object when I hover over it. Right now, my main nav menu looks like so...
<div id= "navbar">
<ul>
<li><a href= "#" class= "navlink" id= "first"> First
<div class= "firstsubmenu">
<ul>
<li> <a href= "#" class="firstsubmenulink"> First sub menu option </li>
<li> <a href= "#" class="firstsubmenulink"> Second sub menu option </li>
etc...
</ul>
</div></a></li>
<li><a href= "#" class= "navlink" id="second"> Second
<div class= "secondsubmenu">
<ul>
..and so on
</ul>
</div>
Right now, my css is looking like
ul
{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li
{
float:left;
}
.navlink:link
{
display:block;
width:120px;
text-align:center;
padding:10px;
text-decoration:none;
color:#FFFFFF;
}
.navlink:hover
{
background-color:#ADD8E6;
color:#FFFFFF;
}
.navlink:visited
{
background-color:#ADD8E6;
color:#FFFFFF;
}
Before I tried making each item in the submenu a clickable link, everything showed up perfectly fine. IE: firstsubmenu showed up perfectly. It's css is
.firstsubmenu
{
display : none;
position : absolute;
left : 75px;
top : 32px ;
background-color : red;
width : 930px;
height : 25px;
z-index : 10;
}
But now that I added the links (made every list element within an block), firstsubmenu no longer appears.
The css for each link looked something like this
.firstsubmenulink
{
display:block;
width:120px;
text-align:center;
padding:10px;
text-decoration:none;
color:#FFFFFF;
}
But as I said, the submenu no longer even appears. I realize this is a bit of a long post, but any advice would be great.
You can use the below css and create pure css based menu.
Css:
body { padding: 3em; }
#navbar * { padding:0; margin: 0; font: 1em arial; }
#navbar { position: absolute; z-index: 99; margin: 0 auto; float: left; line-height: 20px; }
#navbar a { display: block; border: 1px solid #fff; background: #EFBE37; text-decoration: none; padding: 3px 10px; color:#666666; }
#navbar a:hover { background: #C6991D; }
#navbar ul li, #navbar ul li ul li { width: 120px; list-style-type:none; }
#navbar ul li { float: left; width: 120px; }
#navbar ul li ul, #navbar:hover ul li ul, #navbar:hover ul li:hover ul li ul{
display:none;
list-style-type:none;
width: 120px;
}
#navbar:hover ul, #navbar:hover ul li:hover ul, #navbar:hover ul li:hover ul li:hover ul {
display:block;
}
#navbar:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 120px;
margin-top: -20px;
}
Structure:
<div id="navbar">
<ul>
<li>Home</li>
<li>Abous Us »
<ul>
<li>About us 1</li>
<li>About us 2 »
<ul>
<li>XXX
<li>XXX
<li>XXX
</ul>
</li>
</ul>
</li>
<li>Download</li>
<li>Menus »
<ul>
<li>Menus 1</li>
<li>Menus 2 »
<ul>
<li>Menus 2-1
<li>Menus 2-2
<li>Menus 2-3
</ul>
</li>
</ul>
</li>
<li>Contact</li>
<li>Feedback</li>
</ul>
jsBin live demo
I had to fix lot of errors in your HTML. Here is the css:
#navbar ul{
list-style:none;
margin:0; padding:0;
display:table;
}
#navbar li{
top:0px;
background:#bbf;
display:inline-block;
width:100px;
}
#navbar li ul li{
display:none;
}
#navbar li:hover li{
display:block;
}
And the fixed HTML:
<div id="navbar">
<ul>
<li>
First
<ul class="firstsubmenu">
<li>1. option</li>
<li>2. option</li>
</ul>
</li>
<li>
Second
<ul class="secondsubmenu">
<li>1. option</li>
<li>2. option</li>
</ul>
</li>
</ul>
</div>
Now, after it works, do with colors whatever you want.
Use also alt tags in your links and images, it improves your SEO and compilance.