Consider following simple menu markup (automatically generated, and I do not have much control over it):
.menu {
width: 500px;
height: 20px;
list-style: none;
padding: 0;
margin: 0;
/* overflow: hidden ... problem */
}
li {
float: left;
position: relative;
margin-right: 30px;
}
ul li .submenu {
position: absolute;
left: 0;
display: none;
padding: 0;
margin: 0;
list-style: none;
width: 100px;
}
ul li:hover .submenu {
display: block;
}
<ul class="menu">
<li>Lorem ipsum</li>
<li>Submenu
<ul class="submenu">
<li>Sub item 1</li>
<li>Sub item 2</li>
</ul>
</li>
<li>consectetur</li>
<li>adipiscing elit</li>
<li>Aliquam elit nisi</li>
<li>pharetra quis</li>
<li>nulla eget</li>
</ul>
In the above code, the menu has a fixed width, but it has more items than can fit in that width, so the rest of the items will go on the second line. I want to display only the items which can fit in first line, and want to hide the rest of them.
For that purpose I want to specify the height for the menu. I am using this CSS for the menu:
.menu {
width: 500px;
height: 20px;
overflow: hidden; /* problem */
}
Problem is that The above css hides the .submenu
items too. Please see the demo to understand the problem.
Demo: http://jsfiddle.net/Lgyg2a4r/
Adding the parent element with position:relative; solves the problem. In order to have absolute positioned “wrapper img” work with the property of overflow: hidden, position the parent element “wrapper” to relative.
Try adding position: relative to your outer div. This will position the image relative to that div (honoring the overflow style) instead of relative to the page.
The best way to do this is by using position: absolute . This will remove the element, but we won't push it off the screen. We can hide the element by setting the width and height property to zero.
If you position a flex item absolutely, it no longer participates in the flex layout. This means any flex properties on the item become moot. You can remove them if you like.
For the first part of your problem, you could use white-space: nowrap
on the menu and display: inline-block
on its immediate children. This forces all menu items on one line and they extend past the right edge of the window.
However, this will force a horizontal scrollbar. Depending on your situation, you can add overflow-x: hidden
on the element that contains the menu. That element must have other content so that it is taller than the tallest submenu.
#wrapper {
overflow-x: hidden;
min-height: 400px;
}
.menu {
margin: 0;
padding: 0;
list-style: none;
white-space: nowrap;
background-color: palevioletred;
}
.menu > li {
display: inline-block;
margin-right: 30px;
position: relative;
}
.submenu {
position: absolute;
left: 0;
top: 100%;
margin: 0;
padding: 0;
list-style: none;
display: none;
background-color: paleturquoise;
}
.menu li:hover .submenu {
display: block;
}
<div id="wrapper">
<ul class="menu">
<li>Lorem ipsum</li>
<li>Submenu
<ul class="submenu">
<li>Sub item 1</li>
<li>Sub item 2</li>
</ul>
</li>
<li>consectetur</li>
<li>adipiscing elit</li>
<li>Aliquam elit nisi</li>
<li>pharetra quis</li>
<li>nulla eget</li>
</ul>
</div>
Following way you can do it without need of JQuery. Remove position:relative
from li
. And remove left
, top
from submenu
and use negative margin
will make trick.
.menu {
width: 500px;
height: 20px;
list-style: none;
padding: 0;
margin: 0;
overflow: hidden /* problem */
}
li {
float: left;
margin-right: 30px;
}
ul li .submenu {
position: absolute;
display: none;
list-style: none;
width: 100px;
margin-left: -40px;
}
ul li:hover .submenu {
display: block;
}
<ul class="menu">
<li>Lorem ipsum</li>
<li >Submenu
<ul class="submenu">
<li>Sub item 1</li>
<li>Sub item 2</li>
</ul>
</li>
<li>consectetur</li>
<li>adipiscing elit</li>
<li>Aliquam elit nisi</li>
<li>pharetra quis</li>
<li>nulla eget</li>
</ul>
Check Fiddle Here.
Note: not reliable but good to solve problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With