A long time ago, I found out that by default, you could only format td
elements in HTML table
s: somehow doing that with just tr
failed, and I never understood why.
Since I wasn’t doing a lot of html stuff back then, I just lived with the few occurrences and moved on.
A while ago, I needed html
tables again, and – since WordPress.com hosted blogs do not allow CSS style sheets – I had to use inline style
attributes. That would bloat many td
elements, so I did a [Wayback/Archive.is] html table border below row – Google Search, where the first few hits – all on StackOverflow – tremendously helped my to understand why this was the case and how to fix it:
- In [Wayback/Archive.is] css – Giving a border to an HTML table row, – Stack Overflow, [Wayback/Archive.is] User Jukka K. Korpela – Stack Overflow explains about the various border models, but only shows a a solution having the CSS style sheet in a style element which WordPress.com disallows:
You can setborder
properties on atr
element, but according to the CSS 2.1 specification, such properties have no effect in the separated borders model, which tends to be the default in browsers. Ref.: 17.6.1 The separated borders model. (The initial value ofborder-collapse
isseparate
according to CSS 2.1, and some browsers also set it as default value fortable
. The net effect anyway is that you get separated border on almost all browsers unless you explicitly specificollapse
.)Thus, you need to use collapsing borders. Example:<style> table { border-collapse: collapse; } tr:nth-child(3) { border: solid thin; } </style>
From that, I learned about these:
- [Wayback/Archive.is] Tables: borders
There are two distinct models for setting borders on table cells in CSS. One is most suitable for so-called separated borders around individual cells, the other is suitable for borders that are continuous from one end of the table to the other. Many border styles can be achieved with either model, so it is often a matter of taste which one is used.This property selects a table’s border model. The value ‘separate
‘ selects the separated borders border model. The value ‘collapse
‘ selects the collapsing borders model. The models are described below. - [Wayback/Archive.is] Tables: separated borders (default)
- [Wayback/Archive.is] Tables: collapsing borders
- [Wayback/Archive.is] Tables: borders
- In [Wayback/Archive.is] Create table with only bottom border in HTML – Stack Overflow, [Wayback/Archive.is] Martin2904 explains how to solve it with CSS style sheets and inline style attributes, but does not link to the “why”:
Just use the following code-snippet and paste it in you style.csstable { border-collapse: collapse; } tr{ border-bottom: 1px solid black; }
<table> <tbody> <tr> <td>Lorem</td> <td>Ipsum</td> </tr> </tbody> </table>
Without using style.css<table style="border-collapse: collapse;"> <tbody> <tr style="border-bottom: 1px solid black;"> <td>Lorem</td> <td>Ipsum</td> </tr> </tbody> </table>
–jeroen