Setting a div's height in HTML with CSS

40

I am trying to lay out a table-like page with two columns. I want the rightmost column to dock to the right of the page, and this column should have a distinct background color. The content in the right side is almost always going to be smaller than that on the left. I would like the div on the right to always be tall enough to reach the separator for the row below it. How can I make my background color fill that space?

_x000D_
_x000D_
.rightfloat {_x000D_
  color: red;_x000D_
  background-color: #BBBBBB;_x000D_
  float: right;_x000D_
  width: 200px;_x000D_
}_x000D_
_x000D_
.left {_x000D_
  font-size: 20pt;_x000D_
}_x000D_
_x000D_
.separator {_x000D_
  clear: both;_x000D_
  width: 100%;_x000D_
  border-top: 1px solid black;_x000D_
}
_x000D_
<div class="separator">_x000D_
  <div class="rightfloat">_x000D_
    Some really short content._x000D_
  </div>_x000D_
  <div class="left"> _x000D_
    Some really really really really really really_x000D_
    really really really really big content_x000D_
  </div>_x000D_
</div>_x000D_
<div class="separator">_x000D_
  <div class="rightfloat">_x000D_
    Some more short content._x000D_
  </div>_x000D_
  <div class="left"> _x000D_
    Some really really really really really really_x000D_
    really really really really big content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Edit: I agree that this example is very table-like and an actual table would be a fine choice. But my "real" page will eventually be less table-like, and I'd just like to first master this task!

Also, for some reason, when I create/edit my posts in IE7, the code shows up correctly in the preview view, but when I actually post the message, the formatting gets removed. Editing my post in Firefox 2 seems to have worked, FWIW.


Another edit: Yeah, I unaccepted GateKiller's answer. It does indeed work nicely on my simple page, but not in my actual heavier page. I'll investigate some of the links y'all have pointed me to.

This question is tagged with html css

~ Asked on 2008-08-07 17:02:23

14 Answers


23

Ahem...

The short answer to your question is that you must set the height of 100% to the body and html tag, then set the height to 100% on each div element you want to make 100% the height of the page.

Actually, 100% height will not work in most design situations - this may be short but it is not a good answer. Google "any column longest" layouts. The best way is to put the left and right cols inside a wrapper div, float the left and right cols and then float the wrapper - this makes it stretch to the height of the inner containers - then set background image on the outer wrapper. But watch for any horizontal margins on the floated elements in case you get the IE "double margin float bug".

~ Answered on 2008-08-07 18:24:16


10

Give this a try:

_x000D_
_x000D_
html, body,_x000D_
#left, #right {_x000D_
  height: 100%_x000D_
}_x000D_
_x000D_
#left {_x000D_
  float: left;_x000D_
  width: 25%;_x000D_
}_x000D_
#right {_x000D_
  width: 75%;_x000D_
}
_x000D_
<html>_x000D_
  <body>_x000D_
    <div id="left">_x000D_
      Content_x000D_
    </div>_x000D_
    <div id="right">_x000D_
      Content_x000D_
    </div>_x000D_
  </body>_x000D_
</html>
_x000D_
_x000D_
_x000D_

~ Answered on 2008-08-07 17:33:14


8

Some browsers support CSS tables, so you could create this kind of layout using the various CSS display: table-* values. There's more information on CSS tables in this article (and the book of the same name) by Rachel Andrew: Everything You Know About CSS is Wrong

If you need a consistent layout in older browsers that don't support CSS tables, you need to do two things:

  1. Make your "table row" element clear its internal floated elements.

    The simplest way of doing this is to set overflow: hidden which takes care of most browsers, and zoom: 1 to trigger the hasLayout property in older versions of IE.

    There are many other ways of clearing floats, if this approach causes undesirable side effects you should check the question which method of 'clearfix' is best and the article on having layout for other methods.

  2. Balance the height of the two "table cell" elements.

    There are two ways you could approach this. Either you can create the appearance of equal heights by setting a background image on the "table row" element (the faux columns technique) or you can make the heights of the columns match by giving each a large padding and equally large negative margin.

    Faux columns is the simpler approach and works very well when the width of one or both columns is fixed. The other technique copes better with variable width columns (based on percentage or em units) but can cause problems in some browsers if you link directly to content within your columns (e.g. if a column contained <div id="foo"></div> and you linked to #foo)

Here's an example using the padding/margin technique to balance the height of the columns.

_x000D_
_x000D_
html, body {_x000D_
  height: 100%;_x000D_
}_x000D_
_x000D_
.row {_x000D_
  zoom: 1;          /* Clear internal floats in IE */_x000D_
  overflow: hidden; /* Clear internal floats */_x000D_
}_x000D_
_x000D_
.right-column,_x000D_
.left-column {_x000D_
  padding-bottom: 1000em;  /* Balance the heights of the columns */_x000D_
  margin-bottom: -1000em;  /*                                    */_x000D_
}_x000D_
_x000D_
.right-column {_x000D_
  width: 20%;_x000D_
  float: right;_x000D_
}_x000D_
_x000D_
.left-column {_x000D_
  width: 79%;_x000D_
  float: left;_x000D_
}
_x000D_
<div class="row">_x000D_
  <div class="right-column">Right column content</div>_x000D_
  <div class="left-column">Left column content</div>_x000D_
</div>_x000D_
<div class="row">_x000D_
  <div class="right-column">Right column content</div>_x000D_
  <div class="left-column">Left column content</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

This Barcamp demo by Natalie Downe may also be useful when figuring out how to add additional columns and nice spacing and padding: Equal Height Columns and other tricks (it's also where I first learnt about the margin/padding trick to balance column heights)

~ Answered on 2009-06-23 00:09:29


5

I gave up on strictly CSS and used a little jquery:

var leftcol = $("#leftcolumn");
var rightcol = $("#rightcolumn");
var leftcol_height = leftcol.height();
var rightcol_height = rightcol.height();

if (leftcol_height > rightcol_height)
    rightcol.height(leftcol_height);
else
    leftcol.height(rightcol_height);

~ Answered on 2009-06-22 23:18:16


3

Here's an example of equal-height columns - Equal Height Columns - revisited

You can also check out the idea of "Faux Columns" as well - Faux Columns

Don't go the table route. If it's not tabular data, don't treat it as such. It's bad for accessibility and flexibility.

~ Answered on 2008-08-07 17:39:22


2

I had the same problem on my site (shameless plug).

I had the nav section "float: right" and the main body of the page has a background image about 250px across aligned to the right and "repeat-y". I then added something with "clear: both" to it. Here is the W3Schools and the CSS clear property.

I placed the clear at the bottom of the "page" classed div. My page source looks something like this.

body
 -> header (big blue banner)
 -> headerNav (green bar at the top)
 -> breadcrumbs (invisible at the moment)
 -> page
     -> navigation (floats to the right)
     -> content (main content)
         -> clear (the quote at the bottom)
         -> footerNav (the green bar at the bottom)
     -> clear (empty but still does something)
 -> footer (blue thing at the bottom)

I hope that helps :)

~ Answered on 2008-08-07 17:07:05


2

No need to write own css, there is an library called "Bootstrap css" by calling that in your HTML head section, we can achieve many stylings,Here is an example: If you want to provide two column in a row, you can simply do the following:

_x000D_
_x000D_
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">_x000D_
_x000D_
<div class="row">_x000D_
  <div class="col-md-6">Content</div>_x000D_
  <div class="col-md-6">Content</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Here md stands for medium device,,you can use col-sm-6 for smaller devices and col-xs-6 for extra small devices

~ Answered on 2017-06-23 10:01:07


0

Just trying to help out here so the code is more readable.
Remember that you can insert code snippets by clicking on the button at the top with "101010". Just enter your code then highlight it and click the button.

Here is an example:

<html>
    <body>
    <style type="text/css">
        .rightfloat {
            color: red;
            background-color: #BBBBBB;
            float: right;
            width: 200px;
        }

        .left {
            font-size: 20pt;
        }

        .separator {
            clear: both;
            width: 100%;
            border-top: 1px solid black;
        }
    </style>

~ Answered on 2008-08-07 17:13:35


0

A 2 column layout is a little bit tough to get working in CSS (at least until CSS3 is practical.)

Floating left and right will work to a point, but it won't allow you to extend the background. To make backgrounds stay solid, you'll have to implement a technique known as "faux columns," which basically means your columns themselves won't have a background image. Your 2 columns will be contained inside of a parent tag. This parent tag is given a background image that contains the 2 column colors you want. Make this background only as big as you need it to (if it is a solid color, only make it 1 pixel high) and have it repeat-y. AListApart has a great walkthrough on what is needed to make it work.

http://www.alistapart.com/articles/fauxcolumns/

~ Answered on 2008-08-07 18:21:51


0

I can think of 2 options

  1. Use javascript to resize the smaller column on page load.
  2. Fake the equal heights by setting the background-color for the column on the container <div/> instead (<div class="separator"/>) with repeat-y

~ Answered on 2008-08-08 03:35:30


0

This should work for you: Set the height to 100% in your css for the html and body elements. You can then adjust the height to your needs in the div.

html {
    height: 100%;
}

body {
    height: 100%;
}
div {
    height: 100%; /* Set Div Height */
} 

~ Answered on 2013-08-29 07:55:46


0

_x000D_
_x000D_
.rightfloat {_x000D_
  color: red;_x000D_
  background-color: #BBBBBB;_x000D_
  float: right;_x000D_
  width: 200px;_x000D_
}_x000D_
_x000D_
.left {_x000D_
  font-size: 20pt;_x000D_
}_x000D_
_x000D_
.separator {_x000D_
  clear: both;_x000D_
  width: 100%;_x000D_
  border-top: 1px solid black;_x000D_
}
_x000D_
<div class="separator">_x000D_
  <div class="rightfloat">_x000D_
    Some really short content._x000D_
  </div>_x000D_
  <div class="left"> _x000D_
    Some really really really really really really_x000D_
    really really really really big content_x000D_
  </div>_x000D_
</div>_x000D_
<div class="separator">_x000D_
  <div class="rightfloat">_x000D_
    Some more short content._x000D_
  </div>_x000D_
  <div class="left"> _x000D_
    Some really really really really really really_x000D_
    really really really really big content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

~ Answered on 2019-06-02 14:46:57


0

It's enough to just use the css property width to do so.

Here is an example:

<style type="text/css">;
    td {
        width:25%;
        height:100%;
        float:left;
    }
</style>

~ Answered on 2013-03-24 14:48:12


0

The short answer to your question is that you must set the height of 100% to the body and html tag, then set the height to 100% on each div element you want to make 100% the height of the page.

~ Answered on 2008-08-07 17:47:58


Most Viewed Questions: