i'm learning the basic of CSS and trying to create a dropdown menu, i tried creating a dropdown menu using plain CSS, but it's not working.
So far I tried this code:
CSS
<!-- because of the * default code it takes out all margin and padding or idententation -->
*{
margin: 0px;
padding: 0px;}
body
{
font-family: verdana;
background-color: ABC;
padding: 50px;
}
h1
{
text-align: center;
border-bottom: 2px solid #009;
margin-bottom: 50px;
}
/*rules for navigation menu */
/*============================================*/
ul#navmenu, ul.sub1
{
list-style-type: none;<!-- sets bullets to none -->
}
ul#navmenu li
{
outline: 1px solid red;
width: 125px;
text- align: center;
position: relative;
float: left;
margin-right: 4px;
}
ul#navmenu a
{
text-decoration: none;
display: block; <!-- this code makes the link a button instead pointing specifically on the link -->
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
}
ul#navmenu .sub1 li
{
}
ul#navmenu .sub1 a
{
margin-top: 0px;
}
ul#navmenu li:hover > a
{
background-color: #CFC; <!-- sets all link color when hovering to yellow -->
}
ul#navmenu li:hover a: hover
{
background-color: #FF0; <!-- sets all link color when hovering to yellow -->
}
ul#navmenu ul.sub1
{
display: none;
position: absolute;
top: 26px;
left: 0px;
}
ul#navmenu li:hover .sub1
{
display: block;
}
HTML
<h1>Navigation Menu</h1>
<ul id="navmenu">
<li><a href="#">Hyperlink 1</a></li>
<li><a href="#">Hyperlink 2</a></li>
<ul id="sub1">
<li><a href="#">Hyperlink 2.1</a></li>
<li><a href="#">Hyperlink 2.2</a></li>
</ul>
<li><a href="#">Hyperlink 3</a></li>
<li><a href="#">Hyperlink 4</a></li>
</ul>
</body>
</html>
The dropdown menu is not working, it's not hiding the sub menus, i don't know why.
Here is the picture screenshot using Internet Explorer:
While using Google Chrome:
I can't move on:( Any suggestion why dropdown menu is not working and why it's showing differently using other browsers? Is there a way on how to code CSS dropdown menu where it will show the same on any browser? Thanks in advance.
You need to do three things..
1.Correction in HTML, Where there is 'li' tag with child 'ul'with id="sub1".You need to write it as
// there was a typo mistake.You close 'li' before the 'ul'. It should be closed in the end.
You need to display:none for sub1 menu.
li ul{ display:none; }
Then show it when you hover that 'li'
li:hover ul{ display:block; }
EDITED :Write this in style tag....
TRY IT
Your html is wrong use,
ul#sub1
should be child of<li>
JSFIDDLE
screen capture:
Use correct HTML buddy:
And, add this CSS:
Like this
please put ul in
DEMO
HTML
CSS
DEMO2
DEMO3