table-layout

The table-layout property defines what algorithm the browser should use to lay out table rows, cells, and columns.

table {
  table-layout: fixed;
}

As explained in the CSS2.1 specification, table layout in general is usually a matter of taste and will vary depending on design choices. Browsers will, however, automatically apply certain constraints that will define how tables are laid out. This happens when the table-layout property is set to auto (the default). But these constraints can be lifted when table-layout is set to fixed.

Values

  • auto: the default. The browser's automatic algorithm is used to define how a table's rows, cells, and columns are laid out. The resulting table layout is generally dependent on the content of the table.
  • fixed: With this value, the table's layout ignores the content and instead uses the table's width, any specified width of columns, and border and cell spacing values. The column values used are based on widths defined on columns or cells for the first row of the table.
  • inherit: indicates that the value is inherited from the table-layout value of its parent

In order for a value of fixed to have any effect, the table's width has to be set to something other than auto (the default for the width property). In the demos below, all the table widths are set to 100%, which assumes we want the table to fill its parent container horizontally.

The best way to see the the effects of a fixed table layout algorithm is using a demo.

See the Pen Demo for CSS's table-layout property by Louis Lazaris (@impressivewebs) on CodePen.

When you first view the above demo, you'll notice the layout of the table columns is unbalanced and awkward. At that point, the table is using the browser's default algorithm to define how to lay out the table, which means the content will dictate the layout. The demo exaggerates this fact by including a long string of text inside one table cell, while all the other table cells use just two words each. As you can see, the browser expands the first column to accommodate the larger bit of content.

If you click the "Toggle table-layout: fixed" button, you'll see what the table layout looks like when the "fixed" algorithm is used. When table-layout: fixed is applied, the content no longer dictates the layout, but instead, the browser uses any defined widths from the table's first row to define 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.

Further examples may help make this clearer. In the following demo, the table has a <colgroup> element whose first <col> element has a width of 400px. Notice in this case, toggling table-layout: fixed has no effect.

See the Pen Demo for CSS's table-layout property by Louis Lazaris (@impressivewebs) on CodePen.

This happens because the default layout algorithm is essentially saying "make the first column 400px wide, and distribute the remaining columns based on their content". Since the other three columns have the same content as each other, there will be no change. But now let's add some extra text content to one of the other columns:

See the Pen Demo for CSS's table-layout property with col width and variable content by Louis Lazaris (@impressivewebs) on CodePen.

Now if you click the toggle button, you'll see the columns adjust to accommodate a fixed layout, irrespective of content. Once again, the same thing is happening; the first column is set at 400px then the remaining columns are divided equally. But this time, because one of the columns has extra content, the difference is noticeable.

How a fixed layout algorithm determines column widths

The following two demos should help in understanding that the first row of the table is what helps to define the column widths of a table set to table-layout: fixed.

See the Pen Demo for CSS's table-layout with cell in row 1 given defined width by Louis Lazaris (@impressivewebs) on CodePen.

In the above demo, the first cell in the table's first row has a width of 350px. Toggling table-layout: fixed adjusts the other columns, but the first one remains the same. Now try the following demo:

See the Pen Demo for CSS's table-layout with cell in row 2 given defined width by Louis Lazaris (@impressivewebs) on CodePen.

In this case, it's the second row that has a width attached to its first table cell. Now when the toggle button is clicked, all the column widths are adjusted. Again, this is because the fixed layout algorithm is using the first row to determine the column widths, and the end result is that it distributes the widths equally.

Benefits of a fixed layout algorithm

The aesthetic benefits of using table-layout: fixed should be clear from the demonstrations above. But the other major benefit is performance. The spec refers to the fixed algorithm as a "fast" algorithm, and for good reason. The browser does not need to analyze the entire table's content before determining the size of the columns; it only needs to analyze the first row. So the result is a faster processing of the table's layout.

Related Properties

More Information

Browser Support

Chrome Safari Firefox Opera IE Android iOS
1+ 1+ 1+ 7+ 5+ 2.1+ 3+
This comment thread is closed. If you have important information to share, please contact us.
icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag