Drop Down menu hidden when put inside div with overflow auto - html

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>

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>

Alignment of third level navigation in centered navigation menu out in IE

I have created a drop-down menu which works fine in Firefox but the alignment of the third level dropdown menu is out in IE, and I can't work out why.
The HTML:
<nav class="main-navigation clearfix">
<ul class=" main-nav-ul">
<li>Main Link 1</li>
<li>Dropdown
<ul>
<li>Level 2 Item 1</li>
<li>Level 2 Item 2
<ul>Level 3 Item 1
</ul>
</li>
<li>Level 2 Item 3</li>
</ul>
</li>
<li>Main Link 3</li>
</ul>
</nav>
And the CSS from styles.less file:
.main-navigation {
text-align: center;
font-family: #body-font;
}
.main-navigation ul {
li {
display: inline;
text-align: center;
position: relative;
a {
color: lighten(#dark-color, 10%);
display: inline-block;
font-size: 14px;
font-weight: bold;
height: 42px;
line-height: 42px;
margin-right: 14px;
position: relative;
&:hover {
color: black;
}
&::before {
content: '|';
position: absolute;
font-size: 10px;
right: -1em;
top: -1px;
color: lighten(#dark-color, 50%);
}
} //end a
ul {
opacity: 0;
filter: alpha(opacity=0);
visibility: hidden;
-webkit-transition: opacity 0.2s ease-in;
-moz-transition: opacity 0.2s ease-in;
-o-transition: opacity 0.2s ease-in;
transition: opacity 0.2s ease-in;
background: #light-text-color;
border: 1px solid darken(#light-text-color, 10%);
border-bottom: 0;
position: absolute;
left: -49px;
li {
float: none;
white-space: nowrap;
position: relative;
a {
padding: 0 14px;
margin-right: 0;
border-bottom: 1px solid darken(#light-text-color, 10%);
&::before {
content: '';
}
} //end a
ul {
position: absolute;
left: 92px;
top: -15px;
opacity: 0;
filter: alpha(opacity=0);
visibility: hidden;
-webkit-transition: opacity 0.2s ease-in;
-moz-transition: opacity 0.2s ease-in;
-o-transition: opacity 0.2s ease-in;
transition: opacity 0.2s ease-in;
} //end 3rd level ul
} //end 2nd level li
} //end ul
&:hover > ul {
opacity: 10;
filter: alpha(opacity=100);
visibility: visible;
}
} //end li
} //end main-navigation ul
I am using bootstrap CSS as well. I'm still quite new to CSS so my apologies if coding isn't so good!
Thanks

Having trouble getting a smooth dropdown menu working with css

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!

Expand and collapse responsive menu

What is the best way I can make this menu default collapse when at a mobile size and use a toggle to collapse and expand.
I would prefer this to be CSS only but am unsure how to do it.
This is the source I am working from.
http://www.script-tutorials.com/demos/335/index.html
/* common and top level styles */
#nav span {
display: none;
}
#nav, #nav ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
#nav {
background-color: #F5F5F5;
border-bottom: 5px solid #333333;
float: left;
margin-left: 1%;
margin-right: 1%;
position: relative;
width: 98%;
}
#nav ul.subs {
background-color: #FFFFFF;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
color: #333333;
display: none;
left: 0;
padding: 2%;
position: absolute;
top: 54px;
width: 96%;
}
#nav > li {
border-bottom: 5px solid transparent;
float: left;
margin-bottom: -5px;
text-align: left;
-moz-transition: all 300ms ease-in-out 0s;
-ms-transition: all 300ms ease-in-out 0s;
-o-transition: all 300ms ease-in-out 0s;
-webkit-transition: all 300ms ease-in-out 0s;
transition: all 300ms ease-in-out 0s;
}
#nav li a {
display: block;
text-decoration: none;
-moz-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-ms-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-o-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-webkit-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
white-space: normal;
}
#nav > li > a {
color: #333333;
display: block;
font-size: 1.3em;
line-height: 49px;
padding: 0 15px;
text-transform: uppercase;
}
#nav > li:hover > a, #nav > a:hover {
background-color: #F55856;
color: #FFFFFF;
}
#nav li.active > a {
background-color: #333333;
color: #FFFFFF;
}
/* submenu */
#nav li:hover ul.subs {
display: block;
}
#nav ul.subs > li {
display: inline-block;
float: none;
padding: 10px 1%;
vertical-align: top;
width: 33%;
}
#nav ul.subs > li a {
color: #777777;
line-height: 20px;
}
#nav ul li a:hover {
color: #F55856;
}
#nav ul.subs > li > a {
font-size: 1.3em;
margin-bottom: 10px;
text-transform: uppercase;
}
#nav ul.subs > li li {
float: none;
padding-left: 8px;
-moz-transition: padding 150ms ease-out 0s;
-ms-transition: padding 150ms ease-out 0s;
-o-transition: padding 150ms ease-out 0s;
-webkit-transition: padding 150ms ease-out 0s;
transition: padding 150ms ease-out 0s;
}
#nav ul.subs > li li:hover {
padding-left: 15px;
}
/* responsive rules */
#media all and (max-width : 980px) {
#nav {
display: none;
}
#burger:hover #nav {
display: block;
}
#nav > li {
float: none;
border-bottom: 0;
margin-bottom: 0;
}
#nav ul.subs {
position: relative;
top: 0;
}
#nav li:hover ul.subs {
display: none;
}
#nav li #s1:target + ul.subs,
#nav li #s2:target + ul.subs {
display: block;
}
#nav ul.subs > li {
display: block;
width: auto;
}
}
<ul id="nav">
<li>Home</li>
<li>Menu 1
<span id="s1"></span>
<ul class="subs">
<li>Header a
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
<li>Header b
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
</ul>
</li>
<li class="active">Menu 2
<span id="s2"></span>
<ul class="subs">
<li>Header c
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
<li>Header d
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
</ul>
</li>
<li>Menu 3</li>
<li>Menu 4</li>
<li>Menu 5</li>
</ul>
I've wrapped the navigation in a div. Added a button and am toggling the navigation on and off when at a mobile size. As well as hiding the burger button when at full width.
<div class="nav-bar">
<div id="burger">
<input type="button" data-name="show" value="Toggle" id="toggle">
</div>
<!--rest of navigation follows here-->
</div>
<script>
$(document).ready(function () {
$("#burger").click(function () {
if ($(this).data('name') == 'show') {
$("#nav").animate({
height: '0px'
}).hide()
$(this).data('name', 'hide')
} else {
$("#nav").animate({
height: '100%'
}).show()
$(this).data('name', 'show')
}
});
});
</script>

How do I make the box smaller in html?

In my code, I have a drop-down menu. However, the grey box extends to the end of the screen. How can I limit the grey box?
Code from here: http://cssdeck.com/labs/7rsznauv
HTML:
<nav>
<ul class="cf">
<li><a class="dropdown" href="#">Menu Item 2</a>
<ul>
<li>Sub-menu Item 1</li>
<li>Sub-menu Item 2</li>
<li>Sub-menu Item 3</li>
</ul>
</li>
</ul>
</nav>
CSS:
nav ul {
-webkit-font-smoothing:antialiased;
text-shadow:0 1px 0 #FFF;
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
nav li {
float: left;
margin: 0;
padding: 0;
position: relative;
min-width: 25%;
}
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: ' ▶';
}
nav .dropdown:hover:after{
content:'\25bc'
}
nav li:hover a {
background: #ccc;
}
nav li ul {
float: left;
left: 0;
opacity: 0;
position: absolute;
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 {
float: none;
width: 100%;
}
nav li ul a:hover {
background: #bbb;
}
/* Clearfix */
.cf:after, .cf:before {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}​
nav ul {
-webkit-font-smoothing:antialiased;
text-shadow:0 1px 0 #FFF;
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 90%; // I made it 90% you can make it even narrower
}
Just reduce the width by reducing the percentage or like width: 180px;
just change the width in hnav ul{....}, actually is 100%, change it to 50% or whatever you want