开发者

2 boxes of same height (percentage)

How to create two boxes (floating side by side) of same height. I want to create boxes of height 40% of the container/w开发者_JAVA百科indow?


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>


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;
}
​


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.


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]-->
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜