The empty-cells property in CSS

css empty cells property introduction
10 November 2024

Introduction to the empty-cells property

In CSS, one of the lesser-known and less commonly used properties is empty-cells. This property is used to control the display or non-display of empty cells in tables. Sometimes in the design of large and complex tables, there are cells that may remain empty. By using the empty-cells property, you can determine whether these cells should be displayed or not.

How to use empty-cells

This property works with two values, show and hide. The default value is show, which means that empty cells will be displayed. On the other hand, the hide value causes these cells to be hidden and have no visible effect on the table design. In this case, the functionality and effect of this property are more effective in modern browsers, and older browsers may not work well with it.

Sample Code

Now let’s take a look at a simple example of how to use the empty-cells property.


<style>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  td {
    border: 1px solid black;
    height: 50px;
  }
  .hide-empty-cells {
    empty-cells: hide;
  }
</style>
<table class="hide-empty-cells">
  <tr>
    <td>Cell 1</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>Cell 4</td>
  </tr>
</table>

Line-by-Line Explanation of the Code

<style>
These tags contain CSS styles directly within themselves.
table { ... }
Using these styles, general styles for tables such as border and width are defined.
td { ... }
Here, styles related to the table cells such as border and height are applied.
.hide-empty-cells { empty-cells: hide; }
In this line, we define that empty cells for the class hide-empty-cells should be hidden.
<table class="hide-empty-cells">
This creates a table with the class hide-empty-cells which will cause empty cells to be hidden.
<tr>...</tr>
A new row in the table, which includes two cells, one filled and one empty.

FAQ

?

In which browsers is the empty-cells property supported?

?

How can I use empty-cells for a specific table?

?

Does empty-cells have an effect on the entire table?