Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make DIV 100% height of browser without vertical scrolling of header

Tags:

html

css

height

Both the left and right panels have a height of 100%, but since the Header div takes up X amount of space, there is some vertical scrolling in the window that I want to get rid of.

How can I remove that vertical scrolling?

JSFiddle: http://jsfiddle.net/G7unG/1/

CSS and HTML

html, body{
  height: 100%;
  margin: 0;
}
.header{
  background: #333;
  padding: 15px;
  text-align:center;
  font-size: 18px;
  font-family: sans-serif;
  color: #FFF;
}
.leftpanel, .rightpanel{
  height: 100%;
}
.leftpanel{
  float: left;
  width: 70%;
  background: #CCC;
}
.rightpanel{
  float: left;
  width: 30%;
  background: #666;
}
<div class="header">Header</div>
<div class="leftpanel">Left Panel</div>
<div class="rightpanel">Right Panel</div>
<div class="clearfix"></div>
like image 803
iammikerodriguez Avatar asked Jun 27 '14 21:06

iammikerodriguez


People also ask

How do you give a height 100% to a div?

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.


1 Answers

Here's a modern solution using flexbox. Regardless of the height of the header the rest of the elements will stretch vertically to fill the remaining space. Here's the fiddle: http://jsfiddle.net/mggLY/1/.

HTML:

<div id = "wrapper">
    <div class="header">Header</div>
    <div>
        <div class="leftpanel">Left Panel</div>
        <div class="rightpanel">Right Panel</div>
    </div>
</div>

CSS:

* {
    margin: 0;
    padding: 0;
    border: 0;
}

html, body {
    height: 100%;
}

.header{
    background: #333;
    padding: 15px;
    text-align:center;
    font-size: 18px;
    font-family: sans-serif;
    color: #fff;
}

.leftpanel{
    background: #CCC;
}

.rightpanel{
    background: #666;
}

#wrapper {
    height: 100%;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: column;
    flex-direction: column;
    outline: 1px solid red;
}

#wrapper > .header {
    -webkit-flex: 0 0 auto;
    flex: 0 0 auto;
}

#wrapper > .header + div {
    -webkit-flex: 1 1 auto;
    flex: 1 1 auto;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row;
    flex-direction: row;
}

#wrapper > .header + div > div:first-of-type {
    -webkit-flex: 7 0 0;
    flex: 7 0 0;
}

#wrapper > .header + div > div:last-of-type {
    -webkit-flex: 3 0 0;
    flex: 3 0 0;
}
like image 89
DRD Avatar answered Oct 19 '22 16:10

DRD