I guess the answer is simple, but still, I cannot figure out how to make my second level menu viewed as an inline-block - from this:
to this:
So what I need is an inline block-listing + text aligned to the center.
Thank you very much for your help.
HTML:
<ul id="menu">
<li>Home</li>
<li>About Us
<ul>
<li>The Team</li>
<li>History</li>
<li>Vision</li>
</ul>
</li>
<li>Products
<ul>
<li>Cozy Couch</li>
<li>Great Table</li>
<li>Small Chair</li>
<li>Shiny Shelf</li>
<li>Invisible Nothing</li>
</ul>
</li>
<li>Contact
<ul>
<li>Online</li>
<li>Right Here</li>
<li>Somewhere Else</li>
</ul>
</li>
CSS:
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #1e7c9a;
margin-left:1px;
white-space: nowrap;
}
ul li a:hover {
background: #BBBBBB;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a {
background: #3b3b3b;
}
li:hover li a:hover {
background: #CCC;
}
add this to your css
ul li {
display: block;
float: left;
}
li:hover ul {
display: block;
position: absolute;
left:0px;
}
li:hover li {
float: left;
font-size: 11px;
}
Demo
Related
I don't know how to change the code to handle arbitrary nested items? I have no clue how to this to apply for multiple levels. The following works just for 2 levels. But if i add a 3. level it fails. I think this can be done cleverly without writing manually each level?
I want that it can be adjusted to arbitrary many childs and it should be work with hover and clicks.
Thank you very much.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover ul {
display:block;
}
.nav ul li ul li {
display: block;
float:left; /* newly added */
height:auto; /* newly added */
line-height:34px; /* newly added */
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Is this something you wanted? I just added a > in .nav li:hover ul.
See comments in code below. ZZZZ, XXXX, and YYYY are added by me.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
padding: 0.5rem;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
/*.nav li:hover ul { OLD CODE
display:block;
}*/
.nav li:hover > ul { /* ADDED the charcter '>' */
display:block;
}
.nav ul li ul li {
display: block;
float:left;
height:auto;
line-height:34px;
}
<div class="nav">
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>ZZZZ
<ul> <!-- ADDED -->
<li>XXXX</li>
<li>YYYY</li>
</ul>
</ul>
</li>
</ul>
</li>
</ul>
</div>
You can use flex-inline and > for this approach of making menus.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover > ul {
display: inline-flex !important;
}
.nav ul li ul li {
/*display: block;*/
float:left;
height:auto;
line-height:34px;
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
I think what you're looking for is a multi-level dropdown. This is easier to do with separate classes rather than all one class (nav). I rewrote it for the sake of organization, but the design tweaks are really up to you! When running this snippet, make sure to use full screen because StackOverflow's snippet will distort the display a little bit.
.menu1 li {
list-style: none;
text-align: center;
position: relative;
float: left;
height: 30px;
width: 150px;
background-color: white;
}
.menu1 li:hover {
background-color: #5c89c7;
}
.menu1 li:hover>ul {
display: inline;
}
.menu1 a {
border-bottom: none;
font-family: Roboto, sans-serif;
color: black;
text-decoration: none;
padding: 0 0 0 10px;
display: block;
line-height: 30px;
}
.menu1 a:hover {
color: white;
}
.menu2 {
position: absolute;
top: 30px;
left: 0;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu2>li {
position: relative;
height: 30px;
background: #999999;
}
.menu2>li:hover {
background: #CCCCCC;
}
.menu3 {
position: absolute;
top: 0;
right: -150px;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu3>li {
height: 30px;
background: #999999;
}
.menu3>li:hover {
background: #CCCCCC;
}
<ul class="menu1">
<li>HOME</li>
<li>ABOUT</li>
<li>
SERVICES
<ul class="menu2">
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>
CONTACT US
<ul class="menu2">
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul class="menu3">
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
I have 1 side menu in my main page of website. And some of the list have have sub menu. And on hover i want to display it. Firstly i am display it none and then on display it on hover but it is not displaying.
#menu {
width: 15%;
height: 100%;
background: #424242;
color: white;
float: left;
}
#menu ul {
margin: 0;
padding: 0;
}
#menu li {
list-style: none;
font-size: 20px;
padding: 15px 20px;
cursor: pointer;
}
#menu li:hover {
background-color: #90CAF9;
}
#menu li.active {
background-color: #2196F3;
}
#menu li ul {
display: none;
}
#menu li:hover ul {
display: block;
position: absolute;
left:100%
}
<div id="menu">
<ul>
<li onClick="Dashboard();">Dashboard</li>
<li>Employee >
<ul>
<li>Add Employee</li>
<li>Employee View</li>
</ul>
</li>
<li>Leave</li>
<li>Salary</li>
<li>Change Password</li>
</ul>
</div>
#menu {
width: 150px;
height: 100%;
background: #424242;
color: white;
float: left;
}
#menu ul {
margin: 0;
padding: 0;
}
#menu li {
list-style: none;
font-size: 20px;
padding: 15px 20px;
cursor: pointer;
}
#menu li:hover {
background-color: #90CAF9;
}
#menu li.active {
background-color: #2196F3;
}
#menu ul li ul {
display: none;
}
#menu ul li.submenu {
position: relative
}
#menu ul li.submenu ul {
position: absolute;
left: 150px;
width: 200px;
top: 0;
background: #333
}
#menu ul li.submenu:hover ul {
display: inline-block
}
<div id="menu">
<ul>
<li onClick="Dashboard();">Dashboard</li>
<li class="submenu">Employee >
<ul>
<li>Add Employee</li>
<li>Employee View</li>
</ul>
</li>
<li>Leave</li>
<li class="submenu">Salary
<ul>
<li>Add Employee</li>
<li>Employee View</li>
</ul>
</li>
<li>Change Password</li>
</ul>
</div>
Also Add .submenu class to submenu dropdown parent li.
CSS drop-down menu not showing sub-menu on hover, but find out the actual problem. Below are the HTML and CSS. Please see fiddle.
It looks display: block; has no effect at all.
body {
background-color: #eee;
padding: 0;
}
.inline-menu,
.inline-menu ul {
list-style: none;
padding: 0;
margin: 0;
}
.inline-menu > li {
display: inline-block;
padding-right: 25px;
}
.inline-menu a {
text-decoration: none;
}
.inline-menu > li > ul {
display: none;
}
.inline-menu > li > ul:hover {
display: block;
}
<nav>
<span class="logo"></span>
<ul class="inline-menu left-menu">
<li>L-A
<ul>
<li>1
</li>
<li>2
</li>
</ul>
</li>
<li>L-B
<ul>
<li>1
</li>
<li>2
</li>
</ul>
</li>
</ul>
</nav>
You are applying hover to the wrong element- use this:
.inline-menu > li:hover > ul{
display: block;
}
instead of .inline-menu > li > ul:hover
body{
background-color: #eee;
padding: 0;
}
.inline-menu,
.inline-menu ul{
list-style: none;
padding: 0;
margin: 0;
}
.inline-menu > li{
display: inline-block;
padding-right: 25px;
}
.inline-menu a{
text-decoration: none;
}
.inline-menu > li > ul{
display: none;
}
.inline-menu > li:hover > ul{
display: block;
}
<nav>
<span class="logo"></span>
<ul class="inline-menu left-menu">
<li>L-A
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>L-B
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
</ul>
</nav>
* {
margin: 0px;
padding: 0px;
}
#menu ul {
list-style: none;
}
#menu ul li {
float: left;
width: 150px;
height: 50px;
background-color: #0C3;
border: 1px solid #FFF;
text-align: center;
line-height: 50px;
position: relative;
}
#menu ul li a {
color: #CC0;
display: block;
text-decoration: none;
}
#menu ul li a:hover {
background: #C30;
color: #FFF;
}
#menu ul ul {
display: none;
position: absolute;
}
#menu ul li:hover> ul {
display: block;
}
<div id="menu">
<ul>
<li>L-A
<ul>
<li>A
</li>
<li>B
</li>
</ul>
</li>
<li>L-B
<ul>
<li>C
</li>
<li>D
</li>
</ul>
</li>
</li>
</div>
</div>
i have a menu i'm developing at this link:
http://events.discoverportland.net/
The menu should load to a blue square just to the right of the discover portland logo. There are serveral nested ul's in the menu, the problem i'm having is getting them to load to the side of the menus above them As it load now, you can't get to the teritairy links unless you move VERY fast.
Is there any way to get the uls to load inline as i'm hovering over them, or is there a better solution?
this my nav html:
<nav>
<div class="clearfix hd8 suckerfish" >
<ul id="md">
<li>Home2</li>
<li>Neighborhoods
<ul>
<li>Northwest
<ul>
<li>Pearl District</li>
<li>Oldtown-Chinatown </li>
<li>Nob Hill</li>
</ul>
</li>
<li>Southwest
<ul>
<li>West End</li>
<li>Riverplace-South Waterfront</li>
<li>Downtown</li>
</ul>
</li>
<li>Southeast
<ul>
<li>Sellwood-Westmoreland</li>
<li>Hawthorne</li>
<li>Clinton-Division</li>
</ul>
</li>
<li>Northeast
<ul>
<li>Alberta</li>
<li>Lloyd District</li>
<li>Hollywood District</li>
</ul>
</li>
<li>North
<ul>
<li>Mississippi</li>
<li>Williams</li>
<li>St. Johns</li>
</ul>
</li>
</ul>
</li>
<li>Itineraries</li>
<li>Day Trips</li>
<li>Food+Drink
<ul>
<li>Dining Picks</li>
<li>Food Cart Fare </li>
<li>Beer, Wine & Spirits</li>
<li>Café Culture</li>
</ul>
</li>
<li>To Do
<ul>
<li>Shopping</li>
<li>Recreation</li>
<li>Culture
<ul>
<li>Galleries</li>
</ul>
</li>
<li>Family</li>
</ul>
</li>
<li>Events</li>
</ul>
</nav>
this is the css i'm using:
nav ul ul li a::before {
content: "\f0da";
font-family: FontAwesome;
margin-right: 8px;
}
nav {
text-transform:uppercase;
font-family: Oswald,Arial,Helvetica,sans-serif !important;
font-size: 14px;
font-weight: 100 !important;
letter-spacing: 1px!important;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
background: #FF0066;
padding: 2px 0 0 64px;
border-radius: 0px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #FF0066;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block; padding: 15px 20px;
color: #fff; text-decoration: none;
}
nav ul ul {
background: #FF0066; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 25px;
color: #fff;
font-family: 'Fira Sans', sans-serif;Important;
font-size: 12px;
}
nav ul ul li a:hover {
background: #AD0548;
}
nav ul ul ul {
position: absolute; left: 100%; top:0;
}
nav ul ul ul li {
width: 275px !important;
}
#media (max-width: 767px) {
#logo a {
background: rgba(0, 0, 0, 0) url("http://discoverportland.net/templates/urbanlife/images/logos/DP_HeadingLogo2.png") no-repeat scroll 0 0 / 100% auto;
height: 60px;
margin: 0 !important;
width: 60px !important;
}
#menu-icon {
display:inline-block;
}
nav ul, nav:active ul {
display: none;
position: absolute;
right: 20px;
top: 60px;
width: 50%;
border-radius: 4px 0 4px 4px;
}
nav li {
text-align: center;
width: 100%;
padding: 10px 0;
margin: 0;
}
nav:hover #md {
display: inline-block;
}
}
I have a dropdown below ive creaeted, but im having troulbe centering the the menu. Ive tried to put <center> tags around it and also set the ul to margin auto 0 but its not working. Is there anything im missing?
<style type="text/css">
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #1e7c9a;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: #3b3b3b;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #3b3b3b; }
li:hover li a:hover {
background: #1e7c9a;
}
</style>
</head>
<body>
<ul id="menu">
<li>Home</li>
<li>Portfolio
<ul>
<li>Web Design</li>
<li>Graphic Design</li>
<li>Logo Design</li>
<li>Blog Design</li>
</ul>
</li>
<li>Projects
<ul>
<li>This is a project</li>
<li>So is this</li>
<li>and this</li>
<li>don't forget this too</li>
</ul>
</li>
<li>Contact
<ul>
<li>Support</li>
<li>Quote</li>
<li>General Enquiry</li>
</ul>
</li>
</ul>
I went ahead and put it on jsfiddle Here
I assume that you actually want to center the list items rather than just the menu.
JSfiddle Demo
Revised CSS
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0 auto;
padding: 0;
list-style: none;
text-align: center; /* added this */
font-size:0; /* whitespace adjustment */
}
ul li {
font-size:1rem; /* font-size reset */
display: block;
position: relative;
/* float: left; removed this */
display: inline-block;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #1e7c9a;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: #3b3b3b;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #3b3b3b; }
li:hover li a:hover {
background: #1e7c9a;
}
Do you want the whole menu centered? If so, it helps to stick that all within something, like a table or div
so I added the following to your code:
css
#navbar {
width: 50%;
margin: 0 auto;
}
html
<div id="navbar">
all your ul/li's
</div>
all together
<style type="text/css">
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #1e7c9a;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: #3b3b3b;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #3b3b3b; }
li:hover li a:hover {
background: #1e7c9a;
}
#navbar {
width: 50%;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="navbar">
<ul id="menu">
<li>Home</li>
<li>Portfolio
<ul>
<li>Web Design</li>
<li>Graphic Design</li>
<li>Logo Design</li>
<li>Blog Design</li>
</ul>
</li>
<li>Projects
<ul>
<li>This is a project</li>
<li>So is this</li>
<li>and this</li>
<li>don't forget this too</li>
</ul>
</li>
<li>Contact
<ul>
<li>Support</li>
<li>Quote</li>
<li>General Enquiry</li>
</ul>
</li>
</ul>
</div>
Here is a JSFiddle that demo's what I think you're after: JSFiddle
Basically, I changed your parent <li> to display: inline-block and removed the float: left property. To center those parent nav items, I applied text-align: center to the parent <ul>. Then I changed the nested dropdowns ul ul to be text-align: left and then set those <li>'s to display: block.
Final CSS (changed some selectors to target differently):
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
text-align: center; /* added this */
}
ul > li {
display: inline-block; /* changed from display: block */
position: relative;
/*float: left;*/
}
ul ul { text-align: left; } /* added this */
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #1e7c9a;
margin-left: 1px;
white-space: nowrap;
}
ul ul li { display: block; } /* added this */
ul li a:hover {
background: #3b3b3b;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #3b3b3b; }
li:hover li a:hover {
background: #1e7c9a;
}