I am trying to implement a menubar with submenus using CSS.
Demo http://jsfiddle.net/kgu/skg3ctu5/
HTML
<div class="menu">
<ul id="navmenu">
<li> Item1 </li>
<li> Item2 </li>
<li>
Item3
<ul class="sub1">
<li> Item3.1 </li>
<li>
Item3.2
<ul class="sub2">
<li> Item3.2.1 </li>
<li> Item3.2.2 </li>
<li> Item3.2.3 </li>
</ul>
</li>
<li> Item3.3 </li>
</ul>
</li>
<li> Item4 </li>
</ul>
</div>
CSS
* {
margin:0px;
padding:0px;
}
body {
padding:50px;
font-family:verdana;
background-color:#000000;
}
#navmenu li {
list-style-type:none;
}
#navmenu li {
outline:1px solid red;
width:125px;
text-align:center;
position:relative;
float:left;
}
.sub1 {
position:absolute;
top:27px;
left:0px;
}
.sub2 {
position:absolute;
left:126px;
top:0px;
}
#navmenu a {
display:block;
height:25px;
background-color:#ffffff;
text-decoration:none;
}
I want my (submenu 1) .sub1 to appear horizontally below the nav menu. When hovering over the item 3 the submenu item 3.1 should appear below item 1, item 3.2 should appear below item 2, and item 3.3 should appear below item 3.
Could anyone help? thanks!
Here is a complete CSS solution. Change part of your css to this:
*{
margin:0px;
padding:0px;
}
body{
padding:50px;
font-family:verdana;
background-color:#000000;
}
#navmenu li{
list-style-type:none;
}
#navmenu li{
outline:1px solid red;
width:125px;
text-align:center;
position:relative;
float:left;
}
.sub1{
position:absolute;
top:27px;
left:0px;
display:none;
}
.sub1:hover, .sub1:focus {
display:block;
}
.sub2:hover, .sub2:focus {
display:block;
}
.sub2{
position:absolute;
left:126px;
top:0px;
display:none;
}
#navmenu a{
display:block;
height:25px;
background-color:#ffffff;
text-decoration:none;
}
#navmenu a:hover + .sub1{
display:block;
}
#navmenu a:hover + .sub2{
display:block;
}
I gave your sub menu classes display:none. Then if you hover or focus them they will display, this work with the bottom 2 new selectors styles I made which check if an a tag is next to .sub1 or sub2 on hover display block them. All these components together allow for the drop down to work. You can animate with css animations on hover and focus if you want. Note the new selector I used " + " this allows you to select siblings (elements next to each other).
Adding more you can also try using this instead of the css above just add it to the bottom of your css
ul#navmenu li:hover > ul {
display:block;
}
Related
I don't know how to do this without position absolute (the sub-ul must stay nested).
http://codepen.io/elkeschiffer/pen/WrjEwe
May someone can help me please?
Is there a way?
* {
list-style:none;
margin:0;
padding:0;
}
ul {
display:block;
width:100%;
text-align:center;
position:relative;
}
ul ul {
position:absolute;
top:100%;
left:0;
background-color:#333;
color:#fff;
}
li {
padding:5px;
display:inline-block;
}
a {
color:inherit;
text-decoration:none;
}
<ul>
<li>Fair</li>
<li>Energy
<ul>
<li>Beratung & Bau</li>
<li>Photovoltaik</li>
</ul>
</li>
<li>Partner</li>
<li>Wissen</li>
</ul>
My final goal is to create what you see in image B. Note: the menu bar must be centered on the page. I did create B by setting the vertical-align on the image to middle. However, as a result of doing this my dropdown menu is slightly separated from the main header. Therefore, i cannot select the sub-menu items when i move my mouse cursor down. Any ideas on making this work ? Thanks Jillian
<style>
#nav{
border:1px solid #ccc;
border-width:1px 0;
list-style:none;
margin:0;
padding:0;
text-align:center;
}
#nav li{
position:relative;
display:inline;
}
#nav a{
display:inline-block;
padding:10px;
}
#nav ul{
position:absolute;
/*top:100%; Uncommenting this makes the dropdowns work in IE7 but looks a little worse in all other browsers. Your call. */
left:-9999px;
margin:0;
padding:0;
text-align:left;
}
#nav ul li{
display:block;
}
#nav li:hover ul{
left:0;
}
#nav li:hover a{
text-decoration:underline;
background:#f1f1f1;
}
#nav li:hover ul a{
text-decoration:none;
background:none;
}
#nav li:hover ul a:hover{
text-decoration:underline;
background:#f1f1f1;
}
#nav ul a{
white-space:nowrap;
display:block;
border-bottom:1px solid #ccc;
}
a{
color:#c00;
text-decoration:none;
font-weight:bold;
}
a:hover{
text-decoration:underline;
background:#f1f1f1;
}
</style>
</head>
<body>
<ul id="nav">
<li>Item one</li>
<li>Item two
<ul>
<li>Sub1</li>
<li>Sub2</li>
</ul>
</li>
<li class="double-line">
<img style="vertical-align:middle" src="img/logo_large.png" alt="logo" /></li>
<li>The Fourth</li>
<li>Last</li>
</ul>
</body>
</html>
You do something like,
#nav ul{
background:url('img/logo_large.png') no-repeat center center;
/* more CSS here */
}
unless you have to use it as a link. Then consider position:absolute; for the image with #nav ul being position:relative;, and use a floating layout for the other links with a z-index to overlap where they should hang over.
You can just offset the submenu up to cover the logo height.
Here is a JSfiddle using the google logo and altering the submenu style by adding this:
#nav ul {
top: 20px;
}
Try to insert in CSS line-height: X px; (for example, parent div height) in each menu title (Item one, Item two, The Fourth, etc.)
I'm trying to create a mega drop down menu just like the one from the following link : http://i48.tinypic.com/2ln97ip.png
I have created this fiddle but the things aren't going well because of positioning and border of second ul is not ok :(
http://jsfiddle.net/H8FVE/16/
<ul id="firstUl">
<li><a>mainSimple</a></li>
<li>
<a>MainMenu</a>
<ul id="secondUl">
<li>
<a>SecondLevel1</a>
<ul id="lastLevel">
<li>
<a>LastLevelX</a>
</li>
<li>
<a>LastLevelY</a>
</li>
</ul>
</li>
<li>
<a>SecondLevel2</a>
<ul id="lastLevel">
<li>
<a>LastLevel</a>
</li>
<li>
<a>LastLevel</a>
</li>
<li>
<a>LastLevel</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
#firstUl{height:42px; position:relative; background:#ccc;}
#firstUl li{ float:left; border:1px solid #fff; padding:10px; border:1px solid red;}
#secondUl{display:none; background:#fafafa; }
#secondUl li{
float:left;
display:block;
border:1px solid #eaeaea;
}
#lastLevel{
display:none;
}
#firstUl li:hover> #secondUl{
border:1px solid red;
display:block;
position:absolute;
top:100%;
}
#firstUl li #secondUl li:hover> #lastLevel{
display:block;
position:absolute;
top:100%;
left:0;
}
#firstUl li #secondUl li #lastLevel li{
float:none;
}
Can someone help me with this pls.
Try setting the #secondUl li to position:relative so that the child's (UL) position depends on it:
#secondUl li {
...
position: relative;
}
I'am trying to create a navigation menu with a sub menu, and fiddled with it today.
But i'am stuck at getting the sub menu of the parent menu to align it's links.
my HTML
<!-- navigation menu -->
<div class="MenuContainer">
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>
Projects
<ul class="sub">
<li>Project1</li>
<li>Project2</li>
<li>Project3</li>
</ul>
</li>
</ul>
</div>
my CSS
.MenuContainer {
width:100%;
height:50px;
border:1px solid;
position:relative;
}
ul {
margin:0;
padding:0;
}
/*Main menu*/
li.menu {
height:50px;
float:left;
}
ul.menu li {
list-style:none;
float:left;
height:49px;
text-align:center;
}
ul.menu li a {
display:block;
padding:0 20px;
text-align:center;
font-size:17px;
line-height:49px;
text-decoration:none;
color:#5d5d5d;
}
ul.menu li:hover > a {
color:#fdfdfd;
}
ul.menu li:hover > ul {
display:block;
}
/*sub menu*/
li.sub {
height:40px;
float:left;
}
ul.sub li {
list-style:none;
float:left;
height:39px;
text-align:center;
}
ul.sub li a {
display:block;
padding:0 20px;
text-align:center;
font-size:17px;
line-height:39px;
text-decoration:none;
color:#5d5d5d;
}
If anyone can tell me where i went wrong please do. First time trying to create one from scratch.
Also if anyone know a good HTML5 / CSS3 forum / forums please don't hesitate to post a link. I have tried to find some but all are not serious or no active users.
Also this is my first post at stackoverflow so if i do a beginners mistake here, please just point it out.
Thanks on advance.
Hey i think you want this do the sun class position relative and sub ul give position absolute
.sub{
position:absolute;
}
ul.menu li {
position:relative;
}
Live demo http://jsfiddle.net/HVk4G/
Vertical menu Updated demo http://jsfiddle.net/HVk4G/1/
I'm using CSS for creating a dropdown menu, but I don't know what's going wrong with it. It's not dropping the sub-menu (un-ordered list in my code)
when hover is fired. I'm badly stuck here, please help me out.
I also tried the visibility property instead of display. I could see only
menu1, menu2, menu3 in browser horizontally and nothing else.
I'm using IE7 on XP SP3.
CSS:
#navMenu ul{
argin:0;
padding:0;
}
#navMenu li {
margin:px;
padding:0;
position:relative;
float:left;
display:block;
list-style:none;
}
#navMenu li a{
text-align:center;
text-decoration:none;
width:100;
display:block;
}
#navMenu ul ul{
display:none;
}
#navMenu ul li : hover ul {
width:auto;
position:absolute;
background:#453DD;
display:block;
}
HTML:
<div id="wrapper" >
<div id="navMenu">
<ul>
<li>menu1
<ul>
<li>menuitem11</li>
<li>menuitem12</li>
<li>menuitem13</li>
<li>menuitem14</li>
</ul>
</li>
<li>menu2
<ul>
<li>menuitem11</li>
<li>menuitem12</li>
<li>menuitem13</li>
<li>menuitem14</li>
</ul>
</li>
<li>menu3
<ul>
<li>menuitem11</li>
<li>menuitem12</li>
<li>menuitem13</li>
<li>menuitem14</li>
</ul>
</ul>
</div>
</div>
JSFiddle
There mustn't any space between the tag name and pseudo class like you must use li:hover instead of li : hover.
Your style has become messed up. It's missing units and/or values. This seems to work. You can see it here.
#navMenu ul{
margin:0;
padding:0;
}
#navMenu li {
margin:0px;
padding:0;
position:relative;
float:left;
display:block;
list-style:none;
}
#navMenu li a{
text-align:center;
text-decoration:none;
width:100px;
display:block;
}
#navMenu ul ul{
display:none;
}
#navMenu ul li:hover ul {
width:auto;
position:absolute;
background:#453DD;
display:block;
}