CSS properties - table-layout

revision:


table-layout property

- defines the algorithm used to lay out table cells, rows, and columns.

The main benefit of "table-layout: fixed;" is that the table renders much faster. On large tables, users will not see any part of the table until the browser has rendered the whole table. So, if you use "table-layout: fixed", users will see the top of the table while the browser loads and renders rest of the table. This gives the impression that the page loads a lot quicker!.

CSS syntax : table-layout: auto | fixed | initial | inherit;

Property values:

auto : browsers use an automatic table layout algorithm. The column width is set by the widest unbreakable content in the cells. The content will dictate the layout

fixed : sets a fixed table layout algorithm. The table and column widths are set by the widths of table and col or by the width of the first row of cells. Cells in other rows do not affect column widths. If no widths are present on the first row, the column widths are divided equally across the table, regardless of content inside the cells

initial : sets this property to its default value.

inherit : inherits this property from its parent element.

JavaScript syntax: object.style.tableLayout="fixed"

example: table-layout property
Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Island Trading Helen Bennett UK
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

table-layout: fixed; width: 180px:

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Island Trading Helen Bennett UK
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

table-layout: auto; width: 100%:

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Island Trading Helen Bennett UK
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

table-layout: fixed; width: 100%:

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Island Trading Helen Bennett UK
Magazzini Alimentari Riuniti Giovanni Rovelli Italy
code:
                  <div>
                      <table class="a">
                          <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                          </tr>
                          <tr>
                            <td>Alfreds Futterkiste</td>
                            <td>Maria Anders</td>
                            <td>Germany</td>
                          </tr>
                          <tr>
                            <td>Island Trading</td>
                            <td>Helen Bennett</td>
                            <td>UK</td>
                          </tr>
                          <tr>
                            <td>Magazzini Alimentari Riuniti</td>
                            <td>Giovanni Rovelli</td>
                            <td>Italy</td>
                          </tr>
                        </table>
                        
                        <h4>table-layout: fixed; width: 180px:</h4>
                        <table class="b">
                          <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                          </tr>
                          <tr>
                            <td>Alfreds Futterkiste</td>
                            <td>Maria Anders</td>
                            <td>Germany</td>
                          </tr>
                          <tr>
                            <td>Island Trading</td>
                            <td>Helen Bennett</td>
                            <td>UK</td>
                          </tr>
                          <tr>
                            <td>Magazzini Alimentari Riuniti</td>
                            <td>Giovanni Rovelli</td>
                            <td>Italy</td>
                          </tr>
                        </table>
                        
                        <h4>table-layout: auto; width: 100%:</h4>
                        <table class="c">
                          <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                          </tr>
                          <tr>
                            <td>Alfreds Futterkiste</td>
                            <td>Maria Anders</td>
                            <td>Germany</td>
                          </tr>
                          <tr>
                            <td>Island Trading</td>
                            <td>Helen Bennett</td>
                            <td>UK</td>
                          </tr>
                          <tr>
                            <td>Magazzini Alimentari Riuniti</td>
                            <td>Giovanni Rovelli</td>
                            <td>Italy</td>
                          </tr>
                        </table>
                        
                        <h4>table-layout: fixed; width: 100%:</h4>
                        <table class="d">
                          <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                          </tr>
                          <tr>
                            <td>Alfreds Futterkiste</td>
                            <td>Maria Anders</td>
                            <td>Germany</td>
                          </tr>
                          <tr>
                            <td>Island Trading</td>
                            <td>Helen Bennett</td>
                            <td>UK</td>
                          </tr>
                          <tr>
                            <td>Magazzini Alimentari Riuniti</td>
                            <td>Giovanni Rovelli</td>
                            <td>Italy</td>
                          </tr>
                        </table>
          
                  </div>
                  <style>
                      table {border-collapse: collapse; border: 1px solid black;} 
                      th,td {border: 1px solid black;}
                      table.a {table-layout: auto; width: 180px;}
                      table.b {table-layout: fixed; width: 180px;}
                      table.c {table-layout: auto; width: 100%;}
                      table.d {table-layout: fixed; width: 100%;}
                      
                  </style>