I am making a css navbar dropdown, and am having some issues. I'm not the most experienced person in css, so a little help would be appreciated.
The products text doesn't change color, like on the other ones and you can only click on the text of the products box.
Also the dropdown is not horizontally aligned with the products box.
Here's a link to jsfiddle:
https://jsfiddle.net/epp0zmd6/
Code(HTML):
<div class="navbar">
<ul>
<li class="active">Homepage</li>
<li>Contact Us</li>
<li>Web Learning Platform</li>
<li><a href="#">Products
<ul>
<li>Requirement Extraction & Analysis</li>
<li>Early Deduction Modelling & Analysis</li>
</ul>
</li></a>
</ul>
</div>
Code(CSS):
.navbar ul {
display: inline-table;
vertical-align: middle;
list-style: none;
position: relative;
}
.navbar ul:after {
content: "";
clear: both;
display: block;
}
.navbar ul ul {
display: none;
border-radius: 0;
padding: 0;
position: absolute;
top: 100%;
}
.navbar ul ul li {
float: none;
position: relative;
}
.navbar ul li:hover > ul {
display: block;
}
.navbar ul li {
background-color: #0A6CA3;
display: inline-block;
vertical-align: middle;
font-family: SinkinSans;
font-size: 20px;
padding-top: 27px;
padding-bottom: 21px;
padding-left: 10px;
padding-right: 10px;
color: #FFFFFF;
transition: all 0.8s;
-moz-transition: all 0.8s;
-webkit-transition: all 0.8s;
-o-transition: all 0.8s;
margin-left: -2px;
margin-right: -2px;
float: left;
}
.navbar ul li:hover {
background-color: #FFFFFF;
color: #00A3FF;
}
.navbar ul a {
text-decoration: none;
}
.navbar ul a:visited {
color: #FFFFFF;
}
.navbar ul a:hover {
color: #00A3FF;
}
.navbar ul .active {
background-color: #FFFFFF;
color: #00A3FF;
}
the first mistake is, always put anchor tag inside the li tag
and second mistake is for the dropdown use ul > li > a and than, start child ul before closing li and after closing the anchor tag
and for proper clicking i have put padding on anchor tag with display inline block property.
i have updated fiddle i think you want something like this try this, hope the code will help you.
.navbar ul {
display: inline-table;
vertical-align: middle;
list-style: none;
position: relative;
}
.navbar ul:after {
content: "";
clear: both;
display: block;
}
.navbar ul ul {
display: none;
border-radius: 0;
padding: 0;
position: absolute;
top: 100%;
}
.navbar ul li:hover > ul {
display: block;
}
.navbar ul li {
display: inline-block;
margin-left: -2px;
margin-right: -2px;
float: left;
}
.navbar ul li >a{
padding-top: 27px;
padding-bottom: 21px;
padding-left: 10px;
padding-right: 10px;
display:inline-block;
color:#fff;
text-decoration: none;
background-color: #0A6CA3;vertical-align: middle;
font-family: SinkinSans;
font-size: 20px;
transition: all 0.8s;
-moz-transition: all 0.8s;
-webkit-transition: all 0.8s;
-o-transition: all 0.8s;
}
.navbar ul li:hover >a,
.navbar ul li.active >a{
background-color: #FFFFFF;
text-decoration: none;
color: #00A3FF;
}
<div class="navbar">
<ul>
<li class="active">Homepage</li>
<li>Contact Us</li>
<li>Web Learning Platform</li>
<li>Products
<ul>
<li>Requirement Extraction & Analysis</li>
<li>Early Deduction Modelling & Analysis</li>
</ul>
</li>
</ul>
</div>
You have 2 links on your "Products" entry, that's too much:
<li><a href="#">Products
etc.
Just remove one.
Concerning the position of the dropdown: add
.navbar ul li { left: -8px; }
Related
So I am trying to make a basic nav menu with a drop down from my Django app. My menu is fine, but the dropdown doesn't want to show all the links.
How to fix this?
HTML
<nav role="navigation">
<ul>
<li>Chat Home</li>
<li>Go To <i class="fa fa-caret-down"></i>
<ul class="dropdown" aria-label="submenu">
<li>Calendar</li>
<li>Big Blue</li>
</ul>
</li>
<li>Logout</li>
</ul>
</nav>
CSS
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
position: sticky;
position: -webkit-sticky;
width: 100%;
height: 1.5rem;
}
li {
float: left;
font-size: 1rem;
padding: 0.25rem 1rem;
letter-spacing: 1.5px;
cursor: pointer;
display: block;
position: relative;
}
li a {
color: white;
text-decoration: none;
text-align: center;
}
ul li ul li {
display: block;
padding: 0.25rem 1rem;
}
li:hover,
li:focus-within {
background-color: black;
}
li:focus-within a {
outline: none;
}
ul li ul {
display: none;
background-color: #333;
position: absolute;
visibility: hidden;
left: 0;
margin-top: 2px;
}
ul li:hover > ul,
ul li:focus-within > ul,
ul li ul:hover,
ul li ul:focus {
display: block;
visibility: visible;
}
You can see what I mean here: https://jsfiddle.net/rj269hsf/
But essentially, when I hover over the "Go To" item it will drop the first listed item below it. The only way to see the second is to move down and hover where it would be, then it shows up.
You can fix the problem by wrapping the dropdown <ul> in a <div>. I also gave the nav item with the dropdown the class of .dropdown-btn to make the CSS easier to understand.
The ul li:hover > ul selector you've used is also incorrect - I replaced it with .dropdown-btn:hover ul which selects the ul which is a child of .dropdown-btn but only when it is hovered.
Lastly, you don't need both visibility and display to hide the dropdown, simply display: none will do.
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
position: sticky;
position: -webkit-sticky;
width: 100%;
height: 1.5rem;
}
li {
float: left;
font-size: 1rem;
padding: 0.25rem 1rem;
letter-spacing: 1.5px;
cursor: pointer;
display: block;
position: relative;
}
li a {
color: white;
text-decoration: none;
text-align: center;
}
/*ul li ul li {
display: block;
padding: 0.25rem 1rem;
}*/
li:hover,
li:focus-within {
background-color: black;
}
/*li:focus-within a {
outline: none;
}*/
.dropdown {
display: none;
background-color: #333;
position: absolute;
left: 0;
margin-top: 2px;
}
/*ul li:hover > ul,
ul li:focus-within > ul,
ul li ul:hover,
ul li ul:focus {
display: block;
visibility: visible;
}*/
.dropdown-btn:hover .dropdown {
display: block;
}
<nav role="navigation">
<ul>
<li>Chat Home</li>
<li class="dropdown-btn">
Go To <i class="fa fa-caret-down"></i>
<div class="dropdown">
<ul aria-label="submenu">
<li>Calendar</li>
<li>Big Blue</li>
</ul>
</div>
</li>
<li>Logout</li>
</ul>
</nav>
You just need to add two properties width and height to the class .dropdown i.e. in your CSS ul li ul. JSFiddle
ul li ul {
/* already mentioned styles */
width: fit-content;
width: -moz-fit-content; /* Firefox support */
height: fit-content;
height: -moz-fit-content;
}
I have a menu and submenu from one of the elements. The code is here: http://jsfiddle.net/mq5g6upe/ . I would like you to tell me how to implement vertical dropdown menu from element My project because now it not seem ok.
header.html
<header>
<div class="main">
<ul>
<li>Home</li>
<li>My projects
<ul class="my-projects-dropdown">
<li>Endless Blow
<li>Sub-2</li>
<li>Sub-3</li>
</ul>
</li>
<li>My Google Play link</li>
<li>About</li>
<li><input type="submit" class="a-login" value="Login" (click)="navigateToLogin()"></li>
</ul>
</div>
</header>
header.css
#container {
margin: 0 auto;
}
ul {
float: right;
list-style-type: none;
margin-top: 25px;
margin-right: 115px;
}
ul li {
display: inline-block;
vertical-align: middle;
}
ul li a {
text-decoration: none !important;
padding: 5px 20px;
border: 1px solid #000;
color: #000;
transition: 0.4s ease;
font-size: 20px !important;
}
ul li a:hover {
background-color: cyan;
}
ul li a.li-login {
text-decoration: none !important;
position: relative;
margin-left: 10px;
padding: 5px 10px;
color: #000;
transition: 0.4s ease;
font-size: 16px !important;
border: none;
vertical-align: middle;
line-height: normal;
}
ul li:hover > ul {
visibility: visible;
opacity: 1;
display: block;
}
ul li:nth-child(5){
margin-left: 20px;
vertical-align: middle;
line-height: normal;
}
ul ul {
display: none;
position: absolute;
}
ul ul li {
float:none;
display:list-item;
position: relative;
}
Now there are three problems. First the submenu of items seems to overlap on another elements. Second there is a distance between the menu element My projects and first element of submen. Third outside jsfiddle in production (https://jakuwegiel.web.app/home) also submenu is moved a bit to right.
I forked your fiddle. check this https://jsfiddle.net/wrtxkz0d .
Made these changes in your css. Dropdown is working.
ul li:hover > ul {
visibility: visible;
opacity: 1;
display: block;
margin:0;
padding: 0;
}
ul ul li {
float:none;
display:list-item;
position: relative;
margin: 13px 0;
}
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 have a nested <ul> within my nav ul. When the <ul> has only one element its padding is fine and the <li> fits nicely. However when I have more than one element in the nested <ul> it extends to the side of the page. I can see nothing to do with padding changes in my :first-child or anything in my css for the nested <ul>.
The nested ul with multiple elements
The nested ul with only one element
Here is the HTML:
<header id="header" class="alt">
<h1><strong>South Somerset Motocross Club</strong></h1>
<nav id="nav">
<ul>
<li>Home</li>
<li><a>Info</a>
<ul>
<li>Club Rules</li>
<li>Pre-Race Bike Check</li>
<li>Job Descriptions</li>
<li>Race Fees</li>
<li>Groups And Ages</li>
</ul>
</li>
<li><a>About Us</a>
<ul>
<li>Our Heritage</li>
<li>Committee</li>
</ul>
</li>
<li><a>News</a></li>
<li>Fixtures</li>
</ul>
</nav>
</header>
Here is the CSS:
#header nav {
height: inherit;
line-height: inherit;
position: absolute;
right: 1.25em;
top: 0;
vertical-align: middle;
}
#header nav ul {
list-style: none;
margin: 0;
padding-left: 0;
}
#header nav ul li {
color: #fff;
display: inline-block;
padding-left: 10px;
padding-right: 10px;
margin-left: 1em;
}
#header nav ul li ul {
display: none;
padding: 0;
margin: 0;
background-color: #eee;
border-radius: 4px;
}
#header nav ul li:hover ul {
display: block;
position: absolute;
}
#header nav ul li ul li {
border-radius: 4px;
margin: 0 0 0 0;
padding-left: 4px;
padding-top: 1px;
padding-right: 4px;
width: 100%;
color: black;
font-size: 12pt;
}
#header nav ul li ul li a {
color: #111;
padding: 0;
margin: 0;
display: block;
}
#header nav ul li ul li a:hover {
color: rgba(255, 255, 0, 1);
}
#header.alt nav ul li ul li a:hover {
color: rgba(255, 255, 0, 1);
}
#header.alt nav ul li ul li:hover {
background-color: #3477ff;
}
#header nav ul li ul li:hover {
background-color: #3477ff;
}
#header.alt nav ul li ul li a {
color: #000;
padding: 0;
display: block;
}
#header nav ul li a {
-moz-transition: color 0.1s ease-in-out;
-webkit-transition: color 0.1s ease-in-out;
-ms-transition: color 0.1s ease-in-out;
transition: color 0.1s ease-in-out;
color: #000;
display: inline-block;
text-decoration: none;
}
#header nav ul li a:hover {
color: #000;
}
Any ideas on how I can keep the width of the <ul> to the max length of one of the items?
Add display block in
#header nav ul li ul li {
border-radius: 4px;
margin: 0 0 0 0;
padding-left: 4px;
padding-top: 1px;
padding-right: 4px;
width: 100%;
color: black;
font-size: 12pt;
display:block;
}
here is the fiddle
Yop!
i checked Your css and the problem was here:
#header nav ul li ul li {
border-radius: 4px;
margin: 0 0 0 0;
padding-left: 4px;
padding-top: 1px;
padding-right: 4px;
width: 100%;
color: black;
font-size: 12pt;
}
Which i changed to this:
#header nav ul li ul li {
border-radius: 4px;
margin: 0 0 0 0;
padding-left: 4px;
padding-top: 1px;
padding-right: 4px;
//width: 100%;
display:table;
color: black;
font-size: 12pt;
}
So now it's getting the width of longest item :)
Hope i helped!
I want to build a menu that is CSS only. No jQuery.
I've gotten this far but can't make the menu slide in from the top. Here's a fiddle (oddly enough, it doesn't look like my menu... but all the styles are there)
Some code: The HTML:
<div class="menu">
<ul>
<li class="blue"> <a style="text-decoration: none" href="/who-we-are">Who We Are</a>
</li>
<li class="red"> <a style="text-decoration: none" href="/services">Services</a>
<ul>
<li> Post 1
</li>
<li> Post 2
</li>
<li> Post 3
</li>
</ul>
</li>
<li class="orange"><a style="text-decoration: none" href="/packages">Packages</a>
<ul>
<li> Post 1
</li>
<li> Post 2
</li>
<li> Post 3
</li>
</ul>
</li>
<li class="green"><a style="text-decoration: none" href="/contact-us">Contact</a>
</li>
</ul>
</div>
And my CSS:
.menu {
float: left;
margin-top: 0px;
}
.blue, div.item.blue div {
color: #009dc4;
}
.red, div.item.red div {
color: #fe4f00;
}
.orange, div.item.orange div {
color: #ff5958;
}
.green, div.item.green div {
color: #50c402;
}
.menu ul li a {
display: block;
height: 45px;
padding-top: 18px;
}
.menu ul li a:visited {
color: inherit;
}
.menu ul {
list-style: none;
margin: 0;
}
.menu ul li {
display: inline-block;
position: relative;
cursor: pointer;
width: 145px;
font-family: Georgia;
height: 45px;
font-size: 20px;
line-height: 22px;
font-weight: bold;
padding-left: 5px;
margin-top: 0px;
margin-right: 46px;
border-bottom: 5px solid;
z-index: 5000;
}
.menu ul li:hover {
color: #ffffff !important;
}
.menu ul li.blue:hover {
background-color: #009dc4;
border-bottom: 5px solid #009dc4;
}
.menu ul li.red:hover {
background-color: #fe4f00;
border-bottom: 5px solid #fe4f00;
}
.menu ul li.orange:hover {
background-color: #ff5958;
border-bottom: 5px solid #ff5958;
}
.menu ul li.green:hover {
background-color: #50c402;
border-bottom: 5px solid #50c402;
}
.menu ul li ul {
display: none;
opacity: 0;
visibility: hidden;
position: absolute;
top: 45px;
}
.menu ul li ul li {
display: block;
font-size: 12px;
border-bottom: none;
width: 145px;
color: #ffffff;
padding-left: 0px;
height: 34px;
line-height: normal;
position: relative;
left: -5px;
}
.menu ul li ul li a {
font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: normal;
height: auto;
padding: 10px 5px;
font-size: 12px;
background-color: #4fc7c1;
-webkit-transition: all 0.1s;
-moz-transition: all 0.1s;
-ms-transition: all 0.1s;
-o-transition: all 0.1s;
transition: all 0.1s;
}
.menu ul li ul li a:hover {
background: #a3edf5;
}
.menu ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
.menu > ul > li:last-of-type {
margin-right: 0px;
}
If someone can just help with getting the slide down to work, I'd appreciate it.
Unfortunately there is no way to animate height:0 to height:auto with CSS (as of CSS3). However there is a workaround using max-height documented here: http://css3.bradshawenterprises.com/animating_height/
With that logic I have created a simple example from your JS fiddle. All it does is set the css style max-height:0 on the drop-down <ul> element, some transitions for the max-height css attribute and then a large max-height value on menu hover.
Unfortunately the max-height must be hard-coded (cannot be auto) so we are limited here; but if you are confident that your menus will never exceed say 500px then you would simply put 500px.
Here's the fiddle:
http://jsfiddle.net/6ame5wcu/4/
All you need to do is to set max-height:0; and overflow:hidden; then add a transition on it like this:
.menu ul li ul {
max-height:0em;
overflow:hidden;
position: absolute;
top: 45px;
transition:max-height .9s ease
}
on :hover set a max-height ie max-height:600px;
.menu ul li:hover ul {
max-height:600px;
}
DEMO
Full code:
<div class="menu">
<ul>
<li class="blue"> Who We Are
</li>
<li class="red"> Services
<ul>
<li> Post 1
</li>
<li> Post 2
</li>
<li> Post 3
</li>
</ul>
</li>
<li class="orange">Packages
<ul>
<li> Post 1
</li>
<li> Post 2
</li>
<li> Post 3
</li>
</ul>
</li>
<li class="green">Contact
</li>
</ul>
</div>
css
a{text-decoration: none}
.menu {
float: left;
margin-top: 0px;
}
.blue, div.item.blue div {
color: #009dc4;
}
.red, div.item.red div {
color: #fe4f00;
}
.orange, div.item.orange div {
color: #ff5958;
}
.green, div.item.green div {
color: #50c402;
}
.menu ul li a {
display: block;
height: 45px;
padding-top: 18px;
}
.menu ul li a:visited {
color: inherit;
}
.menu ul {
list-style: none;
margin: 0;
}
.menu ul li {
display: inline-block;
position: relative;
cursor: pointer;
width: 145px;
font-family: Georgia;
height: 45px;
font-size: 20px;
line-height: 22px;
font-weight: bold;
padding-left: 5px;
margin-top: 0px;
margin-right: 46px;
border-bottom: 5px solid;
z-index: 5000;
}
.menu ul li:hover {
color: #ffffff !important;
}
.menu ul li.blue:hover {
background-color: #009dc4;
border-bottom: 5px solid #009dc4;
}
.menu ul li.red:hover {
background-color: #fe4f00;
border-bottom: 5px solid #fe4f00;
}
.menu ul li.orange:hover {
background-color: #ff5958;
border-bottom: 5px solid #ff5958;
}
.menu ul li.green:hover {
background-color: #50c402;
border-bottom: 5px solid #50c402;
}
.menu ul li ul {
max-height:0em;
overflow:hidden;
position: absolute;
top: 45px;
transition:max-height .9s ease
}
.menu ul li:hover ul {
max-height:600px;
}
.menu ul li ul li {
display: block;
font-size: 12px;
border-bottom: none;
width: 145px;
color: #ffffff;
padding-left: 0px;
height: 34px;
line-height: normal;
position: relative;
left: -5px;
}
.menu ul li ul li a {
font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: normal;
height: auto;
padding: 10px 5px;
font-size: 12px;
background-color: #4fc7c1;
-webkit-transition: all 0.1s;
-moz-transition: all 0.1s;
-ms-transition: all 0.1s;
-o-transition: all 0.1s;
transition: all 0.1s;
}
.menu ul li ul li a:hover {
background: #a3edf5;
}
.menu > ul > li:last-of-type {
margin-right: 0px;
}