12

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>
captainsac
  • 2,484
  • 3
  • 27
  • 48
iammikerodriguez
  • 379
  • 1
  • 5
  • 16

6 Answers6

3

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;
}
DRD
  • 5,557
  • 14
  • 14
  • Add some kind of overflow rule to `.leftPanel, .rightPanel` like `overflow: auto;`to prevent content from bleeding out of the frame, and causing body to show scrollbar. – Wish Nov 24 '17 at 18:09
2

You can use absolute positioning if you want to have it 100% height always. And then use scroll bars if required inside the leftpanel or the rightpanel.

Example: http://jsfiddle.net/G7unG/2/

html, body{
    height: 100%;
    margin: 0;
}
.header{
    background: #333;
    padding: 15px;
    text-align:center;
    font-size: 18px;
    font-family: sans-serif;
    color: #FFF;
    height: 22px;
}
.leftpanel, .rightpanel{
    top: 52px;
    bottom: 0;
    position: absolute;
}
.leftpanel{
    width: 70%;
    left: 0;
    background: #CCC;
}
.rightpanel{
    width: 30%;
    right: 0;
    background: #666;
}

Solution 2 - use fixed percentages for height: http://jsfiddle.net/G7unG/4/

html, body{
    height: 100%;
    margin: 0;
}
.header{
    background: #333;
    padding: 15px;
    text-align:center;
    font-size: 18px;
    font-family: sans-serif;
    color: #FFF;
    height: 30%;
    box-sizing: border-box;
}
.leftpanel, .rightpanel{
    height: 70%;
    float: left;
}
.leftpanel{
    width: 70%;
    left: 0;
    background: #CCC;
}
.rightpanel{
    width: 30%;
    float: right;
    background: #666;
}
Niels
  • 48,601
  • 4
  • 62
  • 81
  • Solution 1: positioning absolute would work BUT the top wouldn't be known and cannot be fixed since different pages have different header heights. Solution 2: The header changes height on different pages according to the content so I don't think a fixed header height would work. – iammikerodriguez Jun 27 '14 at 21:43
  • Solution 2 was exactly what I needed, +1 – Spankied Oct 12 '20 at 01:49
  • Just to let you know, there are easier ways now or better solutions. To use Flex, see this post: https://stackoverflow.com/questions/25098042/fill-remaining-vertical-space-with-css-using-displayflex – Niels Oct 12 '20 at 17:04
2

You could use overflow: hidden; to protect the body to be scrollable.

according to your comment: http://jsfiddle.net/G7unG/9/

T_01
  • 1,256
  • 3
  • 16
  • 35
  • 3
    Overflow hidden will cause other parts of my website that need vertical scrolling to not scroll. – iammikerodriguez Jun 27 '14 at 21:38
  • no, if you only apply it to the body? In other elements you can enable it with overflow: auto; again... – T_01 Jun 27 '14 at 22:06
  • This works if you're okay with [each column scrolling separately](http://jsfiddle.net/G7unG/12/). (Not ideal in my opinion.) Otherwise, it [truncates the page and doesn't allow scrolling](http://jsfiddle.net/G7unG/13/). – showdev Jun 27 '14 at 22:15
1

Like this: http://jsfiddle.net/G7unG/3/

html, body{
    height: 100%;
    margin: 0;
    overflow:hidden;
}
Takoyaro
  • 928
  • 7
  • 14
1

You could use a "faux columns" type of structure -- adding the background color of your columns as "fixed" elements (they wont scroll with the page) behind your real columns.

<div id="left_faux"></div>
<div id="right_faux"></div>
div#left_faux {
    position: fixed;
    top:0;
    left:0;
    right:30%;
    bottom:0;
    background-color:#CCC;
}
div#right_faux {
    position: fixed;
    top:0;
    left:70%;
    right:0;
    bottom:0;
    background-color:#666; 
}

.leftpanel{
    float: left;
    width: 70%;
}
.rightpanel{
    float: left;
    width: 30%;
}

This quick example is perhaps overly verbose, for demonstration purposes. I'm sure you can streamline the CSS so there aren't so many redundant definitions.

WORKING EXAMPLE

showdev
  • 28,454
  • 37
  • 55
  • 73
1

Use viewports. Browsers now support giving height a percentage of page height. Drop the 100 down to 80 if you've got a header taking up space.

div {
    height:100vh;
}
Nathan S
  • 114
  • 1
  • 6