tags:

views:

53

answers:

4

How to create two boxes (floating side by side) of same height. I want to create boxes of height 40% of the container/window?

A: 

Using a % as height is relative to your parent container's height. Therefore you need to declare the height of your parent container. Take a look at this tutorial: Equal Height Columns.

Anzeo
Also, let me warn you ahead of time: CSS height is one of the trickier things to accomplish http://stackoverflow.com/questions/3931187/css-metaphysics-why-is-page-vertical-alignment-so-difficult
Steve
+2  A: 

See the Example Here


If that is what you are looking for, here is more:

CSS:

  #parent{
    width:205px;
    height:200px;
    border:1px solid #000000;
    overflow:auto;
  }

  #child1{
    height:40%;
    background:#00ff00;
    float:left;
  }

  #child2{
    height:40%;
    background:#0000ff;
    float:left;
  }  

The Important Points:

  • The float:left is used to align the two boxes side-by-side
  • The height is specified in % for both child boxes so that they inherit from their parent.

HTML:

  <div id="parent">
    <div id="child1">
        This is first box
    </div>
    <div id="child2">
        This is second box
    </div>
  </div>
Sarfraz
Got there before me! Good answer.+1
Kyle Sevenoaks
+1  A: 

This should be a simple solution for you. Here's my example:

jsfiddle

HTML:

<div class="wrap">
    <div class="left">
        Content
    </div>
    <div class="right">
        More content
    </div>
</div>

CSS:

.wrap
{
    width: 400px;
    height: 500px;
    border: 1px solid #000;
}

.left, .right
{
    float: left;
    width: 45%;a
    height: 40%;
    margin: 2%;
}

.left
{
    border: 1px solid #f00;
}

.right
{
    border: 1px solid #00f;
}
​
Kyle Sevenoaks
+1 I think you meant to use ids though :)
Sarfraz
Yeah, I probably should have looking at it again.. I tend to use classes for most everything in case I have to repeat things in the document, you know how some clients are.. :)
Kyle Sevenoaks
A: 

The question specifically mentions floating, and there have been several good answers for that, but I thought it might be worth posting an answer that doesn't use floats in case the the mention of floating was accidental:

.wrapper {
    width: 400px;
    height: 400px;
    outline: 1px solid #000;
}
.wrapper div {
    display: inline-block;
    width: 198px;
    height: 40%;
    background: #66c;
}
.wrapper div:first-child {
    background: #6c6;
}

<div class="wrapper">
    <div>This is the first box</div>
    <div>This is the second box</div>
    <p>Some other content</p>
</div>

It doesn't currently work in WebKit, but I assume that's a bug and there'll be a workaround, I am investigating. If you need it to work in IE < 8 add a conditional comment:

<!--[if lt IE 8]>
<style>
    .wrapper div { zoom:1; *display:inline;}
</style>
<![endif]-->
robertc