5

Given the following HTML document containing a table:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <style>
      body {
        /* text-align: left; */
      }
      th, td {
        border: 1px solid black;
        width: 200px;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th>Row 1 Column 1</th>
        <th>Row 1 Column 2</th>
      </tr>
      <tr>
        <td>Row 2 Column 1</td>
        <td>Row 2 Column 2</td>
      </tr>
    </table>
  </body>
</html>

This will show a 2x2 table, where the contents of the first row are centered, and the contents of the second row are left-aligned. This is due to the user-agent stylesheets, which on chrome sets text-align: -internal-center; on th elements, and does not set an alignment for td-elements, so that these use the default left-alignment.

A 2x2 table, where the contents of the first row are centered, and the contents of the second row are left-aligned

If we uncomment the text-align: left; styling for the body element in the code snippet above, the th-elements will inherit this style from the body-element, and all the contents of the table will be left-aligned.

However, if we change it to text-align: start;, the first row will be centered again. How can that be? According to MDN, the values start and left should behave identical if the direction is ltr (left-to-right), which is the case since we put dir="ltr" on the html element.

Using text-align: end or right both shifts the contents to the right, while using left shifts the contents to the left. The only value which does not work is start.

I see this behaviour on both Firefox and Chrome, on MacOS.

Note that I am not asking for workarounds, but want to understand why this happens!

Timitry
  • 2,646
  • 20
  • 26
  • Checking this Chrome dev tools, in the "Styles" tab it still shows `text-align: -internal-center;` as the style for the `th` elements, and whatever value `text-align` on body has, is crossed out. On the "Computed" tab however, the computed value is the same as on body for every of the values other than `start`, when it explicitly becomes `left`. – CBroe Jul 28 '23 at 10:21
  • 1
    _"According to MDN, the values start and left should behave identical"_ - they do, if you set them for the `th` elements directly. Looks like a side effect of that `-internal-center` value to me, but I couldn't say if there was any intention behind it, or it is basically just a bug. – CBroe Jul 28 '23 at 10:21
  • @CBroe The behaviour is the same in FF, the user agent value is `text-align: -moz-center-or-inherit;`. Reading the W3C Docs, it seems to be a bug but the fact all browsers behave the same seems to indicate some sort of intention. Hard to say what is going on exaclty. – web-tiki Jul 28 '23 at 10:31

1 Answers1

2

Source: W3C > HTML5 > Rendering > The CSS user agent style sheet and presentational hints > Alignment (the last paragraph in the section):

User agents are expected to have a rule in their user agent stylesheet that matches th elements that have a parent node whose computed value for the 'text-align' property is its initial value, whose declaration block consists of just a single declaration that sets the 'text-align' property to the value 'center'.

In other words, user agents should center th only if the default value of its parent is unchanged. text-align: start is the default value in current browsers, therefore th gets centered.

I was able to come to this conclusion thanks to a question posted in 2011 <th> text-align compatibility and the second answer

Timitry
  • 2,646
  • 20
  • 26
Tim R
  • 2,622
  • 1
  • 3
  • 19