2010-10-15 70 views

回答

2

See the Example Here


如果這是你所尋找的,這裏更多:

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

要點:

  • float:left用來對準兩個箱子並排側
  • height%兩個孩子箱指定讓他們從他們的父母繼承。

HTML:

<div id="parent"> 
    <div id="child1"> 
     This is first box 
    </div> 
    <div id="child2"> 
     This is second box 
    </div> 
    </div> 
+0

在我之前到過!好的答案+ 1 – Kyle 2010-10-15 08:40:16

0

使用%作爲高度是相對於父容器的高度。因此您需要聲明父容器的高度。看看這個教程:Equal Height Columns

+0

此外,讓我提醒你時間提前:CSS高度的棘手的事情要完成http://stackoverflow.com/questions一個/ 3931187/css-metaphysics-why-is-page-vertical-alignment-so-difficult – Ben 2010-10-15 08:20:38

1

這應該是你一個簡單的解決方案。這是我的例子:

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

+1我認爲你的意思是使用IDS,但:) – Sarfraz 2010-10-15 08:59:23

+0

是的,我可能應該再看一遍..我傾向於使用類的大多數一切萬一我不得不在文件中重複的東西,你知道一些客戶是如何.. :) – Kyle 2010-10-15 09:08:03

0

問題特別提到浮動,也有過了幾個很好的答案,但我認爲如果提及浮動是偶然的,則可能值得發帖an answer that doesn't use floats

.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> 

它目前不在WebKit中工作,但我認爲這是一個錯誤,並且會有一個解決方法,我正在調查。如果你需要在IE <工作8添加條件註釋:

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