Flex整页布局避免底部被撑掉 🔑 Flexbox Full Page Web App Layout

From: http://geon.github.io/programming/2016/02/24/flexbox-full-page-web-app-layout

I was working on a web app layout, and since we require a modern browser (ie10+), I had the chance to use Flexbox. Yay!

The layout was designed by someone who apparently don’t like the way the web tend to work, with unlimited page height. Instead the app is supposed to cover the entire window without scrolling, and have a footer stuck to the bottom of the screen. There are also sidebars with tools, and a 3D view in the center.

If the content in the sidebars it too large to fit in the window, it is important that the footer is not pushed down to make room, but that the sidebar and only the sidebar overflows and scrolls.

Let’s say we have this basic document structure.

 

The first issue to take care of is making the full-screen div cover the full window exactly. It’s pretty easy, but should be taken care of first, since the entire layout could break if you add it later.

…Which has happened to me. Sigh.

You need to set height: 100% on full-screen. But that isn’t enough. As far as CSS is concerned, the BODY element surrounding the div of the document doesn’t have a height on it’s own, so you need the 100% height there too. Same thing with the HTML element surrounding that. The BODY also has a margin we need to get rid of.

The flexbox-y stuff is pretty simple when you get it. Basically, you set display: flex; on a container, and optionally specify the flex-grow and flex-shrink on the children. If you want to set a fixed, explicit width on a child, you need to prevent it from flexing the width by setting both properties to 0. (You can use the flex shorthand.)

To make a column of stuff stacked vertically, set flex-direction: column; on the container.

It’s all in this exellent guide.

This is going to be pretty repetitive, tedious and error prone if you write it in plain CSS. Just like all styling. Don’t do that. Use a preprocessor, like Less.

Let’s make the outermost div a flexbox. The children will have a fixed height unless explicitly marked to use flex-grow (and flex-shrink) to fill the available space.

Apply them as mixins to the full-screen, and add sizing to the children:

You need to do the same for every div you want to behave like this.

This still isn’t working, though. Large content in .main will push down the footer of the page, instead of causing scrolling. (Or in an iframe like this Codepen, push the content down below the footer.) Adding overflow: scroll; to .left.right and .middle is necessary, but not enough. We also need to make the parent stop trying to accommodate them, by setting overflow: hidden; on .main. I have no idea why that works, and the specs says nothing either, but the behavior is consistent over Chrome, Safari, Firefox and Explorer.

Let’s add that to .flex-container.

Notice how the left menu column scrolls independently, and the footer is locked to the bottom of the Codepen iframe.

发表评论

电子邮件地址不会被公开。 必填项已用*标注