css drop down menu is only showing last list item - html

My drop down menu is only showing the last drop down items. I know my css has something wrong with it, but I cannot figure it out. Can someone please help? Thanks in advance.
Here is the HTML:
<div class="menuBar">
<nav class="Menu">
<ul class="main">
<li>Home</li>
<li>Weddings
<ul>
<li>Gallery</li>
<li>Testimonials</li>
</ul>
</li>
<li>Restaurant
<ul>
<li>Gallery</li>
</ul>
</li>
<li>Special Occasions</li>
<li>Corporate</li>
<li>Upcoming Events</li>
<li>Contact Us</li>
<li>Our Locations</li>
</ul>
</nav>
</div>
Here is the CSS:
nav.Menu {
text-align: center;
margin-top: 14px;
}
nav.Menu .main li {
display: inline-block;
margin-right: -4px;
position: relative;
padding: 5px 20px;
cursor: pointer;
}
nav.Menu .main li ul li {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
display: none;
opacity: 0;
visibility: hidden;
}
nav.Menu .main li ul li {
background: #83562c;
display: block;
color: #fff;
margin-top: -2px;
z-index: 1000 !important;
}
nav.Menu .main li:hover ul li {
background: #83562c;
}
nav.Menu .main li:hover ul li {
display: block;
opacity: 1;
visibility: visible;
z-index:99;
}
nav.Menu a {
color: #fff;
font: bold 13px/42px Arial, Helvetica, sans-serif;
padding: 0 10px;
text-align: center;
}
nav.Menu a:hover {
color: #eacc90;
}

Huangism is right, your use of position:absolute is breaking the dropdown. I've rewritten your CSS in the following JSFiddle, but it's still hackish.
I'd recommend doing some reading on dropdown menus:
http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu
http://cssdeck.com/labs/another-simple-css3-dropdown-menu

drove me to desparation but with a little trickery position:absolute does work http://jsfiddle.net/z509cjmg/3/. Adapted from here: http://cssdeck.com/labs/multi-level-dropdown-menu
.navmenu ul li ul {
visibility: hidden;
opacity: 0;
position: absolute;
transition: all 1s ease;
margin-left: 142px;
margin-top: -30px;
}

Related

Making a drop down menu in HTML and CSS

I want to make a dropdown menu just using html and css. However, I am unable to do it. I want to make the others list item a dropdwon menu. Can anyone help me with that?
Here's my CSS code:
```
nav {
background-color: #01a6f0;
}
.navbar {
list-style: none;
float: right;
margin-right: 10rem;
margin-top: 2.6rem;
margin-bottom: .5rem;
}
.navbar li {
display: inline-block;
padding: 1rem 1rem;
}
.navbar li:hover {
background-color: #01A6FE;
}
.navbar li a {
color: #fff;
}
```
Here's the HTML code
```
<nav>
<ul class="navbar">
<li>Home</li>
<li>Puzzles</li>
<li>Stories</li>
<li>Goods</li>
<li class="dropdown-link">Others
<ul class="dropdown-menu">
<li>Under The Sea</li>
<li>Middle The Sea</li>
<li>Ovver The Sea</li>
</ul>
</li>
</ul>
</nav>
Try this example with just HTML and CSS:
It is from this site if you want to read more about this code:
https://css-tricks.com/solved-with-css-dropdown-menus/
a {
text-decoration: none;
}
nav {
font-family: monospace;
}
ul {
background: darkorange;
list-style: none;
margin: 0;
padding-left: 0;
}
li {
color: #fff;
background: darkorange;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
li a {
color: #fff;
}
li:hover {
background: red;
cursor: pointer;
}
ul li ul {
background: orange;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
<nav role="navigation">
<ul>
<li>One</li>
<li>Two
<ul class="dropdown">
<li>Sub-1</li>
<li>Sub-2</li>
<li>Sub-3</li>
</ul>
</li>
<li>Three</li>
</ul>
</nav>

The submenu doesn't show up when hover the mouse

I have a problem. I don't know what wrong with the code as when I hover the mouse over 'About us', the submenu didn't show up. I did make the submenu display:none and then when hover, it display: block but nothing happen. Thanks for your help
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
display: none;
position: absolute;
top: 100%;
list-style: none;
}
#nav_menu #sub_menu:hover{
display: block;
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>
My method is a little different and shorter with the animation opening and closing for the submenu, I used the scale instead of display for animation;
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
position: relative;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
position: absolute;
top: 100%;
list-style: none;
padding: 0;
transform : scaleY(0);
transform-origin: top;
transition: all 0.4s;
}
#nav_menu li:hover #sub_menu{
transform: scaleY(1);
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>
To display a submenu when hovering, you need to write like this:
#nav_menu li:hover #sub_menu {
display: block;
}
Because you are accessing the #sub_menu selector. Also, you need to add position: relative the selector #nav_menu li is relevant for the correctness of your position: absolute of the selector #nav_menu #sub_menu. And add padding: 0 for the selector of the same selector #nav_menu #sub_menu, in order to align it under the main menu.
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
position: relative;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
display: none;
position: absolute;
top: 100%;
list-style: none;
padding: 0;
}
/*#nav_menu #sub_menu:hover{
display: block;
}*/
#nav_menu li:hover #sub_menu{
display: block;
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>

I need help in fixing my drop down menu from moving when showing <li>

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;}

Can't center navbar and have to click the word itself

This is my css code (for the index page):
span{
display:block;
font-size:100px;
font-weight: lighter;
font-family: "Times New Roman", Times, serif;
}
.index {
background-image: url("images/indexbackground.png");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 1600px 800px;
}
/*NAVBAR (begin)*/
a {
text-decoration: none;
}
nav {
font-family: Arial;
background-color: Black;
}
ul {
background: black;
list-style: none;
margin: 0;
padding-left: 0;
}
li {
color: #white;
background: black;
display: block;
float: left;
padding: 27px;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
li a {
color: #fff;
}
li:hover {
background: #DEB887;
cursor: pointer;
}
ul li ul {
background: black;
visibility: hidden;
opacity: 0;
min-width: 17rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 27px;
left: 0;
display: none;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
/*NAVBAR (end)*/
This is the code for HTML (index page):
<!DOCTYPE html>
<link rel="stylesheet" href="piano.css" />
<html class="index">
<head>
<title>The piano - Home</title>
</head>
<body>
<br><br><br><br><br><br><br><br><br><br><br><br>
<center><span>The Piano</span></center>
<!--(start) NAVBAR-->
<br><br><br><br><br><br>
<nav>
<div class="navwrapperindex">
<nav class="navmenuindex">
<ul class="clearfixindex">
<li>Home</li>
<li><a>The piano itself</a>
<ul class="sub-menu">
<li>Mechanics</li>
<li>Construction</li>
</ul>
<li><a>Types of pianos</a>
<ul class="sub-menu">
<li>Grand</li>
<li>Upright</li>
<li>Electric</li>
</ul>
</li>
<li>History of the piano
</li>
<li>Piano songs
</li>
</div>
</nav>
<!--(end)NAVBAR-->
</body>
</html>
I already tried a lot of things but nothing seems to work...
Right now my index page looks like this:
(Index page but I had to cut it because the full picture was over 2 mb)
(I'm sorry, It's really bad but it's my first time making a website with html and css).
I need to center the navigation bar and to be able to click the box instead of the word.
Thank you for reading (and maybe answering)!
First of all, I recommend to use classes instead of the direct element selector,
If you want to center the nav, you only need to set the property align-text: center in the parent divof the main nav. and if you want to click all the box instead only the text, the element that needs the padding and margin is the a not the li.
Check the snippet:
span{
display:block;
font-size:100px;
font-weight: lighter;
font-family: "Times New Roman", Times, serif;
}
.index {
background-image: url("images/indexbackground.png");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 1600px 800px;
}
.navwrapperindex {
text-align: center;
}
.navwrapperindex > nav{
display: inline-block;
}
a {
text-decoration: none;
}
nav {
font-family: Arial;
}
ul {
background: black;
list-style: none;
margin: 0;
padding-left: 0;
}
li {
color: #white;
background: black;
float: left;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
li a {
color: #fff;
padding: 27px;
display: block;
}
li:hover {
background: #DEB887;
cursor: pointer;
}
ul li ul {
background: black;
visibility: hidden;
opacity: 0;
min-width: 17rem;
position: absolute;
transition: all 0.5s ease;
left: 0;
display: none;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
<center><span>The Piano</span></center>
<!--(start) NAVBAR-->
<br><br><br><br><br><br>
<nav>
<div class="navwrapperindex">
<nav class="navmenuindex">
<ul class="clearfixindex">
<li>Home</li>
<li><a>The piano itself</a>
<ul class="sub-menu">
<li>Mechanics</li>
<li>Construction</li>
</ul>
<li><a>Types of pianos</a>
<ul class="sub-menu">
<li>Grand</li>
<li>Upright</li>
<li>Electric</li>
</ul>
</li>
<li>History of the piano
</li>
<li>Piano songs
</li>
</ul>
</nav>
</div>
</nav>

Prevent nested nav items from breaking on to the next line?

I have a basic horizontal nav menu, where a drop down list shows on hover. When the drop down links are single words everything works fine, but if the links are more than one word, ie "Lori Ipsum", the line breaks. I'm looking for the lines to keep their full width, so "Lori Ipsum" would be shown on the same line.
How can I prevent the line break?
Recreated in a code pen: http://codepen.io/agconti/pen/zvjkm
html:
<nav role='navigation'>
<ul>
<li>Home</li>
<li>
About
<ul>
<li>Lorem ipsum</li>
<li>Aliquam tincidunt</li>
<li>Vestibulum auctor</li>
</ul>
</li>
<li>Clients</li>
<li>Contact Us</li>
</ul>
</nav>
css:
html {
font-size: 1.5em;
}
a {
color: white;
text-decoration: none;
width: 100%;
}
li {
transition: color 0.15s ease-out, background-color 0.1s ease-in;
}
ul {
list-style: none;
background: blue;
margin: 0;
}
ul li {
padding: 1em;
margin: 0;
display: inline-block;
position: relative;
}
ul li:hover {
background: pink;
}
ul li:hover > ul {
display: block;
}
ul ul {
background-color: pink;
position: absolute;
display: none;
padding: 0;
left: 0;
top: 100%;
}
ul ul li {
box-sizing: border-box;
display: block;
minwidth: 100%;
}
ul ul li:hover {
background: #ffa6b6;
color: white;
}
set li default width;
or use
li a {
white-space: nowrap;
}
Maybe I am missing something, but you have a pretty good solution as is, and you could
fix the text wrap problem by specifying a width to the nest ul as follows:
ul ul {
background-color: tan;
position: absolute;
display: none;
padding: 0;
left: 0;
top: 100%;
width: 300px;
}
I used 300px but you can pick a suitable value.
html {
font-size: 1.5em;
}
a {
color: white;
text-decoration: none;
}
li {
transition: color 0.15s ease-out, background-color 0.1s ease-in;
}
ul {
list-style: none;
background: blue;
margin: 0;
}
ul li {
padding: 1em;
margin: 0;
display: inline-block;
position: relative;
}
ul li:hover {
background: pink;
}
ul li:hover > ul {
display: block;
}
ul ul {
background-color: tan;
position: absolute;
display: none;
padding: 0;
left: 0;
top: 100%;
width: 300px;
}
ul ul li {
/* white-space: nowrap;*/
box-sizing: border-box;
display: block;
min-width: 100%;
}
ul ul li:hover {
background: #ffa6b6;
color: white;
}
ul ul li a {
width: 200%;
}
<nav role='navigation'>
<ul>
<li>Home</li>
<li>
About
<ul>
<li>Lorem ipsum</li>
<li>Aliquam tincidunt</li>
<li>Vestibulum auctor</li>
</ul>
</li>
<li>Clients</li>
<li>Contact Us</li>
</ul>
</nav>
<section class="hero">
<div class="mask"></div>
</section>
li a {
white-space: nowrap;
}