Table Grid Box

May 20, 2023

A table grid box is a type of CSS box that is created using the display: table property. It is part of the CSS Table Layout Module, which allows developers to create layouts similar to those found in HTML tables, but without using the <table> element. The purpose of the table grid box is to provide a flexible and easy-to-use way to create complex layouts using CSS.

Usage

The table grid box is created by applying the display: table property to a container element. This tells the browser to treat the element as if it were a table. The container element is then populated with child elements that are treated as table rows (display: table-row) and table cells (display: table-cell). These child elements can be styled using CSS, just as if they were part of an HTML table.

The table grid box is particularly useful for creating layouts with columns of equal width, as this can be accomplished simply by setting the width property on the table cells. It also allows for easy vertical alignment of content, as the vertical-align property can be used on table cells to align their content with each other.

Another advantage of using the table grid box is that it is not subject to the same quirks and inconsistencies that can arise when using the <table> element. This makes it a good choice for creating layouts that need to be consistent across different browsers and devices.

Example

Here is an example of a simple layout created using the table grid box:

<div class="container">
  <div class="row">
    <div class="cell">Column 1</div>
    <div class="cell">Column 2</div>
    <div class="cell">Column 3</div>
  </div>
  <div class="row">
    <div class="cell">Lorem ipsum dolor sit amet</div>
    <div class="cell">consectetur adipiscing elit</div>
    <div class="cell">sed do eiusmod tempor incididunt</div>
  </div>
  <div class="row">
    <div class="cell">Ut enim ad minim veniam</div>
    <div class="cell">quis nostrud exercitation ullamco</div>
    <div class="cell">laboris nisi ut aliquip ex ea commodo consequat</div>
  </div>
</div>
.container {
  display: table;
  width: 100%;
}
.row {
  display: table-row;
}
.cell {
  display: table-cell;
  padding: 10px;
  border: 1px solid black;
  vertical-align: middle;
}

In this example, we have a container element with three child elements that are styled as table cells. The display: table property on the container element tells the browser to treat it as a table, while the display: table-row and display: table-cell properties on the child elements tell the browser to treat them as table rows and cells, respectively.

The width property on the table cells is set to 33.33%, which ensures that each column is of equal width. The padding and border properties on the table cells are used to add some spacing and visual separation between the content.

Finally, the vertical-align property on the table cells is set to middle, which vertically centers the content within each cell.

Compatibility

The table grid box is supported by all modern browsers, as well as Internet Explorer 8 and above. However, there are some minor differences in how different browsers handle certain aspects of the table grid box, so it is important to test your layouts across multiple browsers to ensure consistency.