- 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

Tips for integrating an HTML template into a React project

I'm finding it challenging to integrate an HTML template into React. The template I am using is for an admin dashboard with multiple pages. I have successfully copied and pasted the HTML code into JSX files and fixed any syntax issues. Here's wh ...

Having trouble resolving 'primeng/components/utils/ObjectUtils'?

I recently upgraded my project from Angular 4 to Angular 6 and everything was running smoothly on localhost. However, during the AOT-build process, I encountered the following error: ERROR in ./aot/app/home/accountant/customercost-form.component.ngfactory. ...

Tips for creating a sub-menu within a dropdown menu

I need some help adding a sub-menu to my drop-down function. I'm not very familiar with CSS, so I'm struggling to figure out how to do it. Ideally, I want the sub-menu to open to the right when the cursor hovers over it. Below is the CSS and HTML ...

Is it possible to continuously loop and gradually fade the same image URL using JavaScript?

I have a dynamic image stored on my web server at example.com/webcam.jpg that is continuously updated by the server. My goal is to display this image on a static HTML page with a looping effect that smoothly transitions from the previous image to the upda ...

Tips for achieving vertically centralized components with uniform height columns in Bootstrap 4

I tried implementing the solution provided in this question: Bootstrap 4 vertical center - equal height cards However, I am still facing an issue. Despite applying the suggested code: <div class="cycle-des h-100 justify-content-center">Product Cycl ...

The JQuery TextNTags plugin eliminates tag formatting once the trigger syntax has been modified

I have incorporated the JQuery TextNTags plugin into my web application. Here is the original code snippet: $.browser = { webkit: true }; $(function () { $('textarea.tagged_text').textntags({ triggers: {'!': { ...

incorporating numerous interconnected javascript files within a single html document

I have a pair of JavaScript files (file1, file2). File1 utilizes a class that is defined in file2. Can I include these files in an HTML document like this: <script type="text/javascript" src="file1.js"></script> <script type="text/javascrip ...

Effective ways to transmit a variable array from an HTML document to a PHP server

Is there a better method for transferring a variable array from HTML to PHP? I attempted to use the serialize function, but it doesn't seem to be functioning correctly. Any suggestions would be greatly appreciated. //HTML var arrayTextAreasNames = [ ...

Navigating the absence of Prismic's ability to use the <blockquote> element in their rich text editor

My experience with Prismic as a headless CMS has been mostly positive, but I recently encountered an issue that surprised me - the lack of support for a simple blockquote. It's baffling to me that such a basic HTML element is not natively supported by ...

Unable to assign a height of 100% to the tr element or a width of 100% to the

While inspecting the elements, I noticed the presence of <tbody>, within which <tr> is nested. The issue arises when the tbody element has 100% height and width as intended, but <tr> always falls short by 4 pixels in height even when styl ...

How can I create a single-page application that adjusts to different screen sizes effortlessly?

I am working on developing a single-page application that fits within one screen with the following structure: There is a fixed header that includes: Two columns (1/3 for left, 2/3 for right) : a logo on the left a breadcrumb on the right. The ...

Remove dynamically inserted list item using a button

I have dynamically created an unordered list (<ul>) that adds list items (<li>) when a button is clicked, along with a remove button. Initially, the default list item should not contain a remove button so I added it in the script. SCRIPT $(d ...

Storing persistent JSON data in a mobile app built with HTML5 involves utilizing the local storage capabilities of the

I am currently working on a mobile app using PhoneGap that is based on HTML technology. When the app is opened for the first time, my goal is to have it download a zip file that includes a JSON file and media files such as images or audio. Once the zip f ...

Ways to conceal the contents of a page behind a transparent background div while scrolling

I created a Plunkr with the following markup: <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <nav class="navbar navbar-fixed-top"> <div class="container-fluid"& ...

Unfold an HTML element and reveal a sneak peek of its content with the aid of JQuery

I need a way to organize and display a set of 5 lines of text vertically. Specifically, I want them arranged like this: 1) Line one 2) Line two 3) Line three 4) Line four 5) Line five However, I have a special requirement where only the first ...

Navigate one level up or down from the current tag that contains a specified value by utilizing Scrapy

To extract the price text from within the custom-control / label / font style, I must use the data-number attribute data-number="025.00286R". This unique identifier differentiates between control section divs based on the letter at the end. <d ...

Incorporate new content into JavaScript using the input element

I have a unique question - can text be added to JavaScript using the input tag? For example, <input type="text" id="example" /> And let's assume the JavaScript function is: function display_text() { alert("The text entered in #example wi ...

What are some effective methods to completely restrict cursor movement within a contenteditable div, regardless of any text insertion through JavaScript?

Recently, I encountered the following code snippet: document.getElementById("myDiv").addEventListener("keydown", function (e){ if (e.keyCode == 8) { this.innerHTML += "&#10240;".repeat(4); e.preventDefault(); } //moves cursor } ...

The challenge of vertically aligning text in a dynamically generated div

Currently, I am in the process of developing a straightforward application that allows for the dynamic addition of students and teachers. I am dynamically adding divs with a click function. To these dynamically created divs, I have assigned the class "us ...

Content positioned beside an image with the help of Bootstrap 4 framework

How can I align text to the right of an image, like a table layout? I experimented with grid layout using <div class="span2"> and <div class="span10"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bo ...