CSS - 表格单元格边距?

时间:2018-04-05 10:31:55

标签: html css

我认为这很容易,但我被困了

https://codepen.io/anon/pen/JLaqWw

表的左侧我需要在tr的左边有一个边距,但标题下的线需要到达表的边缘。

enter image description here

我已经在我的例子中通过positiong在线上方和下方的20px宽度div完成了它。

我显然不能这样做,但是我不知道该怎么做。

body{
  background: grey;
  font-family: sans-serif;
}
.page{
  background: white;
  width: 1200px;
  margin: 0 auto;
}
.table-con{
  overflow: scroll;
  max-width: 1200px;
}

.white-block{
  width: 10px;
  height: 150px;
  background: white;
  position: absolute;
  top: 50px;
}

.white-block-1{
  height: 20px;
  top: 20px;
}


table{
  /*table-layout: fixed;*/
  border-collapse: collapse; 
  width: 1500px;
}

thead th{
  text-align: left;
}

thead tr{
  border-bottom: 1px solid #444;
}

tbody tr{
  border-bottom: 1px solid #aaa;
}

tbody tr:nth-child(even){
  background: yellow;
}

th, td{
  padding: 10px 5px;

   &:first-of-type{
    padding-left: 20px;
  }

  &:last-of-type{
    padding-right: 20px;
  }
}

1 个答案:

答案 0 :(得分:1)

在CSS中,总有很多方法可以达到任何预期的效果。在你的情况下,我现在能想到的最简单的就是添加额外的列作为左边距。

我修改了你的小提琴并成功实现了你想要的东西:
https://codepen.io/anon/pen/KoxEvb

我添加了其他列并更改了样式。请注意,我已将背景色从tr移至td。我总是发现tr样式与浏览器有关,所以我尽量避免使用它。

此外,还有一些必须解决的特殊问题,更多信息,请阅读:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

相关问题