ttmt
ttmt

Reputation: 4984

CSS - Table cell margins?

I thought this would be easy but I'm stuck

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

The left side of the table I need a margin on the left of the tr but the line under the heading needs to go to the edge of the table.

enter image description here

I have done it in my example by positiong a 20px width div above and below the line.

I obviosuly cant do it this way but I'm stuck how I should do it.

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;
  }
}

Upvotes: 0

Views: 146

Answers (1)

Aleksander Stelmaczonek
Aleksander Stelmaczonek

Reputation: 1518

In CSS there is always a ton of ways to achieve any desired effect. In your case the simplest I can think of right now is just adding additional column that will act as left margin.

I have modified your fiddle and successfully achieved what you wanted:
https://codepen.io/anon/pen/KoxEvb

I added additional column and changed its styling. Please, note that I have moved background colouring from tr to td. I have always found tr styling to be browser dependant, so I try to avoid it if possible.

Additionally, there were a few specificity issues which had to be resolved, for more info, please, read this:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

Upvotes: 1

Related Questions