According to mdn display: table-column
should
behave like HTML elements.
But as far as I know, <col>
elements have no display mode, they're actually just a rough edge of html which defines style in markup. So what in the world is table-column
in css supposed to do?
The "table-column" display type means it acts like the <col> tag in HTML - i.e. an invisible element whose width* governs the width of the corresponding physical column of the enclosing table.
Setting display to table makes the element behave like a table. So you can make a replica of an HTML table without using the table element and corresponding elements such as tr and td . For example, in HTML, you can make a table with the <table> element and also a <div> , or any container of your choice.
The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.
Select names of tables in the grid, then right-click and select Display Rows from the shortcut menu, or select Display Rows from the File menu. You can use ctrl or shift to select more than one table name, or use the Select All and Invert Selection options on the Edit menu or the grid shortcut menu.
<col>
elementsBecause <col>
elements belong to no content category and they aren't permitted to contain any content, they are essentially removed from the flow and their content ignored (exactly as if they had display: none
).
Unlike display: none
, when properly applied to children of a column-group, the table-column
keyword may be used to affect the styles of cells (even simulated ones) associated with columns within that group.
According to CSS 2.1 §17.2 The CSS table model:
Elements with
display
set totable-column
ortable-column-group
are not rendered (exactly as if they haddisplay: none
), but they are useful, because they may have attributes which induce a certain style for the columns they represent.
According to CSS 2.1 §17.3 Columns:
Table cells may belong to two contexts: rows and columns. However, in the source document cells are descendants of rows, never of columns. Nevertheless, some aspects of cells can be influenced by setting properties on columns.
The following properties apply to
column
andcolumn-group
elements: [border
,background
,width
,visibility
]
For example:
.table {
display: table;
table-layout: fixed;
border-collapse: collapse;
width: 100%;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
border: 1px solid silver;
padding: 0.5em;
}
.table-colgroup {
display: table-column-group;
}
.table-col {
display: table-column;
}
.col-1 {
/* Invalid. See: CSS 2.1 §17.3 Columns */
text-align: center;
}
.col-2 {
/* Valid. See: CSS 2.1 §17.3 Columns */
background-color: tomato;
}
<div class="table">
<div class="table-colgroup">
<div class="table-col col-1">Col 1 (hidden content)</div>
<div class="table-col col-2">Col 2 (hidden content)</div>
</div>
<div class="table-row">
<div class="table-cell">Row 1, Col 1</div>
<div class="table-cell">Row 1, Col 2 </div>
</div>
<div class="table-row">
<div class="table-cell">Row 2, Col 1</div>
<div class="table-cell">Row 2, Col 2</div>
</div>
<div class="table-row">
<div class="table-cell">Row 3, Col 1</div>
<div class="table-cell">Row 3, Col 2</div>
</div>
</div>
Despite the existence of the table-*
display properties, using non-semantic HTML for tables is bad for accessibility because it may cause assistive technology such as screen readers to misinterpret the data.
According to the Web Accessibility Initiative (WAI) Tables Tutorial:
Data tables are used to organize data with a logical relationship in grids. Accessible tables need HTML markup that indicates header cells and data cells and defines their relationship. Assistive technologies use this information to provide context to users.
If you are tempted to use non-semantic markup and CSS to build a table, I suggest reading Tables, CSS Display Properties, and ARIA | Adrian Roselli
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With