如何为水平滚动条使用位置粘性?
How to use position sticky for horizontal scrollbar?
我需要根据 X 轴滚动 table。我的代码看起来像这样。
我也试过定位 fixeb 但由于某种原因它只在 Y 轴上滚动。
我做错了什么?
HTML
table {
margin-top: 20px;
width: 100%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
}
th {
font-weight: 700;
position: sticky;
}
<table>
<tr>
<th>Some header</th>
<td>1</td>
<td>2</td>
...
<td>20</td>
</tr>
<tr>
<th>Some header</th>
<td>1</td>
<td>2</td>
...
<td>20</td>
</tr>
<table></table>
</table>
我想你只需要第一栏的便签:
.wrapper {
max-width: 400px;
overflow-x: scroll;
}
table {
position: relative;
border: 1px solid #ddd;
border-collapse: collapse;
}
td, th {
white-space: nowrap;
border: 1px solid #ddd;
padding: 20px;
text-align: center;
}
td:first-of-type {
background-color: #eee;
position: sticky;
left: -1px;
text-align: left;
}
<div class="wrapper">
<table>
<tbody>
<tr>
<td>Some header</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>Some header</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
解决方案来自 CSS-Tricks 上的 post。
我需要根据 X 轴滚动 table。我的代码看起来像这样。 我也试过定位 fixeb 但由于某种原因它只在 Y 轴上滚动。 我做错了什么?
HTML
table {
margin-top: 20px;
width: 100%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
}
th {
font-weight: 700;
position: sticky;
}
<table>
<tr>
<th>Some header</th>
<td>1</td>
<td>2</td>
...
<td>20</td>
</tr>
<tr>
<th>Some header</th>
<td>1</td>
<td>2</td>
...
<td>20</td>
</tr>
<table></table>
</table>
我想你只需要第一栏的便签:
.wrapper {
max-width: 400px;
overflow-x: scroll;
}
table {
position: relative;
border: 1px solid #ddd;
border-collapse: collapse;
}
td, th {
white-space: nowrap;
border: 1px solid #ddd;
padding: 20px;
text-align: center;
}
td:first-of-type {
background-color: #eee;
position: sticky;
left: -1px;
text-align: left;
}
<div class="wrapper">
<table>
<tbody>
<tr>
<td>Some header</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>Some header</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
解决方案来自 CSS-Tricks 上的 post。