Reputation: 107
I am creating a navigation menu that is vertical with a horizontal submenu.
The Html:
<div id="sidenav">
<ul class="menu">
<li class="option">
<span class="label">Home</span>
</li>
<li class="option">
<span class="label">About</span>
</li>
<li class="option">
<span class="label">More...</span>
<div class="submenu">
<ul class="menu">
<li class="option">
<span class="label">First</span>
</li>
<li class="option">
<span class="label">Second</span>
</li>
<li class="option">
<span class="label">Thrid</span>
</li>
</ul>
</div>
</li>
</ul>
</div>
The Css:
#sidenav {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100px;
color: #fff;
background-color: #000;
}
.menu {
list-style-type: none;
padding: 0;
margin: 0;
}
.option {
width: 100px;
height: 50px;
text-align: center;
}
#sidenav > .menu > .option {
position: relative;
}
#sidenav > .menu > .option > .submenu {
position: absolute;
top: 0;
left: 100px;
color: #fff;
background-color: #ddd;
}
#sidenav > .menu > .option > .submenu > .menu > .option {
float: left;
}
Both menus are absolutely positioned and the submenu needs to be to the right of the parent menu option.
I added a float left to each menu option in the submenu but they stay vertical and won't go horizontal. How do I get the submenu to be horizontal?
You can see the result in this fiddle:
Upvotes: 2
Views: 1795
Reputation: 1410
"Very simple" - Awesome is the better adjective.
I've enjoyed this Q&A, +1 & thanks to both.
list-style-type: none;
led me to see if divs would give the same result. They do.
<html>
<head>
<style>
#sidenav
{ position:absolute; top:0; left:0; bottom:0; width:100px;
background:#ccc; outline:1px dashed black; }
/* - - - - using lists - - - - */
ul
{ list-style-type:none; padding:0; margin:0; }
ul
{ white-space:nowrap; }
li
{ width:100px; height:50px; line-height:50px; text-align:center; }
.ulmenu > li
{ position:relative; background:#fdd; }
.subulmenu
{ position:absolute; top:0; left:100px; background:#dfd; }
.subulmenu > li
{ display:inline-block; background:#ddf; }
/* - - - - using divs - - - - */
.divmenu
{ white-space:nowrap; }
.divmenu > div, .subdivmenu > div
{ width:100px; height:50px; line-height:50px; text-align:center; }
.divmenu > div
{ position:relative; background:#fdd; }
.subdivmenu
{ position:absolute; top:0; left:100px; background:#dfd; }
.subdivmenu > div
{ display:inline-block; background:#ddf; }
</style>
</head>
<body>
<div id="sidenav">
<ul class="ulmenu"><!-- using lists -->
<li>Home</li>
<li>About</li>
<li>More...
<ul class="subulmenu">
<li>First</li>
<li>Second</li>
<li>Thrid</li>
</ul>
</li>
</ul>
<div class="divmenu"><!-- using divs -->
<div>Home2</div>
<div>About2</div>
<div>More2...
<div class="subdivmenu">
<div>First2</div>
<div>Second2</div>
<div>Thrid2</div>
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 0
Reputation: 30999
Demo http://jsfiddle.net/vS9dY/7/
Explanation: Very simple,
#sidenav > .menu > .option > .submenu > .menu > .option
,
remove float:left;
and apply display:inline-block;
to it. .menu
apply white-space:nowrap;
to prevent the inline-block
.option
s from wrapping to the next line.line-height:50px;
to .option
s to vertically center the text.So:
.menu {
list-style-type: none;
padding: 0;
margin: 0;
white-space:nowrap;
}
.option {
width: 100px;
height: 50px;
line-height:50px;
text-align: center;
}
#sidenav > .menu > .option > .submenu > .menu > .option {
display:inline-block;
}
Upvotes: 3