You cannot apply Contextual Classes to override Table Header Colors Classes like .thead-dark or .thead-light

I am currently learning about tables in Bootstrap 4. I am facing an issue where the background-color in the table-light class is not being applied. I suspect this might be due to the thead-dark class taking precedence over it. Could you please clarify why thead-dark takes priority over table-light? If my assumption is incorrect, I would appreciate an explanation regarding the actual reason.

<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
<title>Table</title>
</head>
<body>
<div class="container">
  <h2>Contextual Classes</h2>
  <p>Contextual classes can be used to color the table, table rows or table cells. The classes that can be used are: .table-primary, .table-success, .table-info, .table-warning, .table-danger, .table-active, .table-secondary, .table-light and .table-dark:</p>
  <table class="table">
    <thead class="thead-dark">
      <tr>
        <th class="table-light">Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td>Defaultson</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7115141731021e1c141c10181d5f121e1c">[email protected]</a></td>
      </tr>      
      <tr class="table-primary">
        <td>Primary</td>
        <td>Joe</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87ede8e2c7e2ffe6eaf7ebe2a9e4e8ea">[email protected]</a></td>
      </tr>
      <tr class="table-success">
        <td>Success</td>
        <td>Doe</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b3b6b1b799bca1b8b4a9b5bcf7bab6b4">[email protected]</a></td>
      </tr>
      <tr class="table-danger">
        <td>Danger</td>
        <td>Moe</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="167b77646f56736e777b667a733875797b">[email protected]</a></td>
      </tr>
      <tr class="table-info">
        <td>Info</td>
        <td>Dooley</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7ddc2dbcef7d2cfd6dac7dbd299d4d8da">[email protected]</a></td>
      </tr>
      <tr class="table-warning">
        <td>Warning</td>
        <td>Refs</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3818ca3869b828e938f86cd808c8e">[email protected]</a></td>
      </tr>
      <tr class="table-active">
        <td>Active</td>
        <td>Activeson</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afceccdbefcad7cec2dfc3ca81ccc0c2">[email protected]</a></td>
      </tr>
      <tr class="table-secondary">
        <td>Secondary</td>
        <td>Secondson</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b485e587b5e435a564b575e15585456">[email protected]</a></td>
      </tr>
      <tr class="table-light">
        <td>Light</td>
        <td>Angie</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8d828b8589ac89948d819c8089c28f8381">[email protected]</a></td>
      </tr>
      <tr class="table-dark text-dark">
        <td>Dark</td>
        <td>Bo</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c4c9e6c3dec7cbd6cac388c5c9cb">[email protected]</a></td>
      </tr>
    </tbody>
  </table>
</div>
</body> <!-- source: https://www.w3schools.com/bootstrap4/bootstrap_tables.asp -->

Issue:

https://i.sstatic.net/dNLO0.png

Answer №1

This is the definition of .thead-light found in the Bootstrap stylesheet.

.table .thead-light th {
  color: #495057;
  background-color: #e9ecef;
  border-color: #dee2e6;
}

It specifically targets th elements that are children of an element with the class thead-light.

When you assign the class to a th element, like so:

<th class="table-light">Firstname</th>

You must target it using:

th.thead-light {
   /* CSS */
}

If not, the specificity of .table-dark > th will take precedence over <th class="table-light">

.table-dark,
.table-dark > th, /* <<< This rule */
.table-dark > td {
  background-color: #c6c8ca;
}

Answer №2

The rule found in bootstrap.css is quite interesting. It states:

.table .thead-dark th {
  color: #fff;
  background-color: #212529;
  border-color: #32383e;
}

Surprisingly, this rule is positioned after another rule:

.table-light,
.table-light > th,
.table-light > td {
  background-color: #fdfdfe;
}

Perhaps the most crucial aspect:

When utilizing the CSS Specificity Calculator to compare th.table-light (the actual usage) with .table .thead-dark th, it becomes evident that the latter carries greater specificity and therefore takes precedence.

To sum it up:

For a CSS rule to triumph in specificity (and thus take control), it must either possess higher specificity than a rival CSS rule OR, in the case where specificity levels are equal (as seen here), it should be listed after the conflicting rule.

Keep in mind:

While some may recommend using the !important declaration for a quick fix, it should only serve as a temporary solution. Establishing it as a permanent fixture could lead to maintenance nightmares.

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

Click the link to capture the ID and store it in a variable

Currently working on a project involving HTML and JavaScript. Two HTML pages ("people.html" and "profile.html") are being used along with one JavaScript file ("people.js") that contains an object with a list of names, each assigned a unique ID: var person ...

styled-components: the parent's styles take precedence over the child's styles

image export const LogOutButton = styled.button` display: inline-block; .... `; export default styled(ThemedApp)` .... button { .... display: flex; .... } `; Upon inspection, it is evident that the Logout button (with class gBuhXv) ...

What sets apart auto, 0, and no z-index values?

Can you explain the distinctions between the following: z-index: auto z-index: 0 the absence of z-index In all scenarios mentioned, we have a container div that contains two inner divs, named div1 and div2, with respective z-index values of 9 and 10. T ...

A step-by-step guide to creating a CSS menu using pure HTML markup

I have been working on the CSS for a menu using some HTML code, but I am stuck and uncertain about my next steps. Here is the CSS I have added so far: #menu-my-integra, ul.sub-menu { list-style: none; padding: 0; margin: 0; } #menu-my-integra li ...

Using jQuery for validation using a checkbox

I'm currently working on a registration system that allows users to choose between paying immediately with a credit card or later with a check. Depending on their choice, the fields for credit card information should either be validated or hidden to d ...

By increasing the background-color to 100%, the list item obstructs the background image of its parent div

In cases where an element is set to display:block, the background-color of that element typically extends to 100% of the width of its parent element. However, I am seeking a solution where the background-color only reaches the edge of the text, allowing th ...

The Angular CLI does not recognize any Bootstrap classes

While working on my Angular project and utilizing Bootstrap along with various libraries, I suddenly encountered an issue where all CSS classes associated with Bootstrap are not being detected. In the angular.json file: "styles": [ &qu ...

What is the reason behind the changes in the CSS rendering behavior of Fieldset legends over the past few months?

A form on my website contains nested <fieldset> elements, each with a unique <legend>. Recently, I discovered an issue with the page rendering on various browsers (Chrome, Firefox, Opera) that was not present before. One element, <span clas ...

Sort through categories using PHP and MySQL for an organized search

Hello, I have a search code that I am using: <form action="search.php" method="get"> <input type="text" name="query"> <select name='category'> <option value="all">All categories</option> <? while ($cat = mys ...

How can autopostback be implemented for a Multiselect ListBox in ASP?

Attempting to create a multi-select ListBox in ASP.NET by utilizing a ListBox control with Bootstrap Multiselect styling. In terms of appearance, it functions well. I am able to open the dropdown, select and deselect options, and close it. <asp:ListBox ...

Width of ListBox Item

How can I adjust the width of items in my RadListBox that are being inserted programmatically to prevent multiple items from displaying on a single line? Currently, the items are added to the listbox in C# code like this: rlbAssigned.Items.Add(new RadLis ...

What could be causing the issue of dynamic meta open graph tags not functioning properly?

For my current project, I am utilizing Strapi as the CMS and Next.js. My task is to generate meta open graph tags dynamically. However, after deployment, when I attempt to share my website link on social media platforms such as Facebook or WhatsApp, the ti ...

The coloring of my div elements is not displaying as intended

After reviewing the provided html and css, I am puzzled as to why the parent container is being colored green while the child items remain uncolored. It seems like all div elements are being affected by the green color. What I actually want is for the pa ...

Error: The specified module is missing an export that was requested

Hello, I encountered an error in my console that reads: "Uncaught SyntaxError: The requested module './components/tileHeader/controller.js' does not provide an export named 'tileHeader'". Below is the code snippet causing the issue: co ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

Tips for invoking a url with JavaScript and retrieving the response back to JavaScript

I am trying to make a URL call from JavaScript with a single parameter, and the URL should respond to that specific request. Here is an example of the response format: {"success":true, "result": {"token":"4fc5ef2bd77a3","serverTime":1338371883,"expireT ...

Unique background image is assigned to each div sharing the same class

Seeking a way to assign unique random backgrounds to each div with the .item class. Initially attempted using PHP and CSS, for example: <?php $bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg ...

Creating multiple versions of a website based on the user's location can be achieved by implementing geotargeting techniques

It may be a bit challenging to convey this idea clearly. I am interested in creating distinct home pages based on the source from which visitors arrive. For instance, if they come from FaceBook, I envision a tailored home page for FaceBook users. If they ...

JsPlumb: Incorrect endpoint drawn if the source `div` is a child of a `div` with `position:absolute`

My current setup involves two blue div elements connected by jsPlumb. You can view the setup here: https://jsfiddle.net/b6phv6dk/1/ The source div is nested within a third black div that is positioned 100px from the top using position: absolute;. It appe ...

Creating a dynamic list filter using JavaScript and three select boxes

Looking for a way to implement a similar feature to the one on this webpage: I will be showcasing a list of brands on the page, with each brand requiring three pieces of information: Starting letter Store (multiple options) Category (multiple options) ...