Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - 100% Height with Header and Footer

Tags:

css

I am trying to design a page with a header, a main div that stretches to 100% of the vertical landscape (minus header and footer) and a footer. Like this pic:

enter image description here

I can get the header and main div to work. Like this:

    <div id="wrapper">

        <div class="header_div">HEADER</div>
        <div class="main_div">MAIN</div>
        <div class="footer_div">FOOTER</div>

    </div>

With this CSS:

html {
    height: 100%;
 }

 body {
    margin: 0;
    padding: 0;
    height: 100%;
 }

.header_div{

    height: 40px;
    width: 100%;
    background-color: #000000;

}

.main_div{

    margin-bottom:40px;
    margin-top:40px;
    position:absolute;
    top:0px;
    left:0px;
    right:0px;
    bottom:0px;
    background-color: red;
}


.footer_div{

    position: relative;
    height: 40px;
    width: 100%;
    background-color: blue;
}

So the main div starts 40px off the top to account for the header and then stops 40px from the bottom to account for the footer. This works well but I cannot get the footer div to show below the main div. The way it is now with position: relative it's putting the footer on top of the main div. If I use position:absolute it puts it underneath the main div.

I am sure I am just doing this wrong because CSS is not my thing.

Any help on this would be great.

Thanks

like image 782
Sequenzia Avatar asked Jun 11 '13 21:06

Sequenzia


People also ask

How do you use 100 height in CSS?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

How do I cover full height in CSS?

Just set your nav to height:100% position:absolute with the html tag position:relative. The reason this works is because height 100% only works if its container is fixed height, with the exception (for some reason) the html tag.

Why is my Div full height?

That is because you have align-items: center for the flexbox in which case it will default to the content height. What you need to do is to remove that (let align-items: stretch come into play as it is the default) and also remove the height:100% from CASideBorder .


1 Answers

Using CSS3 Flexbox:

/*QuickReset*/*{margin:0;box-sizing:border-box;}


body {                /* body - or any parent wrapper */
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1;
}
<header>HEADER</header>
<main>MAIN</main>
<footer>FOOTER</footer>
like image 180
Roko C. Buljan Avatar answered Oct 16 '22 16:10

Roko C. Buljan