-1

I am working on an angular 8 project at work. I have a page that displays a table. As per the current code the width of each column was set like below. Widths are only set for th and not for td

.data-table tr th:nth-child(1) {
  width: 20%;
}
.data-table tr th:nth-child(2) {
  width: 10%;
}
.data-table tr th:nth-child(3) {
  width: 16%;
}
.data-table tr th:nth-child(4) {
  width: 18%;
}
.data-table tr th:nth-child(5) {
  width: 18%;
}
.data-table tr th:nth-child(6) {
  width: 15%;
}
.data-table tr th:nth-child(7) {
  width: 5%;
}

First column in table is Title. I want width of title to be set such that it fits the longest Title in every row. Is there a way to do this without Javascript??

Amruta
  • 1,295
  • 1
  • 13
  • 24

1 Answers1

0

You can also do that in less code.

.data-table tr th:nth-child(1) {
  min-width: 20%;
}

.data-table tr th:nth-child(2), .data-table tr th:nth-child(3), .data-table tr th:nth-child(4), .data-table tr th:nth-child(5), .data-table tr th:nth-child(6) {
  max-width: 15%;
}

.data-table tr th:nth-child(7) {
  width: 5%;
}

SHORT VERSION

.data-table tr th:first-child {
  min-width: 20%;
}

.data-table tr th:not(:first-child):not(:last-child) {
  max-width: 15%;
}

.data-table tr th:last-child {
  width: 5%;
}
Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73