我正在尝试制作带有粘性标题的表。我快到了,但是由于某种原因,标题中的最后一列的宽度比表格要大2px。如果运行下面的代码,您将看到日期列的边缘在表格边框的外面有点粘住。其他一切都完美地排列在一起。
关于如何解决此问题的任何建议?
*,
*::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>
答案 0 :(得分:1)
将height
和overflow
从.table__wrapper
移到.table__sticky
似乎可以解决您的问题。
.table__sticky {
position: relative;
height: 70vh;
overflow-y: scroll;
}
答案 1 :(得分:0)
我通过在position: sticky
元素上使用th
来解决此问题,而不是使用position: absolute
并在th
元素内对div进行“欺骗”。现在一切正常。下面的工作代码。
*,
*::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__wrapper {
overflow-x: auto;
overflow-y: scroll;
white-space: nowrap;
width: 100%;
height: 70vh;
display: block;
}
.table__top__overlay {
min-height: 1rem;
min-width: 100%;
background-color: #fff;
z-index: 100;
position: relative;
margin-bottom: -0.6px;
}
.table thead tr th {
position: sticky;
position: -webkit-sticky;
top: 0;
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 thead tr th:hover {
box-shadow: inset 0 3.4rem 0 -1.7rem #43484d;
background-color: #2d3338;
}
.table td {
border: 1px solid #43484d;
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_25 {
width: 25%;
}
.table .row_width_30 {
width: 30%;
}
.table .row_width_35 {
width: 35%;
}
.table .row_width_40 {
width: 40%;
}
.table .row_width_45 {
width: 45%;
}
.table .row_width_50 {
width: 50%;
}
<div class="table__top__overlay">
</div>
<div class="table__wrapper">
<table class="table">
<thead>
<tr>
<th class="row_width_5">
Id
</th>
<th class="row_width_10">
User (id)
</th>
<th class="row_width_10">
Ip
</th>
<th class="row_width_20">
Referrer
</th>
<th class="row_width_10">
Browser
</th>
<th class="row_width_10">
Platform
</th>
<th class="row_width_20">
Page
</th>
<th class="row_width_15">
Date
</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>6227</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>IE</td>
<td>Win10</td>
<td>N/A</td>
<td>2018-06-19 13:48:48</td>
</tr>
<tr>
<td>6226</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://www.rajohan.org/</td>
<td>Chrome</td>
<td>macOS</td>
<td>N/A</td>
<td>2018-06-19 12:12:57</td>
</tr>
<tr>
<td>6225</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Python-urllib</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 11:29:48</td>
</tr>
<tr>
<td>6224</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 11:12:39</td>
</tr>
<tr>
<td>6223</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Applebot</td>
<td>MacOSX</td>
<td>/newsletter/</td>
<td>2018-06-19 09:56:43</td>
</tr>
<tr>
<td>6222</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>http://172.104.225.59:80/</td>
<td>Default Browser</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 08:54:54</td>
</tr>
<tr>
<td>6221</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>/error.html</td>
<td>2018-06-19 07:53:54</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6220</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>N/A</td>
<td>2018-06-19 07:53:53</td>
</tr>
<tr>
<td>6219</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 07:05:55</td>
</tr>
<tr>
<td>6218</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>Android</td>
<td>N/A</td>
<td>2018-06-19 06:20:15</td>
</tr>
<tr>
<td>6217</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 05:24:28</td>
</tr>
<tr>
<td>6216</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Chrome</td>
<td>MacOSX</td>
<td>N/A</td>
<td>2018-06-19 04:43:10</td>
</tr>
<tr>
<td>6215</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Applebot</td>
<td>MacOSX</td>
<td>/register/</td>
<td>2018-06-19 03:55:35</td>
</tr>
<tr>
<td>6214</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 03:45:36</td>
</tr>
<tr>
<td>6213</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 02:20:35</td>
</tr>
<tr>
<td>6214</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 03:45:36</td>
</tr>
<tr>
<td>6213</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 02:20:35</td>
</tr>
<tr>
<td>6214</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 03:45:36</td>
</tr>
<tr>
<td>6213</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 02:20:35</td>
</tr>
<tr>
<td>6214</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 03:45:36</td>
</tr>
<tr>
<td>6213</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 02:20:35</td>
</tr>
<tr>
<td>6214</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Baiduspider</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 03:45:36</td>
</tr>
<tr>
<td>6213</td>
<td>N/A (0)</td>
<td>111.111.111.111</td>
<td>N/A</td>
<td>Google Bot</td>
<td>unknown</td>
<td>N/A</td>
<td>2018-06-19 02:20:35</td>
</tr>
</tbody>
</table>
</div>