Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent collapse of empty rows in HTML table via CSS

I have a HTML table as follows:

<table border="1">
    <tr>
        <td>Row with text</td>
    </tr>
    <tr>
        <td></td><!-- Empty row -->
    </tr>
</table>

When you run this you'll see the second row is collapsed, but I'd rather it was rendered uncollapsed, with the same height as the first row. One way of doing this is to put a &nbsp; entity, as follows:

<table border="1">
    <tr>
        <td>Row with text</td>
    </tr>
    <tr>
        <td>&nbsp;</td><!-- Empty row -->
    </tr>
</table>

Is there a way I can achieve the second result, via CSS, using the HTML from the first snippet?

like image 586
Dan Stevens Avatar asked Feb 14 '17 11:02

Dan Stevens


People also ask

How do I keep a cell empty in an HTML table?

The usual trick: &nbsp; The usual trick is to put &nbsp; (which is a so-called escape sequence, or formally entity reference, for a character called no-break space) into a table cell.

How do you freeze a row in a HTML table?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.

How do you keep space in a table in HTML?

To set cell padding in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property padding.


2 Answers

You can use this code:

td:empty::after{
  content: "\00a0";
}

It adds escaped &nbsp; after every originally empty td, solving your issue.

td:empty::after{
  content: "\00a0";
}
<table border="1">
    <tr>
        <td>Row with text</td>
    </tr>
    <tr>
        <td></td><!-- Empty row -->
    </tr>
    <tr>
        <td>asd</td>
    </tr>
    <tr>
        <td>dees</td>
    </tr>
    <tr>
        <td></td><!-- Empty row -->
    </tr>
</table>

Learn more about escaping HTML entities here.

like image 132
wscourge Avatar answered Oct 09 '22 01:10

wscourge


You can add height to table-cell, in this case it'll work like min-height property for other elements (with display: block, display: inline-block, etc). I added another table row with long text to demonstrate it:

td {
    height: 22px;
}
<table border="1">
    <tr>
        <td>Row with text</td>
    </tr>
    <tr>
        <td></td><!-- Empty row -->
    </tr>
    <tr>
        <td>Very-very long text with many words, Very-very long text with many words, Very-very long text with many words, Very-very long text with many words, Very-very long text with many words</td>
    </tr>
</table>

You can't use min-height property, because the specification says:

In CSS 2.1, the effect of 'min-height' and 'max-height' on tables, inline tables, table cells, table rows, and row groups is undefined.

like image 4
sergdenisov Avatar answered Oct 09 '22 00:10

sergdenisov