2013-03-15 57 views
2

我遇到的任務看起來很標準:我有一個固定高度的容器和3個div的內部。我希望第二個div在頂部和底部div之間延伸。當第二個div的內容溢出時 - 我想顯示滾動條。頁眉和頁腳之間的伸展div

我知道如何使用absolute定位完成此任務。一個問題是:我可以在div上使用table嗎?

一個額外的要求:如果可能,我想避免設置標題的高度固定。

我試圖在我的小提琴中編碼它,但正如你所見,I failed

CSS:

.container { 
    height: 500px; 
    background-color: gainsboro; 
} 

.table { 
    display: table; 
    height: 100%; 
} 

.table > div { 
    display: table-row; 
} 

.table > div > div { 
    display: table-cell; 
    vertical-align: middle; 
    border: 1px solid black; 
    overflow: scroll; 
} 

.center > div { 
    height: 100%; 
} 

.content { 
    height: 700px; 
} 

HTML:

<div class="container"> 
    <div class="table"> 
    <div> 
     <div>XXX</div> 
    </div> 
    <div class="center"> 
     <div> 
      <div class="content"></div> 
     </div> 
    </div> 
    <div> 
     <div>YYY</div> 
    </div> 
    </div> 
</div> 
+0

我試圖在我的小提琴代碼,但是,你看,我失敗:http://jsfiddle.net/USZXa/ – KutaBeach 2013-03-15 15:16:23

+0

我不知道什麼是「我能用div上的表格做它?「手段。 – isherwood 2013-03-15 15:23:56

+0

對不起,我的英文,isherfood,我的意思是我想要使用從「顯示:表格」,「顯示:表格行」,「顯示:表格單元格」屬性創建的表格,你可以看到它的代碼我提供。 – KutaBeach 2013-03-15 15:56:34

回答

1

這裏是一個可能的解決方案:

<div class="container"> 
    <div class="header"> 
     <div>Top Header Block</div> 
    </div> 
    <div class="center"> 
     <div class="content"> 
      <p>Lorem ipsum ...</p> 
      <p>Lorem ipsum ...</p> 
     </div><!-- .content --> 
    </div><!-- .center --> 
    <div class="footer"> 
     <div>Bottom Footer Block</div> 
    </div> 
</div> 

和CSS:

.container { 
    height: 200px; 
} 
.header, .footer { 
    background-color: gainsboro; 
} 
.center { 
    height: inherit; 
} 
.content { 
    background-color: #F0F0F0; 
    height: inherit; 
    overflow: auto; 
} 

既然你是固定的容器的高度,你繼承了高度都在.center.content<div>的。

如果調整容器高度,中心div會展開,但頁眉和頁腳div保持相同的高度。

在內容div上使用overflow以允許滾動。

小提琴參考:http://jsfiddle.net/audetwebdesign/nae5z/

+0

您是否測量了由此產生的高度? – 2013-03-15 16:05:18

+0

不,但它比父容器的要多... – 2013-03-15 16:09:57

+0

OP需要一個固定的高度,並不是說它必須很容易指定。 – 2013-03-15 16:10:47

1

你的方式是正確的,只是做一些修改(見本Fiddle):

html, body, .container, .table { 
    margin: 0; 
    height: 100%; 
} 

#header,#footer { 
    height: 1px; 
} 

這應該管用 因爲如果內容需要,表格單元格的高度會增加。

只是一個提示:您可以改進整個事情,例如我會使用HTML 5和<header/><footer/>元素。但那不是你問題的一部分。總之,這裏是另一個更新您的fiddle

<div> 
     <header> 
      <div>XXX</div> 
     </header> 
     <main> 
      <div> 
       <div class="content"></div> 
      </div> 
     </main> 
     <footer> 
      <div>YYY</div> 
     </footer> 
</div> 

使用CSS:

html, body, body > div { 
    margin: 0; 
    height: 100%; 
} 

body { 
    background-color: gainsboro; 
} 

body > div { 
    display: table; 
    width: 100%; 
} 

body > div > * { 
    display: table-row; 
} 

body > div > * > div { 
    display: table-cell; 
    vertical-align: middle; 
    border: 1px solid black; 
} 

header, footer { 
    height: 1px; 
} 

main是很新的HTML 5,只是如果你想知道。

+0

@Kutabeach你確定你希望頁眉和頁腳是可以縮小的嗎?無論如何,如果你想要一個好笑的話:http://baijs.nl/tinyscrollbar/ – bleuscyther 2013-03-15 15:44:34

+0

+1 @Linus Cadwell – bleuscyther 2013-03-15 15:45:54

+0

「table-cell」方法的優點是可以垂直居中放置內容。 – 2013-03-15 16:12:27