- Bullet point: Table heading alignment variation

When it comes to centering a <th> element inside an <li>, different browsers have different approaches.

In Internet Explorer, Edge, and Safari, the <th> is center-justified. However, in Chrome, Firefox, and Opera, it's left-justified.

According to W3C Rendering recommendations (italics mine):

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'.

The W3C recommendation implies that a <th> should be centered by default unless there's a specific change made to the text-align property of its parent.

However, in actual practice, <th> elements inside <li> tags in Chrome, Firefox, and Opera do not follow this guideline. It seems like they might be deviating from W3C's suggestions.

(The issue can easily be fixed using th {text-align: center;}, so no solution is necessary.)

Edit

While there have been explanations for this behavior, they remain unsatisfactory – without any fault on the part of those providing answers.

It appears that the intention of the W3C is for <th> elements to be centered unless they specifically include or inherit a different explicit text-align style. Left-justifying <th> due to complex cascading inheritance of implicit styles doesn't seem logical.

The confusion seems to revolve around the definition of "its initial value" as mentioned in the quote. The "initial value" of a <li>'s text-align property is match-parent, which is evidently distinct from text-align: initial.

Hopefully, the W3C will provide clarification on this matter in the future. In the meantime, using th {text-align: center;} resolves the issue.


Example:

th, td {
  border: 1px solid #ddd;
}
<ol>
  <li>
    <table>
      <tr>
        <th>This is a Lorem Ipsum<br>test</th>
        <th>Another header
      </tr>
      <tr>
        <td>ABCDEFG HIJKLMNOP QRSTUV WXYZ
        <td>More
      </tr>
    </table>
  </li>
</ol>

Answer №1

It seems like your problem stems from the fact that li is inherently set to text-align: match-parent in some browsers, such as Firefox. This results in the li elements being aligned to the text-align:left (or text-align:right if direction is rtl). To resolve this issue, you can change the alignment of li to text-align:initial, ensuring that the th behaves as expected.

match-parent

This value is similar to inherit, but with the start and end values calculated based on the parent's direction, resulting in appropriate left or right alignment.

th,
td {
  border: 1px solid #ddd;
}
li {
  text-align: initial
}
<ol>
  <li>
    <table>
      <tr>
        <th>This is a Lorem Ipsum
          <br>test</th>
        <th>Another header
      </tr>
      <tr>
        <td>ABCDEFG HIJKLMNOP QRSTUV WXYZ
          <td>More
      </tr>
    </table>
  </li>
</ol>

Answer №2

Not at all, W3C is not being ignored in this case. As stated in the quote provided in your query,

a parent node whose computed value for the 'text-align' property is its initial value

This indicates that the default value of text-align is start, which ultimately results in start. However, <li> elements are specifically styled with text-align: match-parent, leading to a computation of left due to the <ol> having direction: ltr. Despite start behaving like

left</code because the <code><li>
also has direction: ltr, these are distinct values. Hence, the th element does not appear centered.

It should be noted that both start and match-parent have been introduced relatively recently. It is possible that the th will be centered on browsers that do not yet support these attributes.

If you were to utilize li { text-align: initial; }, then the th would indeed be aligned center.

th, td {
  border: 1px solid #ddd;
}
li {
  text-align: initial;
}
<ol>
  <li>
    <table>
      <tr>
        <th>This is a Lorem Ipsum<br>test</th>
        <th>Another header
      </tr>
      <tr>
        <td>ABCDEFG HIJKLMNOP QRSTUV WXYZ
        <td>More
      </tr>
    </table>
  </li>
</ol>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to eliminate the select all checkbox from mui data grid header

Is there a way to remove the Select All checkbox that appears at the top of the checkbox data column in my table? checkboxSelection The checkboxSelection feature adds checkboxes for every row, including the Select All checkbox in the header. How can I ...

Utilizing hover effects and timeouts to conditionally show React components

I encountered a challenging React layout dilemma. It's not a complex issue, but rather difficult to articulate, so I made an effort to be as clear as possible. The data I have maps individual components in the following way: map => <TableRow na ...

The PHP file I'm trying to access isn't receiving the GET request from my PHP script

Basically, I want a button on one php page to trigger the printing of a new php page. This feature is working perfectly. The only issue is that I am trying to ensure the user ID gets passed over to the second page and displayed there. Strangely, it seems t ...

Generating a business card example using node-html-pdf on an Ubuntu operating system

I am currently attempting to utilize the node-html-pdf module on Ubuntu 16.04 by following the businesscard example provided at https://github.com/marcbachmann/node-html-pdf. Regrettably, I encountered difficulties in generating the PDF file. To begin wi ...

The CSS styling of Vuetify TreeView does not support text wrapping

I'm having trouble getting the long text in this CodePen snippet to break and wrap properly. It extends off screen, rendering the append button unclickable. I've experimented with various CSS rules but haven't found a solution yet. Check ou ...

Scrolling in iOS 8 causing flickering problem with background image

Utilizing the Supersized jQuery slider plugin to create a full-page background slider with a fade-in effect and added height for scrolling. The slider functions correctly on desktop, but upon testing on an iOS 8 iPad device, there is noticeable flickering ...

Vue 3's Paged.js does not respond to requests for page breaks

Currently, I'm working on implementing page breaks in Vue.js 3. Despite my efforts and utilization of the specified CSS code, I am facing challenges with the ".page-break" class parameter. While I can successfully modify other elements like titlePage ...

issue with transparent html5

Here is the code snippet I am struggling with: function clear(){ context2D.clearRect(0, 0, canvas.width, canvas.height); } function drawCharacterRight(){ clear(); context2D.setTransform(1, 0.30, 1, -0.30, 10, 380);//having issues here c ...

Using jQuery UI to create sortable lists that are connected and have hidden overflow features

How can I hide overflow from two fixed-height divs containing sortable lists connected to each other without affecting their connection? For example, if overflow is set to hidden, the list item doesn't display when dragged outside of the div. I' ...

Arrange a stationary child in relation to the grandparent element

I created a small window within a browser window, containing an even smaller window with auto-scrolling text. However, I need some text in the smaller window to remain static and not scroll. Therefore, I used position_fixed which is absolutely related to ...

Adjusting the height of an element based on changes in screen width or height using Angular

I'm in the process of developing a directive that can detect changes in screen size, either width or height, and adjust the height of my element accordingly. The main goal is to have my element extend all the way to the bottom of the browser window. ...

How can I translate these JQuery functions into vanilla JavaScript?

I am currently in the process of building a configurator using transparent images. The image configurator functions through a basic JQuery function: I have assigned each input element a data attribute called data-Image. This function identifies the data-Im ...

Enable compatibility with high resolution screens and enable zoom functionality

My goal is to ensure my website appears consistent on all screen sizes by default, while still allowing users to zoom in and out freely. However, I've encountered challenges with using percentages or vh/vw units, as they don't scale elements prop ...

Is there a way to align the image to the left within the div contained in my header section?

I am facing an issue with positioning the logo in the header section of my website. It appears correctly on the left side for tablet and mobile versions, but on desktop, it does not align to the corner as desired. I tried adding a right margin, but I belie ...

Utilizing SVG and CSS together: Implementing clip-path in a flexbox layout

This task may seem simple to some, but I am struggling to mask an image with an SVG graphic. I have created an SVG with the clipPath element: <svg id="heart-path-container" version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 ...

Thymeleaf: Expression parsing error

I am working on a Thymeleaf template that includes pagination functionality. <ul class="results_perpage" > <li th:if="${previous != null}"><a th:href="javascript:movePage(`${previous}`);" class="results_menu" th:text="PREVIOUS">< ...

Guide to achieving a powerful click similar to a mouse

I've been struggling to get an audio file to play automatically for the past three days, but so far I haven't had any luck. The solutions I've tried didn't work in my browser, even though they worked on CodePen. Can anyone help me make ...

Is there a way to eliminate the additional gap found at the bottom of a div element?

I've been encountering some difficulty when trying to insert an image into a div without any padding. The issue I'm facing is that the sizing doesn't seem to be correct; there's unnecessary extra space at the bottom. Despite my efforts ...

Exploring intricate designs using HTML and Javascript

After extensive experience with both WPF and HTML5 JavaScript, one thing that stands out to me is the clear organization provided by XAML's defined panels. Grid, StackPanel, DockPanel, WrapPanel, and others offer a straightforward way to achieve consi ...

Having difficulties with hovering over a td that has a rowspan

I am encountering some challenges when attempting to hover over a td with rowspan in my HTML table. Despite searching for a solution, I have not been successful. I came across suggestions on forums that hinted at using JS/jQuery. Could someone please provi ...