The position: sticky; property has gained traction in Webkit, enabling elements to remain fixed within their parent containers during scrolling. However, some developers have expressed the need to extend this functionality to scrolling div elements that contain tables.
Can sticky positioning be applied to table headers within a scrolling div?
Answer:
Indeed, position: sticky; can now be used for elements as of 2018!
Implementation: Simply add the following line to your CSS stylesheet: Prerequisites: Example Table Markup: Additional Options: Browser Support: As of March 2018, sticky positioning for thead elements is widely supported across modern browsers, thanks to the efforts of the developer community. The above is the detailed content of Can Sticky Headers be Used Within Scrolling Divs for Table Headers?. For more information, please follow other related articles on the PHP Chinese website!<code class="css">thead th { position: sticky; top: 0; }</code>
elements.
<code class="html"><table>
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th>column 3</th>
<th>column 4</th>
</tr>
</thead>
<tbody>
// body code
</tbody>
</table></code>
<code class="css">thead tr:first-child th { position: sticky; top: 0; }</code>