2009-12-12 60 views
0

我正試圖完成一個簡單的'液體佈局',左邊是兩個寬度= 50%的盒子,右邊是一個寬度= 50%的盒子,最右邊的盒子的高度跟在最左邊的盒子上,反之亦然。我希望盒子間的間距爲10px,箱子的3D感很好,外面沒有邊框或邊距。如何將元素(DIV/TABLE/other)高度設置爲其容器的100%?

我試圖只使用DIV標籤來實現,但沒有成功。由於缺乏時間,我轉而採用更爲熟悉的TABLE/TR/TD方法。事實上,表格單元高度遵循總表格高度,但是當我在表格單元格中引入DIV標籤時,我可以實現我的3D框邊框,高度再次縮小。無論我在代碼中的哪個位置嘗試放置「height = 100%」,我的瀏覽器似乎都不會採取行動。

該問題與StackOverflow之前已詢問的問題有關(鏈接here)。建議的解決方案是使用TABLE而不是DIV。在我的情況下,如果我想要堅持我的3D盒子和我的單元格之間的間距,這將不起作用。

我在下面包含了我的代碼,其中包含兩種不同的方法,兩種都不起作用。有人可以建議我修復我的代碼,以便右邊框的高度達到其TD容器的100%(示例中顯示爲綠色)。任何其他具有相同結果的方法也非常受歡迎。 更新: Tor Valamo發佈了一個答案,指出100%的放置位置。不幸的是,結果僅適用於IE和Firefox,而不適用於Chrome。因此搜索繼續尋找獨立於瀏覽器的解決方案。

UPDATE:接收一些有用的建議後,我已經發布了自己的解決方案是在這一點上,只有一個接受我。不幸的是它使用TABLE not DIV。 DIV解決方案已經完成了一些工作,對於想要完成這項工作的任何人,源代碼可用here

<html> 
<head> 
    <style> 
.outsidetable { 
    border-collapse: collapse; 
    border-style: hidden; 
} 

.outsidecell { 
    border-width: 10px; 
    border-color: #FFF; 
    padding: 0px; 
    border-style: solid; 
    background-color: #0F0; 
} 

.fancybox { 
    border-style: outset; 
    border-color: yellow; 
    border-width: 2px; 
    background-color: #CCF; 
} 
    </style> 
</head> 
<body> 

<!-- First example, using DIV to draw the boxes 
    and TABLE/TR/TD for the layout --> 
<p> 
<table class=outsidetable> 
    <tr> 
<td class=outsidecell><div class=fancybox> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</div></td> 
<td class=outsidecell rowspan=2><div class=fancybox height=100% style="height: 100%"> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</div></td> 
    </tr> 
    <tr> 
    <td class=outsidecell><div class=fancybox> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</div></td> 
    </tr> 
</table> 
</p> 

<!-- Second example, using TABLE instead of DIV to draw the boxes 
    (and again TABLE/TR/TD for the layout) --> 
<p> 
<table class=outsidetable> 
    <tr> 
<td class=outsidecell><table class=fancybox><tr><td> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td></tr></table></td> 
<td class=outsidecell rowspan=2><table class=fancybox height=100% style="height: 100%"><tr><td height=100% style="height: 100%"> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td></tr></table></td> 
    </tr> 
    <tr> 
    <td class=outsidecell><table class=fancybox><tr><td> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td></tr></table></td> 
    </tr> 
</table> 
</p> 

</body> 
</html> 

回答

1

由於可怕的使用表的可能,所有提出的解決方案上場後,我發現只有一個解決方案(使用表)滿足我的所有約束。有一天,我希望能夠將Andy的代碼翻譯成帶有DIV的工作解決方案,現在我擔心我會陷入困境。 編輯:對於任何人想嘗試,所有源代碼可用here

總括來說,我的制約因素,

  • 完全液態佈局:任何細胞如下的任何其它單元格的高度
  • 間距10px的內部(而不是外部)
  • 3D的小區之間周圍的邊框細胞
  • 的,我有我的電腦上所有的瀏覽器工作器(Chrome,IE,火狐)

總結我的解決方案,我用的空隔細胞來實現單元格間距,因爲知道我的所有其他間距的方法並不能滿足所有的約束條件:

  • CSS邊框間距增加了一個邊界周圍全表過,我不想
  • 選擇單元格邊框限制我的能力的3D邊框添加到我的細胞

灌裝間隔細胞的NBSP使他們太高了,所以我選擇了離開他們空將CSS empty-cells屬性設置爲「show」。另一種選擇將是填補他們與1x1的間隔GIF的,但就是如此 1990年......

代碼:

<html> 
<head> 
    <style> 
.outsidetable { 
    border-collapse: separate; 
    border-style: hidden; 
    empty-cells: show; 
} 

.spacercell { 
    width: 10px; 
    height: 10px; 
    border-style: none; 
} 

.contentcell { 
    border-style: outset; 
    border-color: yellow; 
    border-width: 2px; 
    background-color: #CCF; 
} 

    </style> 
</head> 
<body> 

<p> 
<table class=outsidetable> 
    <tr> 
<td class=contentcell> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td> 
<td class=spacercell></td> 
<td class=contentcell rowspan=3> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td> 
    </tr> 
    <tr> 
<td class=spacercell colspan=3></td> 
    </tr> 
    <tr> 
<td class=contentcell> 
    Lorem ipsum dolor sit amet, 
    consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
</td> 
<td class=spacercell></td> 
    </tr> 
</table> 
</p> 

</body> 
</html> 
1

嘗試:

.outsidecell { 
    height:100%; 
    <other stuff here> 
} 

這工作,因爲有一個「內部」內容框是在內容的繪製發生。這個內盒只有它的內容一樣大。所以即使該內容表示100%,它也只會是100%,而不是父容器。因此,您需要將父容器擴展爲其實際的100%大小。

這可能不是它背後的實際機制,但在這麼多的話......

+0

嘿,你是對的IE和Firefox,但它不適用於Chrome。我也不太明白爲什麼會這樣;我認爲綠色已經證明容器是100%的高度。顯然不是。 如果你可以弄清楚如何使它在Chrome中也可以工作,那你就是這樣!:) – thomaspaulb 2009-12-12 01:17:16

+0

我對此做了一個新的回答。也適用於Chrome。 :) – 2009-12-12 12:30:55

0

如果你設置你的列和的DIV = 100%的高度的高度,它將爲you..that工作如果你的設計允許你設置TD上的高度:)。

+0

不,問題是它們都是可變的..它們取決於內容 – thomaspaulb 2009-12-12 01:28:22

2

首先,不要使用表格進行佈局。這是一個語義上的否定。有了這個,請參閱The Perfect 3 Column Liquid Layout by Matthew James Taylor。 Matthew創建了多列液體佈局的一個很好的實現,可以按百分比或em寬度進行設置。

+1

他的問題涉及**可變尺寸和比例**的兩個垂直相鄰部分,旁邊是與兩個尺寸相同的部分其他組合。 div佈局不能解決問題。將他的代碼粘貼到一個html文件中並將其打開。 – 2009-12-12 00:38:59

+0

我必須補充一點,如果他不挑剔兩個垂直部分是相同高度而不考慮內容,它可能是可以解決的。 – 2009-12-12 00:40:47

+0

感謝您的鏈接,但不是隻有三列布局有用的代碼?如果您能提供一些關於如何使用DIV和CSS的工作代碼,我將不勝感激。我嘗試過,並且幾乎成功了,我遇到的主要問題是單元之間的間距爲10px; DIV盒子開始重疊,並且我沒有找到在液體條件下保持間隔不變的方法。 – thomaspaulb 2009-12-12 01:22:28

2

使用表格單元格的大綱而不是邊框​​,它將起作用。把它裏面的div也放在裏面!

<html> 
<head> 
    <style> 
     table { 
      border-spacing:10px; 
      border:0px; 
     } 
     td { 
      width:50%; 
      outline-style:outset; 
      outline-color:yellow; 
      background-color:#CCF; 
      vertical-align:top; 
     } 
     * html td { /* IE hack because it doesn't recognise outline */ 
      border-style:outset; 
      border-color:yellow; 
     } 
    </style> 
</head> 
<body> 
<table> 
    <tr> 
     <td> 
      Lorem ipsum dolor sit amet 
     </td> 
     <td rowspan="2"> 
      Lorem ipsum dolor sit amet 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Lorem ipsum dolor sit amet 
     </td> 
    </tr> 
</table> 

</body> 
</html> 
+0

老兄,謝謝... +1對於努力:-) 但是我也不能使用這個..我不能使用border-spacing屬性,因爲它具有在表格周圍創建一個我無法禁用的白色邊框的副作用我不想要(我想在當前網頁中使用此佈局,我需要它是100%寬度) – thomaspaulb 2009-12-13 22:36:57

+0

查看我發佈的另一個解決方案,該解決方案在大多數情況下都可以工作。基於DIV。 – 2009-12-13 23:27:35

+0

http://stackoverflow.com/questions/1891670/how-to-set-element-div-table-other-height-to-100-of-its-container/1898179#1898179 – 2009-12-13 23:28:12

1

下面是基於一個一個div,它只要工作的右側的單元格不會比其他兩個更多。只有在您使用xhtml文檔類型時,才能在IE中使用。

.inner { 
     border:5px outset yellow; 
     background-color:#CCF; 
    } 
    .outer { 
     position:relative; 
     border:1px solid #000; 
     width:500px; 
    } 
    .left { 
     position:relative; 
     width:240px; 
    } 
    .right { 
     position:absolute; 
     top:0; 
     right:0; 
     bottom:0; 
     width:240px; 
    } 

<div class="outer"> 
    <div class="inner left"> 
     Lorem<br /> 
     Lorem<br /> 
    </div> 
    <div class="inner left"> 
     Schmorem<br /> 
     Schmorem<br /> 
     Schmorem<br /> 
    </div> 
    <div class="inner right"> 
     Ipsum schmipsum!<br /> 
     Ipsum schmipsum!<br /> 
    </div> 
</div> 
+0

它不符合我的要求單元之間的間距爲10px,如你所說,它也不是完全液體的。 我認爲這段代碼與和平主義的「堆積列布局」中的一些CSS結合在一起最終將導致DIV的工作解決方案,但我現在沒有時間去解決它。來源可在這裏找到:http://www.antiflu.dds.nl/layout.zip – thomaspaulb 2009-12-16 15:05:28

+0

更正:嚴格的文檔類型。 XHTML文檔類型不是嚴格按照定義。有足夠的XHTML文檔類型觸發怪異模式。 – BalusC 2009-12-16 15:19:40

相關問題