HTML CSS add dropdown menu - html

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

Related

Dropdown isn't showing everything in CSS

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

Css dropdown navbar issue

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

CSS Transition in a nested list

I have a vertical list of navigation icons, with a sub-menu (under the News tab). I want this submenu to transition from a width 0 to its full width (150px) when I hover over its parent option on the main navigation menu. Currently, the transition is not happening - it is going straight to the full width with no transition.
I pasted all the relevant code (parts of the css that are important for the transition are at the bottom below the marked point).
HTML:
<div id="nav">
<ul>
<li>Home</li>
<li>News
<ul>
<li>Current</li>
<li>Archive</li>
</ul></li>
<li>History</li>
</ul>
</div>
CSS:
#nav {
height: auto;
width: 150px;
float: left;
}
#nav a {
display: block;
padding: 6px 0px;
text-indent: 5px;
color: #ffffff;
font-size: 12px;
text-decoration: none;
border: 3px solid;
border-color: #9c6741 #663918 #572e11 #96643f;
}
#nav ul {
padding: 0px;
margin-top: 0px;
}
#nav li {
list-style: none;
float: left;
width: 150px; /*150-2 for border*/
background-color: #8f5f3c;
}
#nav ul li:hover {
background-color: #7d5334;
}
#nav ul li:hover a {
border-color: #8f5e39 #663918 #572e11 #8a5b38;
}
#nav ul li ul {
display: none;
position: absolute;
margin-top: -33px;
margin-left: 150px;
padding: 0;
}
#nav ul li:hover ul {
display: block;
}
/*code for transition begins here*/
#nav ul li ul li {
float: none;
overflow: hidden;
width: 0px;
transition: width 1s ease-in;
}
#nav ul li:hover ul li {
width: 148px;
}
You can't call an animation on something that is invisible.
https://jsfiddle.net/scLhp54w/1/
#nav ul li ul {
/*display: none; */
position: absolute;
margin-top: -33px;
margin-left: 150px;
padding: 0;
}

Hovering over menu and sub menu

I have a vertical menu some of which have sub menu displayed to the right side. When I hover first menu and sub menus of it, it displays the second set of sub menu belonging to third menu.
<body>
<ul class="menu">
<li>Population and Socio-Economic Atlas
<ul>
<li>Population
<ul>
<li>Population Density</li>
<li>Population By Sex</li>
<li>Population By Age</li>
</ul>
</li>
<li>Education</li>
<li>Agriculture
<ul>
<li>Agricultural Land</li>
<li>Agriculture Holding</li>
<li>Agriculture By Type</li>
</ul>
</li>
</ul>
</li>
<li>Level1 Menu2</li>
<li>CA Election 2070
<ul>
<li>Districts</li>
<li>Constituencies</li>
<li>Result
<ul>
<li>District Map</li>
<li>Constituencies Map</li>
<li>Election Result 2070</li>
</ul>
</li>
</ul>
</li>
<li>To Be Continued</li>
</ul>
</body>
my CSS below:
.menu,
.menu ul,
.menu li,
.menu a {
margin: 5px;
padding: 0;
}
.menu {
height: 250px;
width: 300px;
background: #FFCC99;
}
.menu li {
width:100%;
height:25px;
text-align:left;
display: block;
padding: 0 0px;
margin: 3px 0px;
line-height: 25px;
border-bottom: 1px solid #393942;
}
.menu li a {
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
text-decoration: none;
font-size: 14px;
}
.menu li:hover > a { color: #FF0000; }
/* second level menu */
.menu ul {
position: relative;
width: 200px;
top: -26px;
left: 295px;
opacity: 0;
background: #CCCCCC;
}
.menu li:hover > ul { opacity: 1; }
.menu ul li {
height: 25px;
overflow: hidden;
padding: 0 0px;
width: 200px;
padding: 0 0px;
margin: 6px 0px;
border: none;
border-bottom: 1px solid #353539;
}
.menu li:hover > ul li {
height: 25px;
overflow: visible;
padding: 0 0px;
}
.menu ul li a {
font-family: Helvetica, Arial;
font-style: italic;
font-weight: normal;
font-size: 12px;
}
.menu ul li:last-child a { border: none; }
/* third level menu */
.menu ul ul {
position: relative;
width: 200px;
top: -26px;
left: 195px;
opacity: 0;
background: #00FF00;
}
.menu ul ul:hover > ul { opacity: 1; }
.menu ul ul li {
height: 25px;
overflow: hidden;
padding: 0 0px;
width: 200px;
padding: 0 0px;
margin: 6px 0px;
border: none;
border-bottom: 1px solid #0000FF;
}
.menu ul ul ul:hover > ul {
height: 25px;
overflow: visible;
padding: 0 0px;
}
.menu ul ul li a {
font-family: Helvetica, Arial;
font-style: italic;
font-size: 12px;
}
.menu ul li:last-child a { border: none; }
How to solve this problem?
You need to use display: block/none; instead of overflow: hidden/visible to hide/unhide your sub-menus.
Here's the fiddle
overflow only affects visibility of the element. The element still exists in the DOM and responds to/ triggers all events associated with it. Hence, the issue.
That happens because you use opacity to hide sub menus. When opacity is 0 then sub menu is still on page, and receives mouse events. Submenus added to lower elements, are higher in visibility stack, and that reason why they steal hover. Instead of opacity use display.
Fiddle: http://jsfiddle.net/mL3eD/

Sub menu won't display

I have a hover drop down menu on my website. It displays ok until you hover over. The submenu appears but before you can click on a link, the sub menu vanishes.
Here is the HTML -
<div id="top-nav"><div id="nav">
<nav>
<ul id="menu" style="list-style-type: none;">
<li id="sub">Artists
<ul>
<li>Banks</li>
<li>Lil Silva</li>
<li>Frances and the Lights</li>
<li>Jim-E Stack</li>
</ul>
</li>
<li>Night</li>
<li>Info</li>
</ul>
</nav>
</div>
</div>
And here is the CSS -
#nav {
text-align:center;
list-style: none;
}
ul#menu {
background-color: #FFFFFF;
list-style: none outside none;
margin: 0 auto;
padding-bottom: 0px;
padding-top: 0px;
text-align: center;
width: 300px;
}
ul#menu:after {
content: "";
background-color: #FFFFFF;
height: 10px;
width: 100%;
display: block;
position: absolute;
left: 0;
margin-top: 20px;
}
ul#menu li {
float: left;
}
ul#menu li a {
color: #666666;
font-size: 12px;
margin: 0;
padding: 0px 35px;
text-decoration: none;
}
ul#menu li a:hover {
background-color: #ccc;
}
a.selected-page, ul#menu a.selected-page:hover {
background-color: #FFFFFF;
}
li#sub ul {
display: none;
position: absolute;
background-color: #FFFFFF;
z-index: 22222;
margin-top: 4px;
overflow: hidden;
}
li#sub ul li {
display: block;
float: none;
border-top-style: none;
border-width: 2px;
border-color: #FFFFFF;
text-align: left;
padding-top: 5px;
padding-bottom: 5px;
}
ul#menu li#sub:hover ul {
display: block;
}
ul#menu li#sub ul li:hover {
background-color: #FFFFFF;
}
What am I doing wrong? Any help is greatly appreciated!
the problem is the following line of your CSS code:
li#sub ul {
...
margin-top: 4px;
...
}
Just remove this margin-top and your drop down menu will work properly.
In your example there is a 4px margin space between the "Artists"-link and the drop down menu below. And if your cursors leaves the link and enters this "margin area", the browser interprets it as un-hovering the link - and hides the drop down.
Like dalucks said, remove the top margin from your CSS:
li#sub ul {
margin-top:0;
}
Also, reduce the left margin/paddings from the submenu UL and/or LI children. With the original large values (35px) the menu could be hovered over a lot of invisible space.
ul#menu li ul li a {
padding-left:5px;
}
Fiddle: http://jsfiddle.net/LXwTr/
Add Padding: 0 to this
li#sub ul {
padding: 0;
}
http://jsfiddle.net/tdQmE/