skip to Main Content

I have a table with fixed layout, it has two rows.
In the first the width is working as they are 50%-45%-5%.
In the second row I tried to set it to 30%-20%-50% but it does not work.

I tried by removing table-layout: fixed and it works. But I’d need it to work with it on both rows.

enter image description here

expected result: enter image description here

table {
  border-collapse: collapse;
  position: relative;
  table-layout: fixed;
}
<table cellspacing="0" cellpadding="0" style="margin-bottom: 20px;" width="100%">
  <tbody>
    <tr>
      <td colspan="2" style="font-size: 1.1rem;" width="50%">
        <b>Customer Sales Order</b>
      </td>
      <td width="45%">
        <table width="100%">
          <tbody>
            <tr>
              <td width="20%">Delivery date:</td>
              <td>22/03/2024</td>
            </tr>
          </tbody>
        </table>
      </td>
      <td width="5%">
        <table width="100%">
          <tbody>
            <tr>
              <td>Page</td>
              <td>1/1</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <td width="30%">
        <table width="100%">
          <tbody>
            <tr>
              <td>Account:</td>
              <td>&nbsp;</td>
              <td>325535</td>
            </tr>
            <tr>
              <td>VAT Number:</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>Fiscal Code:</td>
              <td>&nbsp;</td>
              <td>{bank}</td>
            </tr>
            <tr>
              <td>Port:</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>Inv1:</td>
              <td>1</td>
              <td>{inv}</td>
            </tr>
            <tr>
              <td>Inv2:</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </tbody>
        </table>
      </td>
      <td width="20%">
        <table width="100%">
          <tbody>
            <tr>
              <td>Categ.:</td>
              <td>{categ}</td>
            </tr>
            <tr>
              <td>Place:</td>
              <td>{place}</td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
          </tbody>
        </table>
      </td>
      <td colspan="2" width="50%">
        <table width="100%">
          <tbody>
            <tr>
              <td width="10%">Payment:</td>
              <td style="text-align:center" width="20%">31</td>
              <td width="70%">&nbsp;</td>
            </tr>
            <tr>
              <td width="10%">Bank:</td>
              <td style="text-align:center" width="20%">&nbsp;</td>
              <td width="70%">{bank}</td>
            </tr>
            <tr>
              <td width="10%">Agent:</td>
              <td style="text-align:center" width="20%">&nbsp;</td>
              <td width="70%">{bank}</td>
            </tr>
            <tr>
              <td width="10px">Curr.:</td>
              <td style="text-align:center" width="20%">&nbsp;</td>
              <td width="70%">{curr}</td>
            </tr>
            <tr>
              <td width="10%">D/i type:</td>
              <td style="text-align:center" width="20%">1</td>
              <td width="70%">{type}</td>
            </tr>
            <tr>
              <td width="10%">S.ord.Inv.:</td>
              <td style="text-align:center" width="20%">&nbsp;</td>
              <td width="70%">{s_ord_Inv};</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

2

Answers


  1. It seems like you’re trying to create a table with specific column widths in each row, but you’re encountering issues with the widths not behaving as expected when using table-layout: fixed. This is likely because when you set a fixed layout for the table, the browser tries to distribute the widths of the columns evenly based on the first row’s column widths.

    To achieve different column widths in each row while still maintaining a fixed layout, you can explicitly set the width for each cell in the table. Here’s how you can modify your code to achieve this:

    <style>
      table {
        border-collapse: collapse;
        position: relative;
        table-layout: fixed;
        width: 100%;
      }
      td {
        border: 1px solid #ccc;
        padding: 8px;
      }
    </style>
    
    <table cellspacing="0" cellpadding="0">
      <tbody>
        <tr>
          <td colspan="2" style="font-size: 1.1rem; width: 50%;">
            <b>Customer Sales Order</b>
          </td>
          <td style="width: 45%;">
            <table width="100%">
              <tbody>
                <tr>
                  <td width="20%">Delivery date:</td>
                  <td>22/03/2024</td>
                </tr>
              </tbody>
            </table>
          </td>
          <td style="width: 5%;">
            <table width="100%">
              <tbody>
                <tr>
                  <td>Page</td>
                  <td>1/1</td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
        <tr>
          <td style="width: 30%;">
            <table width="100%">
              <tbody>
                <tr>
                  <td>Account:</td>
                  <td>&nbsp;</td>
                  <td>325535</td>
                </tr>
                <!-- Other rows... -->
              </tbody>
            </table>
          </td>
          <td style="width: 20%;">
            <table width="100%">
              <tbody>
                <tr>
                  <td>Categ.:</td>
                  <td>{categ}</td>
                </tr>
                <!-- Other rows... -->
              </tbody>
            </table>
          </td>
          <td colspan="2" style="width: 50%;">
            <table width="100%">
              <tbody>
                <tr>
                  <td width="10%">Payment:</td>
                  <td style="text-align:center" width="20%">31</td>
                  <td width="70%">&nbsp;</td>
                </tr>
                <!-- Other rows... -->
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    Login or Signup to reply.
  2. You could use colgroup to define the width of each individual column. This way each column gets assigned a fix width. Inside a specific row you can then use colspan to merge cells according to your needs:

    table {
      border-collapse: collapse;
      position: relative;
      table-layout: fixed;
    }
    <table cellspacing="0" cellpadding="0" style="margin-bottom: 20px;" width="100%">
    <colgroup>
      <col width="30%">
      <col width="20%">
      <col width="45%">
      <col width="5%">
    </colgroup>
      <tbody>
    <tr>
          <td colspan="2" style="font-size: 1.1rem;">
            <b>Customer Sales Order</b>
          </td>
          <td>
            <table width="100%">
              <tbody>
                <tr>
                  <td width="20%">Delivery date:</td>
                  <td>22/03/2024</td>
                </tr>
              </tbody>
            </table>
          </td>
          <td>
            <table width="100%">
              <tbody>
                <tr>
                  <td>Page</td>
                  <td>1/1</td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
        <tr>
          <td>
            <table width="100%">
              <tbody>
                <tr>
                  <td>Account:</td>
                  <td>&nbsp;</td>
                  <td>325535</td>
                </tr>
                <tr>
                  <td>VAT Number:</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>Fiscal Code:</td>
                  <td>&nbsp;</td>
                  <td>{bank}</td>
                </tr>
                <tr>
                  <td>Port:</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>Inv1:</td>
                  <td>1</td>
                  <td>{inv}</td>
                </tr>
                <tr>
                  <td>Inv2:</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
              </tbody>
            </table>
          </td>
          <td>
            <table width="100%">
              <tbody>
                <tr>
                  <td>Categ.:</td>
                  <td>{categ}</td>
                </tr>
                <tr>
                  <td>Place:</td>
                  <td>{place}</td>
                </tr>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
              </tbody>
            </table>
          </td>
          <td colspan="2">
            <table width="100%">
              <tbody>
                <tr>
                  <td width="10%">Payment:</td>
                  <td style="text-align:center" width="20%">31</td>
                  <td width="70%">&nbsp;</td>
                </tr>
                <tr>
                  <td width="10%">Bank:</td>
                  <td style="text-align:center" width="20%">&nbsp;</td>
                  <td width="70%">{bank}</td>
                </tr>
                <tr>
                  <td width="10%">Agent:</td>
                  <td style="text-align:center" width="20%">&nbsp;</td>
                  <td width="70%">{bank}</td>
                </tr>
                <tr>
                  <td width="10px">Curr.:</td>
                  <td style="text-align:center" width="20%">&nbsp;</td>
                  <td width="70%">{curr}</td>
                </tr>
                <tr>
                  <td width="10%">D/i type:</td>
                  <td style="text-align:center" width="20%">1</td>
                  <td width="70%">{type}</td>
                </tr>
                <tr>
                  <td width="10%">S.ord.Inv.:</td>
                  <td style="text-align:center" width="20%">&nbsp;</td>
                  <td width="70%">{s_ord_Inv};</td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search