css3 - how to make table header fixed in html without using thead tag? -


i have following table not have thead:

    <table>       <tbody>        <tr class="header">         <th>head 1</th>         <th>head 2</th>         <th>head 3</th>        </tr>        <tr>         <td>body</td>         <td>body</td>         <td>body</td>        </tr>        <tr>         <td>body</td>         <td>body</td>         <td>body</td>        </tr>        <tr>         <td>body</td>         <td>body</td>         <td>body</td>        </tr>       </tbody>      </table>    css:    .table tr td {      overflow-y: auto      }  .table tr.header th {      display: block;  }

so how fix table header in above table.

the solution make main body display: block, can use overflow: auto on it. want keep .header dont scroll other <tr>s need move .header out.

i have updated jsfiddle if can accept move .header out of <tbody>.

jsfiddle

.fixed_headers td:nth-child(1), .fixed_headers th:nth-child(1) {   min-width: 200px; } .fixed_headers td:nth-child(2), .fixed_headers th:nth-child(2) {   min-width: 200px; } .fixed_headers td:nth-child(3), .fixed_headers th:nth-child(3) {   width: 350px; } .fixed_headers {     width: 750px;     border-collapse: collapse; } .fixed_headers th, .fixed_headers td {     padding: 5px;     text-align: left; } .fixed_headers .header {     background-color: #333333;     color: #fdfdfd; } .fixed_headers tr {     display: block; } .fixed_headers tbody:last-of-type{     position: relative;     display: block;     overflow: auto;     width: 100%;     height: 300px; } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -