Having trouble getting a smooth dropdown menu working with css - html

Hi guys I've been having trouble getting my sub menu dropdown working with CSS. I'm trying to add a smooth transition appearance but at the moment the menu doesn't even display when I hover. I'm sure it's something small that I'm missing but I just can't seem to figure it out where the problem is. Here's the code:
#main-navigation ul.folder-child{
opacity: 0;
visibility: hidden;
-moz-transition: height 0.3s ease-in-out;
-ms-transition: height 0.3s ease-in-out;
-o-transition: height 0.3s ease-in-out;
-webkit-transition: height 0.3s ease-in-out;
transition: height 0.3s ease-in-out;
}
#main-navigation li:hover ul.folder-child{
opacity: 1;
visibility: visible;
top: 50px;
}
I'd appreciate any help anyone can offer. Thanks in advance!

You are defining transition for only height and there is no css rule defined for height. here is your solution.
Please Note: for transition of height property, you need to define height on normal and hover states.
ul.folder-child {
width: 180px;
height: 0;
overflow: hidden;
opacity: 0;
visibility: hidden;
position:absolute;
top: 100%;
left: 0;
transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-webkit-transition: all 0.4s ease-in-out;
}
#main-navigation li:hover > ul.folder-child {
height: 100px;
opacity: 1;
visibility: visible;
}
Check out http://jsfiddle.net/logiccanvas/vWDvy/480/

Maybe check your browser,IE does not support all CSS3 functions.

Maybe it is better to use display: none and display: block instead of opacity and visibility;
<ul>
<li>
Item #1
<ul>
<li>Sub-Item #1</li>
<li>Sub-Item #2</li>
<li>Sub-Item #3</li>
</ul>
</li>
<li>
Item #2
<ul>
<li>Sub-Item #4</li>
<li>Sub-Item #5</li>
<li>Sub-Item #6</li>
</ul>
</li>
<li>
Item #3
<ul>
<li>Sub-Item #7</li>
<li>Sub-Item #8</li>
<li>Sub-Item #9</li>
</ul>
</li>
</ul>
ul > li {display: block; float: left; margin-right: 10px; position: relative; background: Red; padding: 0.5em; line-height: 1em}
ul ul {display: none; width: 150px; position:absolute; top: 2em; left: 0}
ul ul > li {float: none;}
ul > li:hover > ul,
ul > a:hover + ul {display: block}
http://jsfiddle.net/spliter/vWDvy/
WORK ON IE ASWELL!

Related

Hover in css covering menu above it

I have a menu with a drop down that works nicely but the issue I noticed is if you hover a link with a child element, it opens the menu, but the link then become unclickable on the menu on the bottom half.
<div class="desktop_navigation">
<ul>
<li>Link 1</li>
<li>Link 2
<ul>
<li>Link 2 child</li>
</ul>
</li>
<li>Link 3</li>
</ul>
</div>
That's the structure for the menu. The child elements are sub <ul> of the main <li> and then hidden using CSS until their parent <li> is hovered.
The following jFiddle will include all the CSS I am using and a working example of the issue I am currently having:
https://jsfiddle.net/nrzfa49s/
Your .desktop_navigation ul li ul is inheriting the padding-top: 30px from its parent ul (.desktop_navigation ul) which is covering the parent link (Link 2) making it unclickable.
To fix your problem, update these styles:
.desktop_navigation ul li ul {
list-style: none;
display: none;
padding-top: 0; /*remove the 30px padding*/
}
.desktop_navigation ul li:hover ul {
display: block;
position: absolute;
top: 100%; /*set your top value to be more dynamic based on the height of the parent*/
z-index: 890;
}
Here is a fiddle demoing this solution.
Normally when you style menus like this it is recommended to use the > when styling menu elements because of the nesting (i.e. .desktop_navigation > ul this will prevent the child ul from inheriting the padding)
Took some time, but the issue is here:
.desktop_navigation ul {
list-style: none;
padding: 0;
margin: 0;
padding-top: 30px; //remove this line
}
You only want the padding-top for the ul's to be on the parent/top-level menu. Then if you remove the top attribute from the nested menus, they will display after the link in the top-level menu.
.desktop_navigation a {
color: #ccc;
text-decoration: none;
display: inline-block;
padding: 12px;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li:hover a {
color: #fff;
background: #444;
text-decoration: none;
display: inline-block;
z-index: 1002;
padding: 12px;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li ul li a:link,
.desktop_navigation ul li ul li a:visited,
.desktop_navigation ul li ul li a:active {
z-index: 1001;
width: 100%;
display: block;
color: #444;
background: #fff;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li ul li a:hover {
width: 100%;
display: block;
color: #111;
z-index: 1002;
background: #ccc;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul {
list-style: none;
padding: 0;
margin: 0;
}
.desktop_navigation > ul {
padding-top: 30px;
}
.desktop_navigation ul li {
display: inline-block;
position: relative;
padding: 0;
margin: 0;
z-index: 1002;
}
.desktop_navigation ul li ul {
list-style: none;
display: none;
}
.desktop_navigation ul li:hover ul {
display: block;
position: absolute;
z-index: 890;
}
.desktop_navigation ul li ul li {
float: none;
position: relative;
min-width: 180px;
z-index: 890;
}
<div class="desktop_navigation">
<ul>
<li>Link 1</li>
<li>
Link 2
<ul>
<li>Link 2 child</li>
</ul>
</li>
<li>Link 3</li>
</ul>
</div>

Drop Down menu hidden when put inside div with overflow auto

I have a dropdown menu inside a div container with overflow-x: auto, the problem that whenever I add the overflow property to the container to enable the horizontal scrolling you need to scroll vertically to see the dropped menu !!which is not what I want, is there a solution to make the dropdown menu overlap it's container with keeping the ability to scroll the navbar horizontally inside it's container ?
GIF of the Issue :
Another GIF without overflow which solve the problem but disable the horizontal scroll bar in the container
JSFIDDLE
I'm looking for a pure CSS solution.
This is not possible using pure css because of the position: relative at your nav ul li level.
With the position: relative, the child container will always be hidden with an overflow: auto & fixed height.
A simple javascript solution can be helpful that will work on the dynamic positioning of the menu items.
Refer code:
function scrollLeft() {
$(".wrapit").css("left", function() {
return $(this).parent().position().left;
});
}
$(".container").scroll(function() {
scrollLeft();
});
scrollLeft();
nav ul {
background: #ddd;
font-size: 0;
list-style: none;
margin: 0;
padding: 0;
text-align: center;
width: 100%;
}
.container {
width: 500px;
height: 67px;
background-color: #ffffff;
overflow-x: auto;
}
.cf {
white-space: nowrap;
}
.wrapit {
white-space: normal;
}
nav li {
display: inline-block;
margin: 0;
padding: 0;
/* position: relative; */
min-width: 33%;
}
nav a {
background: #ddd;
color: #444;
display: block;
font: bold 16px/50px sans-serif;
padding: 0 25px;
text-align: center;
text-decoration: none;
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
nav .dropdown:after {
content: ' \25bc';
}
nav li:hover a {
background: #ccc;
}
nav li ul {
left: 0;
opacity: 0;
position: absolute;
width: 165px;
top: 35px;
visibility: hidden;
z-index: 1;
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
nav li:hover ul {
opacity: 1;
top: 50px;
visibility: visible;
}
nav li ul li {
width: 100%;
}
nav li ul a:hover {
background: #bbb;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<nav>
<ul class="cf">
<li>Menu Item 1</li>
<li><a class="dropdown" href="#">Menu Item 2</a>
<ul class="wrapit">
<li>Sub-menu Item 1</li>
<li>Sub-menu Item 2</li>
<li>Sub-menu Item 3</li>
</ul>
</li>
<li>Menu Item 3</li>
<li>Menu Item 3</li>
<li>Menu Item 3</li>
</ul>
</nav>
</div>

CSS transition and property delay not working on mouseout

I've made a hidden vertical menu, which show on hover/active. I'm using the transition property with a delay.
When mouse is hover, menu is showing with the delay
When mouse is out, menu is going/draw back, but without any delay.
li {
display: block;
padding: 10px;
}
ul ul {
max-height: 0em;
overflow: hidden;
}
ul > li:hover ul,
ul > li:active ul {
max-height: 10em;
transition: 1000ms all ease 500ms;
}
<ul>
<li>Option1
<ul>
<li>OptionA</li>
<li>OptionB</li>
</ul>
</li>
<li>Option2</li>
</ul>
How can I have a delay/duration, moving back to initial value ?
PS: I can't use JavaScript.
Add the transition to the element, not the state. When the state is over (leaving the element with your mouse) the CSS takes no more effect.
Updated. Snippet now opens instantly and has a closing delay of 2000ms. By increasing the hover state delay you can increase the opening delay.
li {
display: block;
padding: 10px;
}
ul ul {
max-height: 0em;
overflow: hidden;
-webkit-transition: 1000ms all ease 2000ms;
-moz-transition: 1000ms all ease 2000ms;
-ms-transition: 1000ms all ease 2000ms;
-o-transition: 1000ms all ease 2000ms;
transition: 1000ms all ease 2000ms;
}
ul > li:hover ul,
ul > li:focus ul,
ul > li:active ul {
max-height: 10em;
-webkit-transition-delay: 0s;
-moz-transition-delay: 0s;
-ms-transition-delay: 0s;
-o-transition-delay: 0s;
transition-delay: 0s;
}
<ul>
<li>Option1
<ul>
<li>OptionA</li>
<li>OptionB</li>
</ul>
</li>
<li>Option2</li>
</ul>
Note: You can also use transition: 1000ms max-height ease 500ms; in this case for slightly better performance. Not significant on a single transition but they might add up.
You have to set transition to the element when not hovered too. Try the snippet below.
li {
display: block;
padding: 10px;
}
ul ul {
max-height: 0em;
overflow: hidden;
transition: 400ms all ease 500ms;
}
ul > li:hover ul,
ul > li:active ul {
max-height: 10em;
transition: 1000ms all ease 500ms;
}
<ul>
<li>Option1
<ul>
<li>OptionA</li>
<li>OptionB</li>
</ul>
</li>
<li>Option2</li>
</ul>
Simply add the transition to the original properties, and you are good to go, because you can add a transition-delay there. This is why you should specify both the state, and the original elements. Because you wish to include the delay only on exiting the :hover/:active/:focus state.
li {
display: block;
padding: 10px;
}
ul ul {
max-height: 0em;
overflow: hidden;
}
ul > li ul,
ul > li ul {
transition: all ease 500ms;
transition-delay: 2s;
}
ul > li:hover ul,
ul > li:active ul,
ul > li:focus ul {
max-height: 10em;
transition: 1000ms all ease 500ms;
}
<ul>
<li>Option1
<ul>
<li>OptionA</li>
<li>OptionB</li>
</ul>
</li>
<li>Option2</li>
</ul>

How to edit this nav to make it a click nav instead of a hover nav?

This is the following snippet I want to edit.
http://www.cssscript.com/simple-accordion-menu-with-css3-transitions/
heres the demo http://www.cssscript.com/demo/simple-accordion-menu-with-css3-transitions/
I want to click to dropdown and it stays dropped down until I click on another menu or the same one again.
Any idea on how I would be able to do this?
Got a solution.
Here you go: fiddle
.menu li a:focus + ul li a{ styles here}
I've also added a jQuery equivalent of a JavaScript return false on links (anchors) - because by default, an anchor with a # link will reload the page if it can't find that jump point. If your code allows for it, you can use at the first level of the menu some other element besides an anchor and not have need for the JavaScript.
Use the target pseudo-class in place of hover. Note I've added an ID to each sub-menu and used the top level nav item to link to that section.
.menu {
margin: 0 auto;
padding: 0;
width: 350px;
}
.menu li { list-style: none; }
.menu li a {
display: table;
margin-top: 1px;
padding: 14px 10px;
width: 100%;
background: #337D88;
text-decoration: none;
text-align: left;
vertical-align: middle;
color: #fff;
overflow: hidden;
-webkit-transition-property: background;
-webkit-transition-duration: 0.4s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.4s;
transition-timing-function: ease-out;
}
.menu > li:first-child a { margin-top: 0; }
.menu li a:hover {
background: #4AADBB;
-webkit-transition-property: background;
-webkit-transition-duration: 0.2s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.2s;
transition-timing-function: ease-out;
}
.menu li ul {
margin: 0;
padding: 0;
}
.menu li li a {
display: block;
margin-top: 0;
padding: 0 10px;
height: 0;
background: #C6DDD9;
color: #1F3D39;
-webkit-transition-property: all;
-webkit-transition-duration: 0.5s;
-webkit-transition-timing-function: ease-out;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: ease-out;
}
/*This selector has changed*/
.menu > li > ul:target li a {
display: table;
margin-top: 1px;
padding: 10px;
width: 100%;
height: 1em;
-webkit-transition-property: all;
-webkit-transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-property: all;
transition-duration: 0.3s;
transition-timing-function: ease-out;
}
/*This selector has changed*/
.menu > li > ul:target li a:hover {
background: #A4CAC8;
-webkit-transition-property: background;
-webkit-transition-duration: 0.2s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.2s;
transition-timing-function: ease-out;
}
<nav id="menu_box">
<ul class="menu">
<li> Menu 1 <!-- Note the href change here -->
<ul id="sub1"><!-- Note the ID here -->
<li>Menu 1.1</li>
<li>Menu 1.2</li>
</ul>
</li>
<li> Menu 2
<ul id="sub2">
<li>Menu 2.1</li>
<li>Menu 2.2</li>
<li>Menu 2.3</li>
</ul>
</li>
<li> Menu 3
<ul id="sub3">
<li>Menu 3.1</li>
<li>Menu 3.2</li>
<li>Menu 3.3</li>
<li>Menu 3.4</li>
</ul>
</li>
<li> Menu 4 </li>
</ul>
</nav>

Pure CSS dropdown triggering on hover of wrong element

So I have a pure css dropdown menu, but I'm having one problem. The dropdown works when you hover over a link, but it's also triggering when you hover over the actual container that contains the dropdown portion. Here's a jsfiddle with the code. http://jsfiddle.net/9BRac/
<div class="nav-con">
<nav role='navigation' id="nav">
<ul>
<li id="linked">Home</li>
<li>About
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
<li>Clients
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
<li>Contact Us
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
</ul>
</nav>
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
nav {
width: 100%;
height: 60px;
background-color:black;
border-bottom: #ffd600 solid 10px;
margin: 0 auto;
}
nav ul {
position:inheirt;
}
nav ul li {
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
width: 80px;
height:60px;
margin-left:10px;
display:;
float: left;
line-height: 60px;
text-align:center;
list-style:none;
position:inherit;
color: white;
text-decoration:none;
}
nav ul li:hover {
width: 80px;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
height:60px;
margin-left:10px;
background-color:#ffd600;
float: left;
line-height: 60px;
text-align:center;
list-style:none;
position:inherit;
color: white;
text-decoration:none;
}
nav ul li a {
color: white;
text-decoration:none;
}
html {
background-color: #f2f2f2;
}
#dropdown {
width:100%;
height:200px;
opacity: 0.8;
background-color:black;
display:none;
}
.dropdown {
margin-top:10px;
}
.dropdown li {
width: 300px;
background-color: #ffd600;
}
nav ul ul {
opacity: 0;
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in
}
nav ul li:hover > ul {
opacity: 0.8;
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
}
nav ul ul li {
opacity: 1.0 !important;
}
nav ul ul li:hover {
width: 350px !important;
}
Use the following
/* Your dropdown */
nav ul ul {
opacity: 0;
visibility:hidden;
transition: opacity .6s ease-in-out;
-moz-transition: opacity .6s ease-in-out;
-o-transition: opacity .6s ease-in-out;
-webkit-transition: opacity .6s ease-in
}
/* Code to display your dropdown */
nav ul li:hover > ul {
opacity: 0.8;
visibility:visible;
}
For a working example go here http://jsfiddle.net/DanielApt/9BRac/12/
Explanation:
Your .dropdown still exists, it just has an opacity of 0.
Whenever you hover over this “invisible” .dropdown you trigger the hover styling for its parent li, which leads to showing .dropdown again.
The solution: set a visibility of hidden for your .dropdown, and set the visibility to visible only when you hover over the parent li
Also only use the transition on the opacity property, then visibility is changed immediately, but the opacity transitions smoothly.
PS: If you're curious why I'm not using display:none, it is because you can't transition display (source for the solution using visibiliy
Try using visibility:hidden instead of editing just the opacity on hover like this:
http://jsfiddle.net/9BRac/18/
Now you can edit a bit the transitions timing to make it smoother.
As well the transition property could be used on opacity only and not on all properties.