2013-03-05 31 views
2

我正在尋找一個HTML/CSS解決方案,其中的內DIV具有以下特點:如何在包含在固定尺寸的外部div中的內部div上獲得垂直滾動條?

必須有:

  • 包括可變高度同級標題下方的固定高度div容器內DIV。
  • 填充可用的可變高度沒有最大高度CSS屬性
  • 垂直滾動如果必要的話,以適應含量
  • 是文檔內具有DOCTYPE = HTML 4.01嚴格(優選地,HTML 5如有必要)
  • 是多-browser兼容(瀏覽器,Firefox,IE,Safari瀏覽器,移動電話瀏覽器)

想有:

  • 不使用表
  • 不使用JavaScript

到目前爲止,我有以下的部分解決方案:


HTML

<div class="parent"> 
    <table cellspacing="0"> 
     <tr><td class="header">Heading</td></tr> 
     <tr><td class="scrollContainer"><div class="innerScroll"> 
      Lots of text goes here. Lots of text goes here. Lots of text goes here. 
      ... 
      Lots of text goes here. Lots of text goes here. Lots of text goes here. 
      </div> 
     </td></tr> 
    </table> 
</div> 

CSS

.parent 
{ 
    margin:0px; 
    padding:0px; 
    height: 400px; 
    background-color: orange; 
    border: thick purple solid; 
} 

table 
{ 
    border:green thick solid; 
    width:100%; 
    height:100%; 
    margin:0px; 
} 

.header 
{ 
    font-weight: bold; 
    font-size: 28pt; 
    font-family: sans-serif,Arial; 
    border:yellow thick solid; 
} 

.scrollContainer 
{ 
    margin: 0px; 
    position: relative; 
    border: thick blue solid; 
    bottom:0px; 
    top:0px; 
    left:0px; 
    right: 0px; 
    height:100%; 
} 

.innerScroll 
{ 
    overflow-y: auto; 
    position: absolute; 
    top: 0px; 
    bottom: 0px; 
    left: 0px; 
    right: 0px; 
} 


HTML

<div class="parent"> 
    <div class="header">Heading</div> 
    <div class="scrollContainer"> 
     <div class="innerScroll"> 
      Lots of text goes here. Lots of text goes here. Lots of text goes here. 
      ... 
      Lots of text goes here. Lots of text goes here. Lots of text goes here. 
     </div> 
    </div> 
</div> 

CSS

.parent 
{ 
    margin:0px; 
    padding:0px; 
    height: 400px; 
    background-color: orange; 
    border: thick purple solid; 
} 
.header 
{ 
    font-weight: bold; 
    font-size: 28pt; 
    font-family: sans-serif,Arial; 
    border:yellow thick solid; 
} 

.scrollContainer 
{ 
    position: relative; 
    border: thick blue solid; 
    bottom:0px; 
    top:0px; 
    left:0px; 
    right: 0px; 
    height:100%; 
} 

.innerScroll 
{ 
    position: absolute; 
    overflow-y: auto; 
    top: 0px; 
    bottom: 0px; 
    left: 0px; 
    right: 0px; 
} 

JS

var container = $(".scrollContainer"); 
var header = $(".header"); 
var parent = $(".parent"); 
var containerHeight = parent.innerHeight() - header.outerHeight(); 
//alert("containerHeight=[" + containerHeight + "]"); 
container.outerHeight(containerHeight); 

預先感謝您的幫助!

回答

3

如果不是手動設置標題的高度,你可以在滾動區域使用絕對定位和設置上是頭部的高度問題。我害怕你不會用純CSS來接近。

http://jsfiddle.net/Neograph734/yDJrV/2/

.parent 
{ 
    margin:0px; 
    padding:0px; 
    height: 400px; 
    background-color: orange; 
    border: thick purple solid; 
    position: relative; 
} 
.header 
{ 
    font-weight: bold; 
    font-size: 28pt; 
    font-family: sans-serif,Arial; 
    border:yellow thick solid; 
} 

.scrollContainer 
{ 
    position: absolute; 
    border: thick blue solid; 
    bottom:0px; 
    top:55px; /* This is the header height */ 
    left:0px; 
    right: 0px; 

} 

.innerScroll 
{ 
    position: absolute; 
    overflow-y: auto; 
    top: 0px; 
    bottom: 0px; 
    left: 0px; 
    right: 0px; 
    height: 100%; 
} 

更新

如果JavaScript是沒有問題的,你可以使用這個小片的jQuery

var headerHeight = $('.header').outerHeight(); 
$('.scrollContainer').css('top', headerHeight); 

工作示例這裏:http://jsfiddle.net/Neograph734/yDJrV/3/

+0

謝謝爲你解答呃。我將編輯我的問題以區分「必須擁有」和「真的很喜歡」的要求。我必須能夠處理一個可變的標題。例如,標題字體可能會更改。再次感謝! – KSev 2013-03-05 23:42:34

+0

更新了答案,它應該現在工作。您可能希望爲非JavaScript瀏覽器保留'top:55px'以不完全覆蓋標題。 (根據你的字體玩它)。 – Neograph734 2013-03-06 10:26:53

+0

再次感謝您的建議和努力。這個問題是一個更大目標的一部分 - 在jQuery選項卡中的可變標題下獲取可滾動內容。我最終得到了一些工作,但沒有像我希望的那樣乾淨。對於什麼是值得你可以看看這裏:http://jsfiddle.net/ksevksev/9Q957/4/ – KSev 2013-03-07 17:17:23

相關問題