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>
Related
The Problem:
Look at Offers and the li:
After putting position: absolute
Current vs What I want to achieve
How to make my drop down menu so that when I hover on "Offers" the text doesn't move to the left? I would also like to decrease the width of the li as it is too big for me.
I have tried changing the display: property and putting spaces before and after the word "Offers" in HTML. The spaces worked but I didnt like it because the Offers will just look like having more space than the other options.
.nav {
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 0;
width: 100%;
height: 80px;
}
.menu {
float: right;
line-height: 80px;
margin: 0 9em;
text-align: center;
font-family: "Proxima Nova";
text-transform: uppercase;
font-weight: bolder;
letter-spacing: 0px;
}
.menu ul {
margin: 0px;
padding: 0px;
list-style: none;
}
.menu ul li {
text-align: center;
list-style: none;
display: inline-table;
}
.menu ul li a {
text-decoration: none;
color: white;
font-size: 16px;
font-weight: lighter;
padding: 0 20px;
transition: all .3s ease-in-out;
}
.menu ul li a:hover {
color: orange;
}
.menu ul li ul li {
display: none; /*So li dont show up unless hover */
}
.menu ul li:hover ul li {
transition: all .3s ease;
display: block;
background: rgba(0, 0, 0, .6);
}
ul li:nth-child(5) a {
color: white;
border: 1px solid orange;
padding: 10px 20px;
border-radius: 4px;
background-color: none;
transition: all 1s ease-out;
}
ul li:nth-child(5) a:hover {
transition: all .5s ease-in;
background: rgba(204, 204, 204, 0.5);
color: orange;
}
<div class="nav">
<div class="menu">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Offers
<ul>
<li>Packages</li>
<li>Services</li>
<li>Promos</li>
</ul>
</li>
<li>Location</li>
<li>Contact</li>
</ul>
</div>
</div>
I want to dropdown menu to not move when the li is shown in the Offers. And I would like to decrease the width of black background of the li
That's because the sub menu is taking a place and make its parent li wider.
A possible solution is to set the ul child position: absolute so it will not take a place.
Like this:
.menu ul ul {
position: absolute;
}
Live example:
body {
background: url(https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg) 0 0;
background-size: cover;
}
.nav {
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 0;
width: 100%;
height: 80px;
}
.menu {
float: right;
line-height: 80px;
margin: 0 9em;
text-align: center;
font-family: "Proxima Nova";
text-transform: uppercase;
font-weight: bolder;
letter-spacing: 0px;
}
.menu ul {
margin: 0px;
padding: 0px;
list-style: none;
}
.menu ul li {
text-align: center;
list-style: none;
display: inline-table;
position: relative;
}
.menu ul li a {
text-decoration: none;
color: white;
font-size: 16px;
font-weight: lighter;
padding: 0 20px;
transition: all .3s ease-in-out;
}
.menu ul li a:hover {
color: orange;
}
.menu ul ul {
position: absolute;
width: 100%;
}
.menu ul li ul li {
display: none;
/*So li dont show up unless hover */
}
.menu ul li ul li a {
padding: 0;
text-align: center;
}
.menu ul li:hover ul li {
transition: all .3s ease;
display: block;
background: rgba(0, 0, 0, .6);
}
ul li:nth-child(5) a {
color: white;
border: 1px solid orange;
padding: 10px 20px;
border-radius: 4px;
background-color: none;
transition: all 1s ease-out;
}
ul li:nth-child(5) a:hover {
transition: all .5s ease-in;
background: rgba(204, 204, 204, 0.5);
color: orange;
}
<div class="nav">
<div class="menu">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Offers
<ul>
<li>Packages</li>
<li>Services</li>
<li>Promos</li>
</ul>
</li>
<li>Location</li>
<li>Contact</li>
</ul>
</div>
</div>
The problem lies with the min-content size of the list items in the drop down, which are larger than the size of the parent list items in the top menu.
e.g. 'packages' renders at 120px whereas 'offers' is 98px.
The simplest solution is to set a max-width for all the list items based on the max-content size of the longest word (not very dynamic though).
Otherwise, use position:absolute to layout the sub menu as in this example:
https://codepen.io/skippingredpanda/pen/xNrxVg
Is simple, use:
.menu li { position: relative; } .menu li ul { position: absolute; top: your horizontal nav height; left: 0;}
And also you have a mistake in code, must be like this:
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>
Link 3
<ul>
<li>SubLink 1</li>
<li>SubLink 2</li>
<li>SubLink 3</li>
<li>SubLink 4</li>
</ul>
</li>
<li>Link 4</li>
</ul>
ul li {position: relative;}
ul li ul {position: absolute; display: none;}
ul li:hover ul {display: block;}
I am trying to build a dropdown menu. My problem is that hovering the content also shows the dropdown menu. I am currently using this code:
/* actual dropdown menu */
#menu-hauptmenue {
height: 45px;
}
.sub-menu {
opacity: 0;
background: #4d4d4d;
display: block;
z-index: 200;
width: 250px;
position: absolute;
margin-top: 23px;
}
.sub-menu li {
display: block;
width: 250px;
line-height: 2.7;
padding: 10px 15px 10px 15px;
z-index: 220;
color: #fff;
font-family: 'Roboto', sans-serif;
font-size: 1em;
}
.sub-menu li a {
color: #fff;
font-family: 'Roboto', sans-serif;
font-size: 1em;
text-decoration: none;
}
.sub-menu li:hover {
background-color: #333333;
cursor: pointer;
}
.sub-menu a:hover {
border-bottom: none;
}
#menu-hauptmenue li:hover .sub-menu {
opacity: 1;
}
#billboard img {
z-index: 1;
}
li {
z-index: 20;
}
.clearfix:after {
display: block;
clear: both;
}
nav>ul>li {
position: relative;
}
nav.open .sub-menu {
padding-top: 0px;
display: none;
}
/* styling of the nav */
nav ul {
list-style: none;
display: flex;
flex-direction: row;
justify-content: space-between;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #4D4D4D;
font-family: 'Roboto', sans-serif;
font-size: 1em;
text-decoration: none;
}
nav ul li a:hover {
border-bottom: 1px solid #4D4D4D;
}
<nav>
<ul id="menu-hauptmenue">
<li>Parent 1
<ul class="sub-menu">
<li>Dropdown 1</li>
<li>Dropdown 2</li>
<li>Dropdown 3</li>
</ul>
</li>
<li>Parent 2</li>
<li>Parent 3</li>
</ul>
</nav>
As you can see, hovering the content below the navigation also displays the dropdown menu. It would be better to only display the navigation when hovering the parent li element, in this case 'Parent 1'. How can I achieve this the best way? Thank you for your advice.
It's because your element is present with 0 opacity so you are able to interact with it. You may try using display:none/block like this:
/* actual dropdown menu */
#menu-hauptmenue {
height: 45px;
}
.sub-menu {
opacity: 0;
background: #4d4d4d;
display: none;
width: 250px;
position: absolute;
margin-top: 23px;
}
.sub-menu li {
display: block;
width: 250px;
line-height: 2.7;
padding: 10px 15px 10px 15px;
z-index: 220;
color: #fff;
font-family: 'Roboto', sans-serif;
font-size: 1em;
}
.sub-menu li a {
color: #fff;
font-family: 'Roboto', sans-serif;
font-size: 1em;
text-decoration: none;
}
.sub-menu li:hover {
background-color: #333333;
cursor: pointer;
}
.sub-menu a:hover {
border-bottom: none;
}
#menu-hauptmenue li:hover .sub-menu {
opacity: 1;
display:block;
}
#billboard img {
z-index: 1;
}
li {
z-index: 20;
}
.clearfix:after {
display: block;
clear: both;
}
nav>ul>li {
position: relative;
}
nav.open .sub-menu {
padding-top: 0px;
display: none;
}
/* styling of the nav */
nav ul {
list-style: none;
display: flex;
flex-direction: row;
justify-content: space-between;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #4D4D4D;
font-family: 'Roboto', sans-serif;
font-size: 1em;
text-decoration: none;
}
nav ul li a:hover {
border-bottom: 1px solid #4D4D4D;
}
<nav>
<ul id="menu-hauptmenue">
<li>Parent 1
<ul class="sub-menu">
<li>Dropdown 1</li>
<li>Dropdown 2</li>
<li>Dropdown 3</li>
</ul>
</li>
<li>Parent 2</li>
<li>Parent 3</li>
</ul>
</nav>
Been trying to fix my code for a couple of hours and I just can't. I want to have my navbar have a dropdown function. But I can't seem to make it work.
As you can see I've start on the dropdown menu class, but I can't figure where to go from there. Been googling, and found a lot of examples, tried to copy/paste some of the bits, with no good results. So I was thinking that you might be able to help.
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
padding: 20px;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
.dropdown {
display: none;
}
<nav>
<ul>
<li>FORSIDE
</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX
</li>
<li>HTX
</li>
<li>OM TECH COLLEGE
</li>
</ul>
</nav>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
for more detail visit Dropdown Menu in Navbar
Change the CSS to this:
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
padding: 20px;
position:relative;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
nav ul li ul {
display:none;
}
nav ul li:hover ul {
display:block;
position:absolute;
background:indianred;
}
<nav>
<ul>
<li>FORSIDE</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX</li>
<li>HTX</li>
<li>OM TECH COLLEGE</li>
</ul>
</nav>
You wanted to hide the dropdown but then show it when you hover over the dropdown's parent element.
I then made the parent li position:relative; and the dropdown position:absolute so it didn't push any of the other dropdown menu items anywhere it shouldn't be, but didn't style it past giving it a background colour.
you need to write css for the element on hover
like
element1:hover tobeshownele{display:block;}
You can try something like this. Didn't want to change too much so the code and UI are still quite messy.
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
margin: 20px;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
.dropdown {
display: none;
position: absolute;
}
nav ul li:hover .dropdown {
display: block;
}
ul.dropdown li {
display: block;
padding: 10px;
}
<nav>
<ul>
<li>FORSIDE</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX</li>
<li>HTX</li>
<li>OM TECH COLLEGE</li>
</ul>
</nav>
I've spent more time trying to do this than I'm willing to admit. I just can't seem to figure this out. I'm trying to build something which I believe is very simple. I've got a horizontal menu. And I want to have the submenu be vertical when someone hovers over a given link.
This is what I've got right now at RetirePhoenixArizona.com:
HTML
<nav class="secondary-navigation">
<div class="container">
<ul id="secondary-menu">
<li>Link 1
<ul class="secondary-submenu">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
</ul>
</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</div>
</nav>
CSS
#secondary-menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
#secondary-menu li {
float: left;
width: 25%;
}
#secondary-menu li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#secondary-menu li a:hover {
background-color: #111;
}
#secondary-menu ul {
display: none;
}
#secondary-menu li:hover > ul {
position: relative;
display: inline;
width: 200px;
height: 45px;
position: absolute;
margin: 40px 0 0 0;
}
#secondary-menu li:hover > ul li {
float: none;
width: 100%;
}
Remove the float on the li of the submenu
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
float: left;
width: 25%;
position: relative;
}
a {
display: block;
background: #333;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
ul ul {
display: none;
}
li:hover > ul {
display: block;
position: absolute;
left: 0;
top: 40px;
right: 0;
}
li:hover > ul li {
float: none;
width: 100%;
}
I Just do a responsive horizontal Menu ... here
If you don't need responsive remove all concern container , bar1,bar2,bar3, icon, change, #media screen and (max-width:680px) and the javascript
HTML drop down menu secondary links are automatically showing. i want to make them visible
Codepen demo
HTML
Dropdown Menu - WordPress Style
<h1>The Title of your Blog</h1>
<nav>
<div class="menu">
<ul>
<li>Blog</li>
<li>Front Page</li>
<li>Sample Page</li>
<li>About The Tests
<ul>
<li>Page Image Alignment</li>
<li>Page Markup And Formatting</li>
<li>Clearing Floats</li>
<li>Page with comments</li>
<li>Page with comments disabled</li>
</ul>
</li>
<li>Level 1
<ul>
<li>Level 2a
<ul>
<li>Level 3a</li>
<li>Level 3b</li>
<li>Level 3c</li>
</ul>
</li>
<li>Level 2b</li>
<li>Level 2c</li>
</ul>
</li>
<li>Lorem Ipsum</li>
<li>Page A</li>
<li>Page B</li>
</ul>
</div>
</nav><head>
<title>Dropdown Menu - WordPress Style</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<h1>The Title of your Blog</h1>
<nav>
<div class="menu">
<ul>
<li>Blog</li>
<li>Front Page</li>
<li>Sample Page</li>
<li>About The Tests
<ul>
<li>Page Image Alignment</li>
<li>Page Markup And Formatting</li>
<li>Clearing Floats</li>
<li>Page with comments</li>
<li>Page with comments disabled</li>
</ul>
</li>
<li>Level 1
<ul>
<li>Level 2a
<ul>
<li>Level 3a</li>
<li>Level 3b</li>
<li>Level 3c</li>
</ul>
</li>
<li>Level 2b</li>
<li>Level 2c</li>
</ul>
</li>
<li>Lorem Ipsum</li>
<li>Page A</li>
<li>Page B</li>
</ul>
</div>
</nav>
CSS
#charset "UTF-8";
/* CSS Document */
* {
margin: 0;
padding: 0;
}
h1 {
margin-top: 25px;
margin-bottom: 25px;
margin-left: 50px;
}
nav {
font-family: Impact, Haettenschweiler, "Franklin Gothic Bold", "Arial Black", sans-serif;
color: white;
margin-left: 50px;
}
nav ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
}
nav ul li {
font: 12px bold;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #2d2c2c;
}
nav ul li:hover {
background: #555;
color: #fff;
text-decoration: none;
}
ul li:hover {
background: #555;
color: #fff;
text-decoration: none;
}
nav ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
}
nav ul li ul li {
background: #555;
display: block;
color: #fff;
}
nav ul li ul li {
background: #555;
display: block;
color: #fff;
}
nav ul li ul li:hover {
background: #666;
}
nav ul li:hover ul {
display: block;
}
nav a:link {
text-decoration: none;
color: white;
}
nav a:visited {
text-decoration: none;
color: white;
}
nav a:hover {
text-decoration: none;
color: white;
}
nav a:active {
text-decoration: none;
color: white;
}
nav ul li:hover > ul {
display: inherit;
}
nav:after {
content: '';
display: table;
clear: both;
}
nav ul ul ul {
position: absolute;
top: 0px;
left: 100%;
}
nav ul ul ul li:first-child {
margin-top: 20px;
}
nav ul ul li {
min-width: 170px;
float: none;
background-color: #333;
display: list-item;
position: relative;
}
nav li:hover > ul {
display: block;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
}
nav ul li:hover > ul {
display: inherit;
}
nav ul ul ul {
position: absolute;
top: 0px;
left: 100%;
}
Watch here:
http://codepen.io/anon/pen/avVZNp
I've added
nav ul li ul li ul li {
display: none;
}
nav ul li ul li:hover ul li {
display: block;
}
Full CSS
#charset "UTF-8";
/* CSS Document */
* {
margin: 0;
padding: 0;
}
h1 {
margin-top: 25px;
margin-bottom: 25px;
margin-left: 50px;
}
nav {
font-family: Impact, Haettenschweiler, "Franklin Gothic Bold", "Arial Black", sans-serif;
color: white;
margin-left: 50px;
}
nav ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 0 0;
list-style: none;
}
nav ul li {
font: 12px bold;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #2d2c2c;
}
nav ul li:hover {
background: #555;
color: #fff;
text-decoration: none;
}
ul li:hover {
background: #555;
color: #fff;
text-decoration: none;
}
nav ul li ul {
padding: 0;
position: absolute;
left: 0;
width: 150px;
}
nav ul li ul li {
background: #555;
display: block;
color: #fff;
}
nav ul li ul li {
background: #555;
display: block;
color: #fff;
}
nav ul li ul li:hover {
background: #666;
}
nav ul li ul li ul li {
display: none;
}
nav ul li ul li:hover ul li {
display: block;
}
nav ul li:hover ul {
display: block;
}
nav a:link {
text-decoration: none;
color: white;
}
nav a:visited {
text-decoration: none;
color: white;
}
nav a:hover {
text-decoration: none;
color: white;
}
nav a:active {
text-decoration: none;
color: white;
}
nav ul li:hover > ul {
display: inherit;
}
nav:after {
content: '';
display: table;
clear: both;
}
nav ul ul ul {
position: absolute;
top: 0px;
left: 100%;
}
nav ul ul ul li:first-child {
margin-top: 20px;
}
nav ul ul li {
min-width: 170px;
float: none;
background-color: #333;
display: list-item;
position: relative;
}
nav li:hover > ul {
display: block;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
}
nav ul li:hover > ul {
display: inherit;
}
nav ul ul ul {
position: absolute;
top: 0px;
left: 100%;
}