2017-03-19 31 views
0

我要讓粘表頭(總是在頂部),但我有兩個問題:如何使粘性表頭溢出內滾動div只使用css?

  • THEAD並div容器內不會溢出
  • THEAD不滾動(X軸)與內容

下面是一個例子(從另一問題只是分叉):

http://jsfiddle.net/quxshkdh/

HTML:

<section class=""> 
 
     <div class="container"> 
 
     <table> 
 
      <thead> 
 
      <tr class="header"> 
 
       <th> 
 
       Table attribute name 
 
       <div>Table attribute name</div> 
 
       </th> 
 
       <th> 
 
       Value 
 
       <div>Value</div> 
 
       </th> 
 
       <th> 
 
       Description 
 
       <div>Description</div> 
 
       </th> 
 
      </tr> 
 
      </thead> 
 
      <tbody> 
 
      <tr> 
 
       <td>align</td> 
 
       <td>left, center, right</td> 
 
       <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td> 
 
      </tr> 
 
      <tr> 
 
       <td>bgcolor</td> 
 
       <td>rgb(x,x,x), #xxxxxx, colorname</td> 
 
       <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td> 
 
      </tr> 
 
      <tr> 
 
       <td>border</td> 
 
       <td>1,""</td> 
 
       <td>Specifies whether the table cells should have borders or not</td> 
 
      </tr> 
 
      <tr> 
 
       <td>cellpadding</td> 
 
       <td>pixels</td> 
 
       <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td> 
 
      </tr> 
 
      <tr> 
 
       <td>cellspacing</td> 
 
       <td>pixels</td> 
 
       <td>Not supported in HTML5. Specifies the space between cells</td> 
 
      </tr> 
 
      <tr> 
 
       <td>frame</td> 
 
       <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td> 
 
       <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td> 
 
      </tr> 
 
      <tr> 
 
       <td>rules</td> 
 
       <td>none, groups, rows, cols, all</td> 
 
       <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td> 
 
      </tr> 
 
      <tr> 
 
       <td>summary</td> 
 
       <td>text</td> 
 
       <td>Not supported in HTML5. Specifies a summary of the content of a table</td> 
 
      </tr> 
 
      <tr> 
 
       <td>width</td> 
 
       <td>pixels, %</td> 
 
       <td>Not supported in HTML5. Specifies the width of a table</td> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     </div> 
 
    </section>

CSS:

html, body{ 
    margin:0; 
    padding:0; 
    height:100%; 
} 
section { 
    position: relative; 
    border: 1px solid #000; 
    padding-top: 37px; 
    background: #500; 
    width: 200px; 
    height: 200px; 
} 

table { 
    border-spacing: 0; 
    width:100%; 
} 
td + td { 
    border-left:1px solid #eee; 
} 
td, th { 
    border-bottom:1px solid #eee; 
    background: #ddd; 
    color: #000; 
    width: 100px; 
} 
th { 
    height: 0; 
    line-height: 0; 
    padding-top: 0; 
    padding-bottom: 0; 
    color: transparent; 
    border: none; 
    white-space: nowrap; 
} 
th div{ 
    position: absolute; 
    background: transparent; 
    color: #fff; 
    top: 0; 
    line-height: normal; 
    border-left: 1px solid #800; 
} 
th:first-child div{ 
    border: none; 
} 

我希望只使用不JS把戲CSS的解決方案。列的寬度可以明確定義,但高度不能。

你能修復我的例子並解釋它應該如何工作嗎?

編輯: 我已經檢查問題: Fixed header table with horizontal scrollbar and vertical scrollbar on - 這不是我的解決方案,因爲前兩個答案使用jQuery和第三個通過@Anshuman建議也是JS-基於(JS只是隱藏在HTML標籤onscroll =)

大多數解決方案是使用兩個表而不是thead,我想避免 - 我想樣式已經呈現html表與thead節。

+0

請檢查這個答案。 http://stackoverflow.com/questions/14977864/fixed-header-table-with-horizo​​ntal-scrollbar-and-vertical-scrollbar-on/35947146#35947146 贊成票它是否適合你 – Anshuman

+0

可能的複製[修復水平滾動條和垂直滾動條的頭部表格](http://stackoverflow.com/questions/14977864/fixed-header-table-with-horizo​​ntal-scrollbar-and-vertical-scrollbar-on) – Anshuman

+0

謝謝,我只是編輯我的問題來解釋差異 –

回答

1

使用你的html結構,你可以調度容器和tbody上的滾動條。但只有當容器一直滾動到右側時纔會看到tbody滾動條。

不太清楚那是你找什麼(寬度和高度用於演示,更新這些對你的需要,如果這是你試圖這樣做)

table { 
 
    display: block; 
 
    width: 75vw; 
 
} 
 

 
section { 
 
    width: 25vw; 
 
    overflow: hidden; 
 
    position: relative; 
 
    overflow-x: auto; 
 
} 
 

 
thead, 
 
tbody { 
 
    display: block; 
 
} 
 

 
tbody { 
 
    height: 150px; 
 
    overflow-x: hidden; 
 
    overflow-y: auto; 
 
} 
 

 
tr { 
 
    display: table; 
 
    border-collapse: collapse; 
 
    width: 75vw; 
 
    table-layout: fixed; 
 
} 
 

 
th, 
 
td, 
 
th div { 
 
    box-shadow: 0 0 0 1px; 
 
} 
 

 
th div { 
 
    position: absolute; 
 
    top: 0; 
 
    left: auto; 
 
    width: 25vw; 
 
    background: gray; 
 
}
<section> 
 
    <table> 
 
    <thead> 
 
     <tr class="header"> 
 
     <th> 
 
      Table attribute name 
 
      <div>Table attribute name</div> 
 
     </th> 
 
     <th> 
 
      Value 
 
      <div>Value</div> 
 
     </th> 
 
     <th> 
 
      Description 
 
      <div>Description</div> 
 
     </th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td>align</td> 
 
     <td>left, center, right</td> 
 
     <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td> 
 
     </tr> 
 
     <tr> 
 
     <td>bgcolor</td> 
 
     <td>rgb(x,x,x), #xxxxxx, colorname</td> 
 
     <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td> 
 
     </tr> 
 
     <tr> 
 
     <td>border</td> 
 
     <td>1,""</td> 
 
     <td>Specifies whether the table cells should have borders or not</td> 
 
     </tr> 
 
     <tr> 
 
     <td>cellpadding</td> 
 
     <td>pixels</td> 
 
     <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td> 
 
     </tr> 
 
     <tr> 
 
     <td>cellspacing</td> 
 
     <td>pixels</td> 
 
     <td>Not supported in HTML5. Specifies the space between cells</td> 
 
     </tr> 
 
     <tr> 
 
     <td>frame</td> 
 
     <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td> 
 
     <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td> 
 
     </tr> 
 
     <tr> 
 
     <td>rules</td> 
 
     <td>none, groups, rows, cols, all</td> 
 
     <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td> 
 
     </tr> 
 
     <tr> 
 
     <td>summary</td> 
 
     <td>text</td> 
 
     <td>Not supported in HTML5. Specifies a summary of the content of a table</td> 
 
     </tr> 
 
     <tr> 
 
     <td>width</td> 
 
     <td>pixels, %</td> 
 
     <td>Not supported in HTML5. Specifies the width of a table</td> 
 
     </tr> 
 
    </tbody> 
 
    </table>

+0

哇!它幾乎是我一直在尋找的!這個代碼唯一的問題是讓tbody填充父容器的100%寬度和高度。簡單地改變寬度和高度將不起作用。正如我在原始問題中所說的,只有列的寬度可以明確定義。容器寬度和高度僅爲例子。實際上,它將是家長分區的100%。 –

+0

我應用了一些更改:使用display:table-row和display:table-cell - 因爲我在表格中有colspan和rowspan(因此最大寬度和最小寬度使列的大小相等)。畢竟 - 它的所有CSS和它適用於現有的表。感謝幫助! –