mirror of
https://github.com/phuoc-ng/csslayout.git
synced 2025-08-05 05:37:29 +02:00
244 lines
5.8 KiB
Plaintext
244 lines
5.8 KiB
Plaintext
---
|
|
category: Display
|
|
created: '2019-12-29'
|
|
description: Create sticky table column with CSS
|
|
keywords: css position sticky, css sticky table column
|
|
thumbnail: /assets/css-layout/thumbnails/sticky-table-column.png
|
|
title: Sticky table column
|
|
---
|
|
|
|
## HTML
|
|
|
|
```html index.html
|
|
<table class="sticky-table-column">
|
|
<thead>
|
|
<tr>
|
|
<th class="sticky-table-column__sticky">...</th>
|
|
|
|
<!-- Other header column ... -->
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td class="sticky-table-column__sticky">...</td>
|
|
<!-- Other columns ... -->
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
```
|
|
|
|
## CSS
|
|
|
|
```css styles.css
|
|
.sticky-table-headers__sticky {
|
|
/* Background color */
|
|
background-color: #9ca3af;
|
|
|
|
/* Stick to the left */
|
|
left: 0;
|
|
position: sticky;
|
|
|
|
/* Displayed on top of other rows when scrolling */
|
|
z-index: 9999;
|
|
}
|
|
```
|
|
|
|
<Playground>
|
|
```css placeholders.css hidden
|
|
.lines {
|
|
padding: 0.25rem 0;
|
|
width: 100%;
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
flex-direction: column;
|
|
}
|
|
.line {
|
|
background: #d1d5db;
|
|
height: 1px;
|
|
margin-bottom: 0.25rem;
|
|
}
|
|
.line.line--20 {
|
|
width: 20%;
|
|
}
|
|
.line.line--40 {
|
|
width: 40%;
|
|
}
|
|
.line.line--60 {
|
|
width: 60%;
|
|
}
|
|
.line.line--80 {
|
|
width: 80%;
|
|
}
|
|
.line.line--100 {
|
|
width: 100%;
|
|
}
|
|
.rectangle {
|
|
background: #d1d5db;
|
|
border-radius: 0.25rem;
|
|
height: var(--rectangle-height);
|
|
width: var(--rectangle-width);
|
|
}
|
|
.rectangle--hor.rectangle--20 {
|
|
--rectangle-width: 20%;
|
|
}
|
|
.rectangle--hor.rectangle--40 {
|
|
--rectangle-width: 40%;
|
|
}
|
|
.rectangle--hor.rectangle--60 {
|
|
--rectangle-width: 60%;
|
|
}
|
|
.rectangle--hor.rectangle--80 {
|
|
--rectangle-width: 80%;
|
|
}
|
|
.rectangle--hor.rectangle--100 {
|
|
--rectangle-width: 100%;
|
|
}
|
|
.rectangle--hor.rectangle--sm {
|
|
--rectangle-height: 0.5rem;
|
|
}
|
|
.rectangle--hor.rectangle--md {
|
|
--rectangle-height: 2rem;
|
|
}
|
|
.rectangle--hor.rectangle--lg {
|
|
--rectangle-height: 4rem;
|
|
}
|
|
.rectangle--ver.rectangle--20 {
|
|
--rectangle-height: 20%;
|
|
}
|
|
.rectangle--ver.rectangle--40 {
|
|
--rectangle-height: 40%;
|
|
}
|
|
.rectangle--ver.rectangle--60 {
|
|
--rectangle-height: 60%;
|
|
}
|
|
.rectangle--ver.rectangle--80 {
|
|
--rectangle-height: 80%;
|
|
}
|
|
.rectangle--ver.rectangle--100 {
|
|
--rectangle-height: 100%;
|
|
}
|
|
.rectangle--ver.rectangle--sm {
|
|
--rectangle-width: 0.5rem;
|
|
}
|
|
.rectangle--ver.rectangle--md {
|
|
--rectangle-width: 2rem;
|
|
}
|
|
.rectangle--ver.rectangle--lg {
|
|
--rectangle-width: 4rem;
|
|
}
|
|
```
|
|
|
|
```css styles.css hidden
|
|
body {
|
|
height: 24rem;
|
|
}
|
|
|
|
.sticky-table-column {
|
|
/* Demo */
|
|
border-collapse: collapse;
|
|
width: 100%;
|
|
}
|
|
|
|
.sticky-table-column tbody {
|
|
border-bottom: 1px solid #d1d5db;
|
|
}
|
|
|
|
.sticky-table-column tr {
|
|
border-top: 1px solid #d1d5db;
|
|
}
|
|
|
|
.sticky-table-column__sticky {
|
|
/* Background color */
|
|
background-color: #9ca3af;
|
|
|
|
/* Stick to the left */
|
|
left: 0;
|
|
position: sticky;
|
|
|
|
/* Displayed on top of other rows when scrolling */
|
|
z-index: 9999;
|
|
|
|
padding: 0 0.25rem;
|
|
}
|
|
```
|
|
|
|
```html index.html hidden
|
|
<table class="sticky-table-column">
|
|
<tbody>
|
|
<tr>
|
|
<td class="sticky-table-column__sticky">
|
|
<div class="rectangle rectangle--hor rectangle--sm rectangle--100"></div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="sticky-table-column__sticky">
|
|
<div class="rectangle rectangle--hor rectangle--sm rectangle--100"></div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="sticky-table-column__sticky">
|
|
<div class="rectangle rectangle--hor rectangle--sm rectangle--100"></div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="lines">
|
|
<div class="line line--20"></div>
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
```
|
|
</Playground>
|