Bootstrap 3 Column Site {height: 100%;} not workin

2019-09-17 15:40发布

问题:

I know this is a common problem and I feel really stupid for not being able to figure it out, but I have a 3 column layout in HTML, and I seriously cannot figure this out for the life of me.

Here is the basic layout of my site:

fixed header, 1 sidebar on each side, middle area with content.

Here is a mockup of the site that I'm speaking about: http://eitanrosenberg.com/tests/pop/bootstrap/

It looks ok at first, but when the browser is resized, the sidebars get really small and there is a ton of white space. Why is this? Thank you so much in advance.

回答1:

Look at it this way.

The height:100% of the container div (and the column divs) gets their height from the body-element (100% of that), and the height of the body element is calculated as 100% of the height of the html element

The height of the html element is then (in practice) calculated from the current height of the browser window (100% of that).

So the height of your boxes will all be set to match the heigth of the browser window...

and this is exactly what you see when you shrink the browser window heigth!

Because:

Once your content no longer fits within the height of its container (ie. when you shrink the browser window you also shrink the calculated heights of all your containers and eventually the headroom will be too small) - overflow happens. The content of the "highest" box will then be the first to overflow, and parts of its content will then spill out of it to be visible below the boxes (since you don't use overflow: hidden).

The browser will then allow you to scroll past the bottom of the boxes (so to speak) in order for you to be able to see the content that "overflows the box", but it doesn't adjust the height of container - the boxes will still keep the same height as the browser window while you scroll...

So the background patterns will always be the height of 100% of the browser window in this example (an not match the height of the highest content when the height shrinks below that)...

One way to remedy this - is to adjust the height of the boxes with Javascript (calculate the height of the highest column and set that as an absolute value for height on the container and the boxes - every time the page resizes)

... or you could use "display: table" and "display: table-cell" on the container and the columns respectively as a workaround (overriding the Bootstrap grid CSS) for this particular layout width/media queries...

Hope this helps! Good luck!