Expanding drop down menu on hover using css - html

I'm trying to get a drop down menu for my navbar so that when I hover over the tabs it opens a drop down menu with more options. Here is a fiddle of my code. The very final product should look like this, for now I just want to fix the drop down on hover part of it.
Here is a snippet of code im using in css to try and achieve this:
.dropdown {
display: none
}
.navbar-list li:hover .dropdown {
display: relative;
color: white;
text-decoration: none;
}

You are trying wrong approach, please change your css part
.navbar-list li:hover .dropdown {
display: block;
color: white;
text-decoration: none;
}
<ul class="navbar-list">
<li class="navbar-tags">OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
update code

Your HTML structure is wrong, and you need to use display: block on hover, not display: relative
But re: the HTML, a ul can't be a direct child of a ul. You need to nest the dropdowns in an li. That is not only correct markup, but it allows the hover to persist when you hover over an li that has nested menus. Otherwise, you would need to use li:hover + .dropdown to show the next .dropdown menu, but your :hover will stop once your mouse moves off of the li.
Also, each ul.dropdown that is in a single nested nav element could probably just be li's of a single ul, but what you have isn't incorrect, so I didn't change that.
.dropdown {
display: none
}
.navbar-tags:hover > .dropdown {
display: block;
color: white;
text-decoration: none;
}
.navbar-list a {
color: black;
text-decoration: none;
}
.navbar-tags {
margin: 20px;
}
.navbar-tags, .dropdown {
padding: 0;
list-style-type: none;
}
<ul class="navbar-list">
<li class="navbar-tags">
OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
<li class="navbar-tags">PROGRAMS
<ul class="dropdown">
<li>Adventure Sport</li>
</ul>
<ul class="dropdown">
<li>Entertainment</li>
</ul>
<ul class="dropdown">
<li>Collegiate</li>
</ul>
<ul class="dropdown">
<li>Individual</li>
</ul>
<ul class="dropdown">
<li>Commercial</li>
</ul>
</li>
<li class="navbar-tags">RESEARCH
<ul class="dropdown">
<li>Corporate Survey</li>
</ul>
<ul class="dropdown">
<li>Individual Survey</li>
</ul>
</li>
<li class="navbar-tags">STORIES</li>
</ul>

1 ) Your HTML structure is wrong.
2) use display: block instead of display: relative.
Change your Code Like THis :
.dropdown {
display: none
}
.navbar-list li:hover > .dropdown {
display: block;
color: white;
text-decoration: none;
}
.navbar-list a {
color: black;
text-decoration: none;
}
.navbar-tags {
padding: 0;
list-style-type: none;
margin: 20px;
}
<ul class="navbar-list">
<li class="navbar-tags">OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
<li class="navbar-tags">PROGRAMS
<ul class="dropdown">
<li>Professional</li>
</ul>
<ul class="dropdown">
<li>Adventure Sport</li>
</ul>
<ul class="dropdown">
<li>Entertainment</li>
</ul>
<ul class="dropdown">
<li>Collegiate</li>
</ul>
<ul class="dropdown">
<li>Individual</li>
</ul>
<ul class="dropdown">
<li>Commercial</li>
</ul>
</li>
<li class="navbar-tags">RESEARCH
<ul class="dropdown">
<li>Corporate Survey</li>
</ul>
<ul class="dropdown">
<li>Individual Survey</li>
</ul>
</li>
<li class="navbar-tags">STORIES</li>
</ul>

Related

Avoid multiple uls to create a "diagonal" list

I am trying to create a slanted/diagonal list, but want to avoid having several nested uls, is there a way to do this, here is the code I have so far:
<ul className={style.steps}>
<li>
<p className={style.step}>Lorem</p>
<ul className={style.steps}>
<li>
<p className={style.step}>Ipsum</p>
<ul className={style.steps}>
<li>
<p className={style.step}>dolor</p>
<ul className={style.steps}>
<li>
<p className={style.step}>sit</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
If you can update how the code outputs, you don't need nth-selectors, especially if the list is dynamic and changes length, you can use CSS vars.
You can set a default value using :root, then you can add the custom property value on each li element --index: number. You can change the CSS var on the element to whatever you need.
:root {
--margin-value: 10px;
}
ul,
li {
margin: 0;
padding: 0;
}
li {
margin-left: calc(var(--index) * var(--margin-value));
}
<ul>
<li style="--index: 1">One</li>
<li style="--index: 2">Two</li>
<li style="--index: 3">Three</li>
<li style="--index: 4">Four</li>
<li style="--index: 5">Five</li>
</ul>
The :nth-child selector would allow you to set different spacing rules on different <li>s within a single list.
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
ul {
list-style: none;
}
ul li:nth-child(1) {
margin-left: 0;
}
ul li:nth-child(2) {
margin-left: 30px;
}
ul li:nth-child(3) {
margin-left: 60px;
}
ul li:nth-child(4) {
margin-left: 90px;
}
<ul>
<li>Lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
</ul>
You did not use the javascript tag, but it could be handy here , so not really an answer but another approach.
Demo below:
var elMargin = 0;
for (let e of document.getElementsByClassName('indent')) {
elMargin = elMargin + 1;
e.style.marginLeft = elMargin + 'em';
}
ul {counter-reset:lis}
li:before{counter-increment:lis;content:counter(lis);
<ul>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
</ul>

Display Submenus on hover and open link on Click

I created a wordpress page. On the header menu when I click the page, it takes me to that page content. But when I hover it, nothing happens. I want the sub menus to appear on hover.
.dropdown {
position: relative;
display: inline-block;
}
.dropdown:hover {
display: block;
}
.nav navbar-nav navbar-left:hover {
display: block;
}
<ul id="menu-header-menu-left" class="nav navbar-nav navbar-left"> Dental Services</li>
<li class="dropdown ">
<ul class="dropdown-menu total-11 sub-left">
<li>BLEACHING</li>
<li>BONE GRAFT</li>
<li>CHILDREN DENTISTRY</li>
<li>CLEANING</li>
</ul>
</li>
</ul>
The HTML code for submenu was wrong. You need to start submenu <ul> inside parent menu li.
Here is an example:
.dropdown-menu {
position: relative;
display: none;
}
#submenu:hover > ul {
display: block;
}
.nav navbar-nav navbar-left:hover {
display:block;
}
<ul id="menu-header-menu-left" class="nav navbar-nav navbar-left">
<li id="submenu">
Dental Services
<ul class="dropdown-menu total-11 sub-left">
<li>BLEACHING</li>
<li>BONE GRAFT</li>
<li>CHILDREN DENTISTRY</li>
<li>CLEANING</li>
</ul>
</li>
</ul>
css
.dropdown-menu {position: relative; display: none;} #submenu:hover ul {display: block;} .nav.navbar-nav.navbar-left:hover {display:block;}
Html
<ul id="menu-header-menu-left" class="nav navbar-nav navbar-left">
<ul class="dropdown-menu total-11 sub-left">
<li>BLEACHING</li>
<li>BONE GRAFT</li>
<li>CHILDREN DENTISTRY</li>
<li>CLEANING</li>
</ul>

Create a nested ordered list in a tabular style with css

Hi I am looking to display my ordered list this:
so the first node and the first nested node appear on the top line and the remaining nested nodes appear under the 2nd column (under red).
Apples Red
Green
Yellow
Banana Yellow
html:
<ul class="lst" id="list_Apple">
<li>Apple</li>
<ul>
<li id="Apple">Red</li>
<li id="Apple">Green</li>
<li id="Apple">Yellow</li>
</ul>
</ul>
<ul class="lst" id="list_Banana">
<li>Banana</li>
<ul>
<li id="Banana">Yellow</li>
</ul>
</ul>
There is a slight mistake in your html. The <ul> tag should be inside a <li>.
HTML:
<ul class="lst" id="list_Apple">
<li>Apple</li>
<li>
<ul>
<li id="Apple">Red</li>
<li id="Apple">Green</li>
<li id="Apple">Yellow</li>
</ul>
</li>
</ul>
<ul class="lst" id="list_Banana">
<li>Banana</li>
<li>
<ul>
<li id="Banana">Yellow</li>
</ul>
</li>
</ul>
And add this CSS:
.lst {
clear: both;
}
.lst li {
list-style: none;
}
.lst > li {
float: left;
}
Here's a Fiddle: http://jsfiddle.net/rayg8ua9/1/
lithanks... Yes I missed the li tag.
.lst {
clear: both;
padding-top: 10px;
}
.lst li {
list-style: none;
width:100px;
}
.lst > li {
float: left;
}

Add submenu to existing menu

Hi I have a basic menu for which I would like to add a submenu, that appears only when a certain menu link is hovered. Everything I have tried does not hide the submenu when a link is not hovered. Here is my code:
CSS
.navmenu{
float:right;
font-size: 13px;
font-weight:400;
text-transform: uppercase;
}
.navmenu li{
position: relative;
display: inline-block;
float: left;
}
.navmenu li a{
text-decoration:none;
color:#eee;
padding:15px 37px 19px 37px;
}
.navmenu li a:hover{
background:#36332e;
}
.active a{
background:#36332e;
}
HTML
<ul class="navmenu">
<li class="active">Home</li>
<li>About Us
<ul>
<li>Sub Link 1</li>
<li>SubLink 2</li>
</ul>
</li>
<li>Testimonials</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
You need to initially hide the menu:
.navmenu li ul { display: none; }
and then display it when you hover over the nav item:
.navmenu li:hover ul { display: none; }
You should also be careful about defining styles that target .navmenu li or .navmenu li a because those will also target your submenu. You should instead use child selectors, giving you more control over the non-submenu links, so your selectors will look like:
.navmenu > li
.navmenu > li > a
I've encorperated some of those changes into this JSFiddle to get you started:
http://jsfiddle.net/Wexcode/B5P26/
Edit:
This is actually going to lose it's hover state when you hover over the submenu links:
.navmenu > li > a:hover {
background:#36332e;
}
Instead, you should do this:
.navmenu ul { position: absolute; }
.navmenu > li:hover { background: #e6332e; }
.navmenu > li > a { display: block; }
Since the <ul> is nested inside the <li> element, you won't lose the hover state when you hover over the submenu links. I updated the fiddle to reflect these changes.
<ul class="navmenu">
<li class="active">Home</li>
<li>About Us
<ul>
<li>
Sub Link 1
<ul>
</li> <a href=# >hi hi hi</a>
<ul>
<li>hello hello hello</li>
<li>hello hello hello</li>
<li>hello hello hello</li>
</ul>
</li>
</li><a href=# >hi hi hi</a> </li>
</li> <a href=# >hi hi hi</a> </li>
</ul>
</li>
<li>SubLink 2</li>
</ul>
</li>
<li>Testimonials</li>
<li>Services</li>
<li>Contact Us</li>
</ul>

Vertical Gap at End of List?

I have a vertical UL list on a html page, with a sublist inside of it. At the end of the sublist, it has an unwanted gap, like a linebreak, though I can't seem to find anything in my css or html that would cause it (and my attempts at trying to get it to go away aren't working very well).
Here's what it looks like;
1. Item
2. Item
1. Sub Item
2. Sub Item
3. Item
4. Item
My html code:
<ul class="fic">
<li class="fic"><a href="">item</li>
<li class="fic"><a href="">item
<ul class="fic">
<li class="fic">item</li>
<li class="fic">item</li>
</ul>
</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item
<ul class="fic">
<li class="fic">item</li>
</ul></li>
</ul>
And my css code that I was trying to use;
.fic ul {
padding: 0px;
margin: 0px;
}
And since my template came with menu code for the ul used in the navigation bar, here's that;
#menu ul {
display: block;
width: 778px;
margin: 0em auto;
list-style: none;
padding-left: 2.5em;
}
#menu li {
display: inline;
}
#menu li a {
color: #ffffff;
font-weight: bold;
font-size: 1.2em;
text-decoration: none;
padding: 0.25em 0.75em 0.25em 0.75em;
}
#menu li a:hover {
background: #342117 url('images/hover.gif') top left repeat-x;
color: #fffdc6;
}
html code for navigation menu;
<div id="menu">
<ul>
<li class="first">Home</li>
<li>Fan Art</li>
<li>Fan Fiction</li>
<li>Fan Videos</li>
<li>Other</li>
<li>Forum</li>
</ul>
</div>
Help would be appreciated, thank you.
The menu styles are the culprit, need full code