Absolute / Relative Overflow

for some time I was messing around with a specific layout that I was obviously approaching the wrong way.

Here is an approach broken down into its main components:

<!DOCTYPE html> <html> <head> </head> <body> <div class="stretched"> <div class="header">SOME HEADER</div> <div class="someControls"> <input type="button" value="click me"/> <input type="button" value="no me"/> </div> <div class="inner"> some text... </div> </div> </body> </html> 

with the following css:

 .stretched { position:absolute; height:auto; bottom: 0; top: 0; left: 0; right: 0; width: 250px; margin: 30px auto 20px; background-color: green; } .inner { margin:10px; background-color: red; overflow: auto; } 

What I'm trying to do is make the stretched div use all available spaces of the vertical viewport (minus a few pixels higher and lower for the header and footer) and be positioned horizontally with a fixed c.

It seems to work very well. The problem is that the overflow: auto; property overflow: auto; doesn't apply to inner content as i want it. When some text... grows longer, the div inner overlaps my container and doesn't show scrollbars.

Here's the scenario: violin # 1

I want you to not have scrollbars on the body of the page and instead have overflow controlled by scrollbars inside the div inner , thereby making it fully stretched visible.

I could apply the same trick with position: absolute; top: 0; ... position: absolute; top: 0; ... position: absolute; top: 0; ... to the inner div, but then I have to explicitly specify the height of header + someControls , which I want to avoid because it is different on all my pages.

This is how it works, how I want (except for the top: 40px; ): violin # 2

What am I doing wrong here? Thanks in advance!

+6
source share
3 answers

I don't think this is possible with pure CSS (.., which works in all common browsers).

The old Flexbox spec approach is used here: http://jsfiddle.net/thirtydot/xRr7e/

Unfortunately, it only works in WebKit / Firefox browsers.

It's time to use a few lines of JavaScript for this layout.

+1
source

Hey why is position determination absolute in your stylesheet

replace this

  .stretched { width: 250px; margin: 30px auto 20px; background-color: green; } 

Live demo http://jsfiddle.net/rohitazad/Rpdr9/366/

Updated js fiddle http://jsfiddle.net/rohitazad/Rpdr9/368/

0
source

Delete

 position: absolute; 

and add

 border: 10px solid green; 

http://jsfiddle.net/Rpdr9/367/

EDIT: if you don't like the huge border, you can resize the border by 1px.

 border: 1px solid green; 

Even that works.

0
source

Source: https://habr.com/ru/post/914013/


All Articles