Ich möchte eine Tabelle von reinem css stylen, deren Header behoben werden konnte, während Körper vertikal scrollbar sein kann.
Das ist mein demoProzentbreite auf td/th funktioniert nicht (jsfiddle)
/*css for fixed header, vertically scrollable body*/
.fixedHeader {
width: 100%;
}
.fixedHeader thead {
display: block;
width: cal(100% - 17px);
}
.fixedHeader tbody {
display: block;
width: 100%;
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
}
/*custom appearance for your table*/
.fixedHeader {
border: 1px solid gray;
border-collapse: collapse;
}
.fixedHeader th,
.fixedHeader td {
border: 1px solid gray;
}
<table class="fixedHeader">
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Apple</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Pear</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Grape</td>
<td>Purple/Green</td>
<td>These are purple and green.</td>
</tr>
<tr>
<td>Orange</td>
<td>Orange</td>
<td>These are orange.</td>
</tr>
<tr>
<td>Banana</td>
<td>Yellow</td>
<td>These are yellow.</td>
</tr>
<tr>
<td>Kiwi</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Plum</td>
<td>Purple</td>
<td>These are Purple</td>
</tr>
<tr>
<td>Watermelon</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Tomato</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Cherry</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Cantelope</td>
<td>Orange</td>
<td>These are orange inside.</td>
</tr>
<tr>
<td>Honeydew</td>
<td>Green</td>
<td>These are green inside.</td>
</tr>
<tr>
<td>Papaya</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Raspberry</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Blueberry</td>
<td>Blue</td>
<td>These are blue.</td>
</tr>
<tr>
<td>Mango</td>
<td>Orange</td>
<td>These are orange.</td>
</tr>
<tr>
<td>Passion Fruit</td>
<td>Green</td>
<td>These are green.</td>
</tr>
</tbody>
</table>
Was mich verwirrt ist das, was das Element (vielleicht mein Verständnis völlig falsch sind, bitte korrigieren Sie mich), dass die prozentuale Breite auf td/th bezieht sich auf auf der Erde?
Typo in Ihrer 'Breite: cal (100% - 17px);'. Es muss "calc" nicht "cal" sein. – divy3993
Mögliches Duplikat von [HTML-Tabelle mit 100% Breite, mit vertikalem Scroll in tbody] (http://stackoverflow.com/questions/17067294/html-table-with-100-width-with-vertical-scroll-inside-tbody) –
@shubhamagrawalal Danke für Ihre Bereitstellung. Der Schlüssel hier ist, die Breite von th (s) gleich der Breite der entsprechenden Spalte anzupassen. Aber ich verstehe immer noch nicht, warum die relative Breite (Prozent) auf th/td nicht funktioniert? – xiyu