2014-09-26 33 views
0

我只是無法弄清楚什麼是問題,我有一個HTML表,我想讓表頭保持在一個固定的位置,保持最長td的寬度。我需要它的腳本和th元素必須是固定的寬度沒有JavaScript和簡單的CSS 使表頭保持在一個固定的位置,並保持最長的寬度td

BODY { 
 
     font-family: Arial; 
 
     font-size: 8pt 
 
    } 
 
    .focim { 
 
     margin-left: 400px; 
 
     color: Black; 
 
     font-family: Arial; 
 
     font-size: 15pt; 
 
     font-weight: 900; 
 
     padding-bottom: 20px; 
 
     text-decoration: underline 
 
    } 
 
    Time { 
 
     color: Black; 
 
     font-family: Arial; 
 
     font-size: 10pt; 
 
     font-weight: 500; 
 
     padding-bottom: 20px 
 
    } 
 
    TABLE { 
 
     border-width: 3px; 
 
     border-style: solid; 
 
     border-color: Black; 
 
     border-collapse: collapse; 
 
    } 
 
    TH { 
 
     font-family: Arial; 
 
     font-size: 10pt; 
 
     border-width: 1px; 
 
     border-style: solid; 
 
     border-color: Black; 
 
     background: #b3b3b3; 
 
    } 
 
    TR:first-child { 
 
     position: fixed; 
 
     margin-top: 0px; 
 
     white-space: nowrap; 
 
    } 
 
    TD { 
 
     white-space: nowrap; 
 
     border-width: 1px; 
 
     padding: 3px; 
 
     border-style: solid; 
 
     border-color: Black 
 
    } 
 
    TD:first-child { 
 
     background-color: #C19A6B; 
 
     padding-right: 5px; 
 
     padding-left: 5px 
 
    } 
 
    TR:nth-child(even) { 
 
     background-color: #dddddd; 
 
    } 
 
    tr:hover td { 
 
     background: #4E5066; 
 
     color: #FFFFFF; 
 
     border-top: 1px solid #22262e; 
 
     border-bottom: 1px solid #22262e; 
 
    }
<table> 
 
    <colgroup> 
 
    <col/> 
 
    <col/> 
 
    <col/> 
 
    <col/> 
 
    </colgroup> 
 
    <tr> 
 
    <th>Name</th> 
 
    <th>IP_Addresses0</th> 
 
    <th>Domain</th> 
 
    <th>LocalPath0</th> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 

 

 
</table>

+0

什麼是你遇到的問題? – 2014-09-26 09:11:07

+0

可能的重複http://stackoverflow.com/questions/673153/html-table-with-fixed-headers – 2014-09-26 09:16:18

回答

0

嘗試這樣的例子。

BODY { 
 
    font-family: Arial; 
 
    font-size: 8pt 
 
} 
 
.focim { 
 
    margin-left: 400px; 
 
    color: Black; 
 
    font-family: Arial; 
 
    font-size: 15pt; 
 
    font-weight: 900; 
 
    padding-bottom: 20px; 
 
    text-decoration: underline 
 
} 
 
Time { 
 
    color: Black; 
 
    font-family: Arial; 
 
    font-size: 10pt; 
 
    font-weight: 500; 
 
    padding-bottom: 20px 
 
} 
 
TABLE { 
 
    border-width: 3px; 
 
    border-style: solid; 
 
    border-color: Black; 
 
    border-collapse: collapse; 
 
} 
 
TH { 
 
    font-family: Arial; 
 
    font-size: 10pt; 
 
    border-width: 1px; 
 
    border-style: solid; 
 
    border-color: Black; 
 
    background: #b3b3b3; 
 
} 
 
TR:first-child { 
 
    position: fixed; 
 
    left: 10px; 
 
} 
 
TD { 
 
    white-space: nowrap; 
 
    border-width: 1px; 
 
    padding: 3px; 
 
    border-style: solid; 
 
    border-color: Black 
 
} 
 
TD:first-child { 
 
    background-color: #C19A6B; 
 
    padding-right: 5px; 
 
    padding-left: 5px 
 
} 
 
TR:nth-child(even) { 
 
    background-color: #dddddd; 
 
} 
 
tr:hover td { 
 
    background: #4E5066; 
 
    color: #FFFFFF; 
 
    border-top: 1px solid #22262e; 
 
    border-bottom: 1px solid #22262e; 
 
}
<table> 
 
    <colgroup> 
 
    <col/> 
 
    <col/> 
 
    <col/> 
 
    <col/> 
 
    </colgroup> 
 
    <tr> 
 
    <th>Name</th> 
 
    <th>IP_Addresses0</th> 
 
    <th>Domain</th> 
 
    <th>LocalPath0</th> 
 
    </tr> 
 
    <tr> 
 
    <th>Name</th> 
 
    <th>IP_Addresses0</th> 
 
    <th>Domain</th> 
 
    <th>LocalPath0</th> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 
    <tr> 
 
    <td>name</td> 
 
    <td>someip</td> 
 
    <td>domain</td> 
 
    <td>path</td> 
 
    </tr> 
 

 

 

 
</table>

+0

我之前試過這個演示,主要問題是,我不能添加dvis,因爲腳本生成表格並且寬度不應該固定,寬度應該與每個th-td連接使用最長的字段一起使用white-space:nowrap; – LeviD77 2014-09-26 09:40:13

+0

我編輯了我的答案,我又放了一個相同的標題,並且已經給'tr:first-child'定位了'position:fixed;'。告訴我你喜歡這個解決方案或不。 – 2014-09-26 09:52:28

+0

是的,關於這是我想要的,只是在這種情況下,標題是固定的,但不夠寬,th元素不在td元素上方 – LeviD77 2014-09-26 12:17:40

相關問題