1

table, thead, tbody {
    width: 100%;
}

table, th, tr, td {
    border: 1px solid #000;
    border-collapse: collapse;
}

td {
   width: 25% !important;
}
<table>
    <thead>
        <th>
            <td>ID</td>
            <td>Nome da conta</td>
            <td>Titular</td>
            <td>Saldo</td>
        </th>
    </thead>
    <tbody>
     ...
    </tbody>
</table>

I have a table with four columns and I want them all the same width, i.e., each one occupying 25% of the table.

It happens that my columns are not taking 25% of the table width each, as I assumed they would. Even when I apply the !important.

enter image description here

4
  • Try setting th to 100% also. Commented Apr 27, 2018 at 10:51
  • 3
    Check your other web pages styles, because everyting works in plain HTML/CSS as you wrote it Commented Apr 27, 2018 at 10:52
  • 1
    Your code is invalid. table > thead > tr > td, not table > thead > th > td Commented Apr 27, 2018 at 10:53
  • @TadasStasiulionis I did it. In fact, I took all else out and it continues this way. Commented Apr 27, 2018 at 10:53

3 Answers 3

2

You didn't used the semantic tags in a correct manner. In thead tag you must have tr and th as it's children.

Then you can apply {width: 25%;} on the th

table,
thead,
tbody {
  width: 100%;
}

table,
th,
tr,
td {
  border: 1px solid #000;
  border-collapse: collapse;
}

th {
  width: 25% !important;
}
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Nome da conta</th>
      <th>Titular</th>
      <th>Saldo</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

With table-layout: fixed

table {
  table-layout: fixed;
  width: 100%;
}

table,
th,
tr,
td {
  border: 1px solid #000;
  border-collapse: collapse;
}

td {
  width: 25%;
}
<table>

  <tbody>
    <tr>
      <td>ID</td>
      <td>Nome da conta</td>
      <td>Titular</td>
      <td>Saldo</td>
    </tr>
  </tbody>
</table>

1 Comment

This is not related to table-layout: fixed;, but to the fact that you have made the markup correctly.
0

table {
  table-layout: fixed;
  width : 100% ;
}
td {
  text-align : center ;
  width: 25%;
}
<table border = "10">
    <tr>
      <td>Some Text</td>
      <td>Another Text</td>
      <td>Some Another Text</td>
      <td>A Very Long Text</td>
    </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.