Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS flexbox: Header, main, footer layout. Main part collapsing in the middle

Tags:

html

css

I have a simplest situation with

<!DOCTYPE html>
<html>
<head>
    <title>Whatever</title>
</head>

<body>
<header></header>
<main></main>
<footer></footer>
</body>
</html>

So I want my header to be say .. 15%, my footer 5% and my main part taking the rest of the space in the middle. I can do it with position fixed and all that but I am trying to figure out the flex boxes. So all my attempts don't work. According to this article: Flexbox layout I should have it as simple as this:

html, body {
    height: 100%;
    width: 100%;
    padding: 0;
    margin: 0;
}

body {
    display: flex;
    flex-direction: column;
}

header {
    height: 75px;
}

main {
    flex: auto;
}

footer {
    height: 25px;
}

... but this just does not work. I tried changing sizes from pixels to percentage - no luck. The middle part just collapses and it does not stretch.

I also tried few other examples within the first page of Google hits. Nothing works.

What am I doing wrong. How can I achieve the above with flex boxes?

Thanks.

P.S. Almost forgot. My browser is Chromium 34.0.1847.116 (260972)

like image 480
r.sendecky Avatar asked Apr 27 '14 14:04

r.sendecky


People also ask

What are two main parts of flexbox layout?

The two axes of flexbox The main axis is defined by the flex-direction property, and the cross axis runs perpendicular to it. Everything we do with flexbox refers back to these axes, so it is worth understanding how they work from the outset.

When using flexbox layout the flex property does what?

Flexbox is a one-dimensional layout method for arranging items in rows or columns. Items flex (expand) to fill additional space or shrink to fit into smaller spaces.

Can you use flexbox for header?

Flexbox in Action When flexbox is applied to the header element, it will make all the child items in the same line. Then, all you need is to apply justify-content to distribute the spacing between them.


1 Answers

I don't know how you where trying to this method out, but it's working (now at least).

Here's a JSFiddle showing the layout - I added a subtle background-color to illustrate the stretch.

<header>header</header>
<main>main</main>
<footer>footer</footer>

html, body {
    height: 100%;
    width: 100%;
    padding: 0;
    margin: 0;
}

body {
    display: flex;
    flex-direction: column;
}

header {
    height: 75px;
}

main {
    flex: auto;
    background-color: #ccc;
}

footer {
    height: 25px;
}
like image 161
Matias Vad Avatar answered Oct 10 '22 03:10

Matias Vad