如何在滚动时保留HTML表中固定列的右边框?

huangapple go评论62阅读模式
英文:

How can I preserve the right border of a fixed column in HTML tables while scrolling?

问题

Here is the translated code portion:

.tableFixHead table {
  border-collapse: collapse;
  width: 100%;
}

.tableFixHead th,
.tableFixHead td {
  padding: 8px 16px;
}

td:first-child,
th:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background-color: white;
}

td:nth-child(2),
th:nth-child(2) {
  position: sticky;
  left: 40px;
  z-index: 1;
  background-color: white;
  border-right: 1px solid black;
}

.tableFixHead th {
  position: sticky;
  top: 0;
  background: #eee;
  z-index: 2
}

th:first-child,
th:nth-child(2) {
  z-index: 3
}

Please note that the HTML part of your code is already in English, so there's no need to translate it. If you have any further questions or need more assistance, feel free to ask.

英文:

sandbox: https://codepen.io/test41564/pen/mdzaRrV

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tableFixHead table {
  border-collapse: collapse;
  width: 100%;
}

.tableFixHead th,
.tableFixHead td {
  padding: 8px 16px;
}

td:first-child,
th:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background-color: white;
}

td:nth-child(2),
th:nth-child(2) {
  position: sticky;
  left: 40px;
  z-index: 1;
  background-color: white;
  border-right: 1px solid black;
}

.tableFixHead th {
  position: sticky;
  top: 0;
  background: #eee;
  z-index: 2
}

th:first-child,
th:nth-child(2) {
  z-index: 3
}

<!-- language: lang-html -->

&lt;div class=&quot;tableFixHead&quot;&gt;
  &lt;table class=&quot;table table-bordered&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;#&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;01-11&lt;/th&gt;
        &lt;th&gt;02-11&lt;/th&gt;
        &lt;th&gt;03-11&lt;/th&gt;
        &lt;th&gt;04-11&lt;/th&gt;
        &lt;th&gt;05-11&lt;/th&gt;
        &lt;th&gt;06-11&lt;/th&gt;
        &lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;Jack&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;2&lt;/td&gt;
        &lt;td&gt;Michel&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;Amanda&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;Amanda&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

I've tried a few tricks such as trying to set:

td:nth-child(2),
th:nth-child(2) { 
  position: sticky;
  border-right: 1px solid black;
}

But this doesn't get preserved while scrolling. I've tried to use pseudo elements like ::before which I think is the key to do this but I'm unable to figure this out. I've even asked AI for help but nope, so now I'm trying stackoverflow if anyone else here knows whether this is possible.

答案1

得分: 0

这是您提供的内容的翻译:

这是因为 border-collapse: collapse;

thtd 与它们的兄弟元素共享边框。

尝试使用 border-collapse: separate;

英文:

That's because border-collapse: collapse;.

th & td share borders with their siblings.

Try border-collapse: separate;.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tableFixHead table {
  border-collapse: separate;
  width: 100%;
}

.tableFixHead th,
.tableFixHead td {
  padding: 8px 16px;
}

td:first-child,
th:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background-color: white;
}

td:nth-child(2),
th:nth-child(2) {
  position: sticky;
  left: 40px;
  z-index: 1;
  background-color: white;
  border-right: 1px solid black;
}

.tableFixHead th {
  position: sticky;
  top: 0;
  background: #eee;
  z-index: 2
}

th:first-child,
th:nth-child(2) {
  z-index: 3
}

<!-- language: lang-html -->

&lt;div class=&quot;tableFixHead&quot;&gt;
  &lt;table class=&quot;table table-bordered&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;#&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;01-11&lt;/th&gt;
        &lt;th&gt;02-11&lt;/th&gt;
        &lt;th&gt;03-11&lt;/th&gt;
        &lt;th&gt;04-11&lt;/th&gt;
        &lt;th&gt;05-11&lt;/th&gt;
        &lt;th&gt;06-11&lt;/th&gt;
        &lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;Jack&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;2&lt;/td&gt;
        &lt;td&gt;Michel&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;Amanda&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;Amanda&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
        &lt;td&gt;Y&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 09:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定