我想做一张有粘性页眉的桌子。我差不多到了,但是由于某些原因,标题中的最后一列的宽度比表的宽度多了大约2倍。如果您运行下面的代码,您将看到日期列的边缘在表边界之外有一点突出。其他的都排得很好。
有什么建议可以解决这个问题吗?
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
html {
font-size: 62.5%;
}
body {
box-sizing: border-box;
width: 90vw;
max-width: 100%;
position: relative;
min-height: 100vh;
max-height: 100%;
}
.table {
border-spacing: 0;
border-collapse: collapse;
width: 100%;
table-layout: fixed;
}
.table__wrapper {
overflow: auto;
white-space: nowrap;
width: 100%;
height: 70vh;
display: block;
}
.table__sticky {
position: relative;
}
.table__header {
top: -0.1rem;
position: absolute;
padding: 0.8rem;
border: 1px solid #43484d;
box-shadow: inset 0 3.4rem 0 -1.7rem #2d3338;
background-color: #22262a;
color: #fff;
text-transform: uppercase;
cursor: pointer;
font-size: 1.4rem;
}
.table__header:hover {
box-shadow: inset 0 3.4rem 0 -1.7rem #43484d;
background-color: #2d3338;
}
.table thead th {
padding: 1.5rem 0;
}
.table td {
border: 1px solid #959595;
padding: 0.5rem;
font-size: 1.3rem;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.table tbody:hover td {
color: transparent;
text-shadow: 0 0 0.3rem #aaa;
}
.table tbody:hover tr:hover td {
color: #363636;
text-shadow: none;
}
.table tr:nth-child(even) {
background-color: #eff1f2;
}
.table .row_width_5 {
width: 5%;
}
.table .row_width_10 {
width: 10%;
}
.table .row_width_15 {
width: 15%;
}
.table .row_width_20 {
width: 20%;
}
.table .row_width_30 {
width: 30%;
}
.table .row_width_40 {
width: 40%;
}
.table .row_width_50 {
width: 50%;
}
.table .row_width_60 {
width: 60%;
}
.table .row_width_70 {
width: 70%;
}
.table .row_width_80 {
width: 80%;
}
.table .row_width_90 {
width: 90%;
}
.table .row_width_100 {
width: 100%;
}
<div class="table__sticky">
<div class="table__wrapper">
<table class="table">
<thead>
<tr>
<th class="row_width_5">
<div class="table__header row_width_5">
Id
</div>
</th>
<th class="row_width_10">
<div class="table__header row_width_10">
User (id)
</div>
</th>
<th class="row_width_10">
<div class="table__header row_width_10">
Ip
</div>
</th>
<th class="row_width_20">
<div class="table__header row_width_20">
Referrer
</div>
</th>
<th class="row_width_10">
<div class="table__header row_width_10">
Browser
</div>
</th>
<th class="row_width_10">
<div class="table__header row_width_10">
Platform
</div>
</th>
<th class="row_width_20">
<div class="table__header row_width_20">
Page
</div>
</th>
<th class="row_width_15">
<div class="table__header row_width_15">
Date
</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
<tr>
<td>6228</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.no/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 14:30:06</td>
</tr>
</tbody>
</table>
</div>
</div>
最佳答案
从height
移动到overflow
似乎解决了您的问题。
.table__sticky {
position: relative;
height: 70vh;
overflow-y: scroll;
}