Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - absolute within fixed with overflow for scrolling and 100% height

Tags:

html

css

I'm trying to have a scrollable overlay of some sort that blanks out the rest of the site. I can't seem to get 100% height on the absolute element within my fixed element.

http://codepen.io/intellix/pen/GBltK

section {
  background: red;
  position: fixed;
  top:0;
  right:0;
  bottom:0;
  left:0;
  overflow-x:auto;

}

article {
  background: blue;
  position: absolute;
  top:0;
  width:300px;
  bottom: 0;
  left: 0;
}

If I set bottom: 0; on the absolute element, it fills height when the page doesn't overflow. When the page overflows it leaves a gap.

When I use bottom: auto on my absolute element it fills height with overflow, but leaves a gap without overflow!

If you resize the window so the content fits and then resize so the content doesn't, you can see that it doesn't work in both cases.

like image 278
Intellix Avatar asked Mar 05 '13 22:03

Intellix


People also ask

What is the difference between position fixed and absolute?

Whereas the position and dimensions of an element with position:absolute are relative to its containing block, the position and dimensions of an element with position:fixed are always relative to the initial containing block. This is normally the viewport: the browser window or the paper's page box.

Why is Z-Index not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

How to use the sticky position CSS?

To see the effect of sticky positioning, select the position: sticky option and scroll this container. The element will scroll along with its container, until it is at the top of the container (or reaches the offset specified in top ), and will then stop scrolling, so it stays visible.


1 Answers

I think you want to use min-height and bottom:auto here.

article {
  background: blue;
  position: absolute;
  top:0;
  width: 300px;
  bottom: auto;
  min-height: 100%;
  left: 0;
}

The reason you need min-height:100%; and can't use height:100%; is because when the section content in scrollable it's height is actually greater than 100%.

Longer explination:

With positioned (position: relative|fixed|absolute;) elements, percentage based heights are determined relative to their offset parents. In the case of the article element, it's offset parent is the section element.

The section element uses top:0px; and bottom:0px; to determine it's height. These values are determined by the height of it's offset parent which is the html element

html is a special case where height:100%; is the size of the view-able window, which is why your scrollable element has a height larger than 100%.

like image 75
JoeyP Avatar answered Sep 24 '22 15:09

JoeyP