Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS overflow hidden and absolute position

Tags:

html

css

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.

enter image description here

Demo: http://jsfiddle.net/Lgyg2a4r/

like image 673
user1355300 Avatar asked May 12 '15 10:05

user1355300


People also ask

Does overflow hidden work on position absolute?

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.

How do you hide overflow with absolute?

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.

How do you hide an element with an absolute position?

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.

Does position absolute override Flex?

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.


2 Answers

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>
like image 101
Salman A Avatar answered Oct 17 '22 05:10

Salman A


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.

like image 25
ketan Avatar answered Oct 17 '22 04:10

ketan