Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does display: table-column do?

Tags:

css

css-tables

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?

like image 437
George Mauer Avatar asked Jun 11 '15 14:06

George Mauer


People also ask

What is display table column?

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.

What does display table cell do?

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.

What does display mean in CSS?

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.

How do you display a table?

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.


1 Answers

It allows you to treat other HTML elements as <col> elements

Because <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 to table-column or table-column-group are not rendered (exactly as if they had display: 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 and column-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>

Accessibility concerns

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

like image 89
gfullam Avatar answered Sep 29 '22 12:09

gfullam