84

I have a div element wrapping other div elements like so:

<div style="overflow:hidden">
    <div id="a"></div>
    <div id="b"></div>
</div>

I have other css rules that manage the dimensions of the outer div. In my actual code, I want to position the div#a exactly 10 px below the outer div. However, I want div#b to still be cut off by the outer div's overflow:hidden.

What is the best way to achieve this?

3
  • 2
    You can't do it like that, you need to take div#a out of the div with overflow:hidden. Commented Aug 17, 2012 at 20:58
  • #a { position: relative; top: 10px; }? or do you meant you want #a to be BELOW the bottom border of the outer div? Commented Aug 17, 2012 at 20:59
  • @MarcB - yes to I want to position #a below the bottom border of the outer div Commented Aug 17, 2012 at 21:00

4 Answers 4

159

Method 1

A good way to do it is by setting the overflowing element to position:fixed (which will make it ignore the parent overflow), and then positioning it relative to the parent using this technique:

​.parent {
   position: relative;      
   .fixed-wrapper {
       position: absolute;         
       .fixed {
           position: fixed;
       }
   }
}

One caveat is that you cannot have any of the top,right,left,bottom properties set on the fixed element (they must all be default 'auto'). If you need to adjust the position slightly, you can do so using positive/negative margins instead.

Method 2

Another trick I recently discovered is to keep the overflow:hidden element with position:static and position the overriding element relative to a higher parent (rather than the overflow:hidden parent). Like so:

http://jsfiddle.net/kv0bLpw8/

Sign up to request clarification or add additional context in comments.

7 Comments

Method 1 works but may give weird results if the user scrolls the page. People landing on this answer: be sure to check out the JSFiddle under Method 2--it's very simple and works great!
Method 1 has one more caveat: you cannot have any transform set on the element which has overflow
Does not play nicely if container has scrollbar
Method 2 only works if you have no relatively positioned elements in between the leaf and the parent container... Doesn't work in my case where I can't actually override a style.
Interesting choice of 'ignore overflow' class name in fiddle.
|
4

#wrapper {
  width: 400px;
  height: 50px;
  position: relative;
  z-index: 1000;
  left: 0px;
  top: 0px;
}

#wrapper #insideDiv {
  width: 400px;
  height: 50px;
  overflow: hidden;
  position: absolute;
  z-index: 2000;
  left: 0px;
  top: 0px;
}

#wrapper #a {
  position: absolute;
  height: 30px;
  width: 100px;
  bottom: -40px;
  z-index: 1000;
  left: 0px;
}
<div id="wrapper">
  <div id="a">AAA</div>
  <div id="insideDiv">
    <div id="b">BBB</div>
  </div>
</div>

1 Comment

this code solves the specific problem by changing html structure, but do not ignore parent's element overflow
3

The easiest and most convenient way is to wrap your container div inside another div and set position: relative on the external div.

.outer-container {
  position: relative;
  height: 50px;
}

.container {
  background: gray;
  overflow: hidden;
  height: 50px;
}

#a,
#b {
  height: 100px;
  width: 100%;
}

#a {
  background: green;
  position: absolute;
  top: 60px;
}

#b {
  background: red;
  font-size: 60px;
}
<div class="outer-container">
  <div class="container">
    <div id="a"></div>
    <div id="b">Cut off</div>
  </div>
</div>

1 Comment

I had a slightly more complicated structure with a overflow:clip container, with an inner container with overflow-x:scroll, with a wide table inside it, with a context-menu thing inside each <tr>, but your answer put me on the right path and I managed to find the solution thanks to that. (Make tr static, make menu container inside tr absolute, make menu inner-container relative, and then actual menu-content elements inside.) Was stumped on this initially so it was of great help! :)
0

as people said, the element must be presented outside the parent in order to be not cropped. But you can do this with JavaScript to achieve the similar concept without having to change your actual markup:

function breakOverflow(elm) {
   var top = elm.offset().top;
   var left = elm.offset().left;
   elm.appendTo($('body'));
   elm.css({
      position: 'absolute',
      left: left+'px',
      top: top+'px',
      bottom: 'auto',
      right: 'auto',
      'z-index': 10000
   });
} 

then pass the element you want to exclude from the cropping of its parent:

breakOverflow($('#exlude-me'));

1 Comment

This method has disadvantages: 1. the element lose some styles (e.g. .parent .element { ... }) 2. you can't rely on the ancestors in js code (e.g. $(element).closest('.js-parent'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.