CSS Responsive Menu with subs not working - html

Trying to create a responsive nav with sub menus, the first sub menu is working great, but cant seem to get the sub sub menus to work.
It would be great if i can just add UL's under each section and it will pop out.
What am i doing wrong
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
line-height: 40px;
text-align: left;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
padding-left: 15px;
border-bottom: 1px solid #888;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #aaa;
color: #444;
cursor: default;
}
/* Sub Menus */
.nav li li {
font-size: .8em;
}
#media screen and (min-width: 650px) {
.nav li {
width: 130px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1.4em;
display: inline-block;
margin-right: -4px;
}
.nav a {
border-bottom: none;
}
.nav>ul>li {
text-align: center;
}
.nav>ul>li>a {
padding-left: 0;
}
/* Sub Menus */
.nav li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul {
display: block;
}
.nav li ul li {
display: block;
}
}
<header>
<div class="nav">
<ul class="menu">
<li class="home">Home</li>
<li class="tutorials">Tutorials
<ul>
<li>Tutorial #1</li>
<li>
Tutorial #2
<ul>
<li>Tutorial #1</li>
<li>Tutorial #2</li>
<li>Tutorial #3</li>
</ul>
</li>
<li>Tutorial #3</li>
</ul>
</li>
<li class="about"><a class="active" href="#">About</a></li>
<li class="news">Newsletter
<ul>
<li>News #1</li>
<li>News #2</li>
<li>News #3</li>
</ul>
</li>
<li class="contact">Contact</li>
</ul>
</div>
</header>

Your menus are overlapping, that's why you cant see them properly. You can fix this moving the submenus to the right and show them when parent menu item, for example:
HTML:
<header>
<div class="nav">
<ul class="menu">
<li class="home">Home</li>
<li class="tutorials">Tutorials
<ul>
<li>Tutorial #1</li>
<li>
Tutorial #2
<ul class="submenu">
<li>Tutorial #1</li>
<li>Tutorial #2</li>
<li>Tutorial #3</li>
</ul>
</li>
<li>Tutorial #3</li>
</ul>
</li>
<li class="about"><a class="active" href="#">About</a></li>
<li class="news">Newsletter
<ul>
<li>News #1</li>
<li>News #2</li>
<li>News #3</li>
</ul>
</li>
<li class="contact">Contact</li>
</ul>
</div>
</header>
CSS:
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
line-height: 40px;
text-align: left;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
padding-left: 15px;
border-bottom: 1px solid #888;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #aaa;
color: #444;
cursor: default;
}
/* Sub Menus */
.nav li li {
font-size: .8em;
}
ul.submenu {
margin-left:100%;
margin-top: -50px;
}
/*******************************************
Style menu for larger screens
Using 650px (130px each * 5 items), but ems
or other values could be used depending on other factors
********************************************/
#media screen and (min-width: 650px) {
.nav li {
width: 130px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1.4em;
display: inline-block;
margin-right: -4px;
}
.nav a {
border-bottom: none;
}
.nav > ul > li {
text-align: center;
}
.nav > ul > li > a {
padding-left: 0;
}
/* Sub Menus */
.nav li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul:not(.submenu) {
display: block;
}
.nav li ul li {
display: block;
}
.nav ul.submenu { display:none }
.nav li:hover ul li:hover ul.submenu { display:block; }
}

updated fddle https://jsfiddle.net/99Lecy15/2/
.nav li ul li {
display: block;
position:relative;
}
.nav li ul a + ul{
position:absolute;
left:100%;
top:0;
}

Related

CSS for direction of list elements in nested Drop-Down Menu?

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>

How to create sub menu in side menu in html & css

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.

html css ul and button issue

Hi so in this my explore more button and my ul list so whenever I move my explore button line height the ul and button both move together side..and I want the explore more button downward. what should I do?? I tried using other attributes but nothing worked. I am making this website for my dad's academy.
Here is my HTML code
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<div class="row">
<div>
<ul class="main-nav">
<li>Home</li>
<li>About</li>
<li>Study Visa
<ul>
<li>Australia</li>
<li>Canada</li>
<li>New Zealand</li>
</ul>
</li>
<li>Courses
<ul>
<li>Ielts</li>
<li>PTE</li>
<li>Spoken English</li>
</ul>
</li>
<li>Contact Us
</li>
<li> Login </li>
<li>Reviews</li>
</ul>
</div>
<div class="button">
Explore More
</div>
</div>
</header>
</header>
</body>
this is my CSS code
body {
background: url(abc.jpg) no-repeat;
background-size: none;
background-position : none;
background-size : cover;
margin-left: 20%;
}
.main-nav
{
margin: 0px;
padding: 0px;
list-style: none;
font-family: arial;
position: absolute
top : 20px;
}
.main-nav li {
float: left;
width: 145px;
height: 67px;
background-color: black;
opacity: 0.8;
line-height: 67px;
text-align: center;
font-size: 22px;
margin-left : px;
}
.main-nav li a {
text-decoration: none;
color: white;
display: block;
line-height : 100 px ;
}
.main-nav li a:hover {
background-color: orange;
}
.main-nav li ul li{
display: none;
}
.main-nav li:hover ul li {
display: block;
}.button
{
margin-top: 350px;
margin-left: 320px;
}
.btn
{
border: 1px solid white;
padding: 10px 30px;
color: white;
text-decoration: none;
margin-right: 89px;
font-size: 13px;
text-transform: uppercase;
}
.btn-two
{
font-family: "Roboto", sans-serif;
}
.btn-two:hover
{
background-color: darkorange;
transition: all 0.5s ease-in;
}
so I want the menu above near k academy logo.. and explore more button where it is. thanks!!
I changed up your HTML structure since there were empty tags and random closing tags. CSS i basically just removed the float on the li, since that takes it out of the flow, and instead added flexbox to the ul. Hope that helps flexbox ftw. Idk how your button ended up in that location but I would use position: absolute on that top,right,bottom,left:0 margin: auto;
body {
background: url(abc.jpg) no-repeat;
background-size: none;
background-position: none;
background-size: cover;
}
.main-nav {
display: flex;
justify-content: space-between;
alig margin: 0px;
padding: 0px;
list-style: none;
font-family: arial;
}
.main-nav li {
width: 145px;
height: 67px;
background-color: black;
opacity: 0.8;
line-height: 67px;
text-align: center;
font-size: 22px;
margin-left: px;
}
.main-nav li a {
text-decoration: none;
color: white;
display: block;
line-height: 100 px;
}
.main-nav li a:hover {
background-color: orange;
}
.main-nav li ul li {
display: none;
}
.main-nav li:hover ul li {
display: block;
}
.button {
margin-top: 350px;
margin-left: 320px;
}
.btn {
border: 1px solid white;
padding: 10px 30px;
color: white;
text-decoration: none;
margin-right: 89px;
font-size: 13px;
text-transform: uppercase;
}
.btn-two {
font-family: "Roboto", sans-serif;
}
.btn-two:hover {
background-color: darkorange;
transition: all 0.5s ease-in;
}
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<ul class="main-nav">
<li>Home</li>
<li>About</li>
<li>Study Visa
<ul>
<li>Australia</li>
<li>Canada</li>
<li>New Zealand</li>
</ul>
</li>
<li>Courses
<ul>
<li>Ielts</li>
<li>PTE</li>
<li>Spoken English</li>
</ul>
</li>
<li>Contact Us
</li>
<li> Login </li>
<li>Reviews</li>
</ul>
<div class="button">
Explore More
</div>
</header>
</body>
I hope this will help you to solve the problem.
.main-menu {
background-color: #444;
}
.main-menu nav ul {
margin: 0;
padding: 0;
}
.main-menu nav > ul > li {
display: inline-block;
position: relative;
margin: 0;
}
.main-menu nav > ul > li a {
display: block;
color: #fff;
font-size: 16px;
padding: 20px 15px;
text-decoration: none;
transition: .3s;
}
.main-menu nav > ul > li:hover > a {
color: #7AA93C;
}
.main-menu nav ul li ul {
position: absolute;
padding: 10px 0;
width: 200px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
top: 150%;
opacity: 0;
visibility: hidden;
transition: .3s;
}
.main-menu nav ul li:hover ul {
top: 100%;
opacity: 1;
visibility: visible;
}
.main-menu nav ul li ul li {
display: block;
padding: 0;
margin: 0;
}
.main-menu nav ul li ul li a {
display: block;
text-decoration: none;
padding: 6px 20px;
font-size: 14px;
color: #444;
transition: .3s;
}
.main-menu nav ul li ul li:hover a {
color: #7AA93C;
}
<div class="main-menu">
<nav>
<ul>
<li>
Home
<!-- submenu start -->
<ul class="submenu">
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
</ul>
<!-- submenu end -->
</li>
<li>About</li>
<li>
Service
<!-- submenu start -->
<ul class="submenu">
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
<li>Sub Menu</li>
</ul>
<!-- submenu end -->
</li>
<li>Contact</li>
</ul>
</nav>
</div>

CSS Dropdown-menu not displaying when <li>-element is hovered.

I have a top navigation bar with 4 options, where I wish that while the fourth option is hovered, a dropdown-menu will display.
The navigation bar is an unordered list, and the dropdown-menu (which should be activated by the fourth option) is a div consisting of 3 links.
The list item that is supposed to display the dropdown-menu while hovered:
<li class="dropdownbutton">Contact</li>
With the help of this line of code:
.dropdownbutton:hover .dropdowncontent {
display:inline-block;
}
And this is the CSS for the dropdown-content:
.dropdowncontent {
display:none;
background-color:#333;
margin-left:272px;
width:90px;
text-align:center;
}
What am I missing?
This should be allright.
ul {
margin: 0;
padding: 0;
list-style: none;
background-color: #333;
}
ul li {
display: inline-block;
position: relative;
text-align: left;
background-color: #333;
}
ul li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 16px 15px;
text-decoration: none;
transition-duration: 0.3s;
-webkit-transition-duration: 0.3s;
font-size: 17px;
font-family: Arial;
}
ul li a:hover {
background-color: #555;
}
ul li ul.dropdown {
min-width: 100%;
display: none;
position: absolute;
z-index: 999;
left: 0;
width: 90px;
}
ul li:hover ul.dropdown {
display: block;
}
ul li ul.dropdown li {
display: block;
}
ul li ul.dropdown li a {
font-size: 14px;
padding: 10px 15px;
}
<ul>
<li>Home</li>
<li>About Us</li>
<li>Products</li>
<li>Contact
<ul class="dropdown">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</li>
</ul>

CSS drop down menu 2nd level inline-block view

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