Allow for a unique background class for each table row to take precedence over the background class of individual cells in a striped table

When attempting to create a striped Bootstrap table using Bootstrap v5.3.3, I encountered an issue with displaying cells with a red background using the Bootstrap "table-danger" class. Additionally, I wanted to highlight entire rows using the "table-warning" class. Unfortunately, the expected results were not achieved.

In the scenario outlined below, all cells in the last column are assigned the "table-danger" class. I used CSS to target all td elements with the "table-danger" class within a tr with the "table-warning" class and set the background-color to #fff3cd. However, in practice, only row #2 displays the desired behavior. Row #1's background remains red instead of turning yellow. When inspecting the "New-York" cell, the background color value is correct, but it does not display as expected.

The reason behind this unexpected behavior is unclear to me.

    /* Higher specificity to target the table-danger cells inside table-warning rows */
    tr.table-warning td.table-danger {
        background-color: #fff3cd !important;
        /* Ensure the table-warning background color is applied */
    }
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbdb0b0abacabadbeaf9feaf1ecf1ec">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1a1c0f1e2e5b405d405d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<body>
    <div class="container mt-5">
        <table class="table table-striped">
            <thead>
                <tr class="table-warning">
                    <th scope="col">Row</th>
                    <th scope="col">Name</th>
                    <th scope="col">Age</th>
                    <th scope="col">City</th>
                </tr>
            </thead>
            <tbody>
                <tr class="table-warning">
                    <td>#1</td>
                    <td>John Doe</td>
                    <td>25</td>
                    <td class="table-danger">New York</td>
                </tr>
                <tr class="table-warning">
                    <td>#2</td>
                    <td>Jane Smith</td>
                    <td>30</td>
                    <td class="table-danger">London</td>
                </tr>
                <tr>
                    <td>#3</td>
                    <td>Mark Johnson</td>
                    <td>35</td>
                    <td class="table-danger">Paris</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

Answer №1

The issue arises from the fact that cells with the table-danger class are within a tr with a table-warning class, resulting in conflicting background colors. A cell cannot have two background colors simultaneously, so a decision must be made.

Below is a code snippet that can resolve your problem:

.last-cell {
        background-color: #fff3cd !important;
      }
<!DOCTYPE html>
<html lang="en>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"></script>
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <title>Document</title>

  </head>
  <body>
    <div class="container mt-5">
      <table class="table table-striped">
        <thead>
          <tr class="table-warning">
            <th scope="col">Row</th>
            <th scope="col">Name</th>
            <th scope="col">Age</th>
            <th scope="col">City</th>
          </tr>
        </thead>
        <tbody>
          <tr class="table-warning">
            <td>#1</td>
            <td>John Doe</td>
            <td>25</td>
            <td class="last-cell">New York</td>
          </tr>
          <tr class="table-warning">
            <td>#2</td>
            <td>Jane Smith</td>
            <td>30</td>
            <td class="last-cell">London</td>
          </tr>
          <tr>
            <td>#3</td>
            <td>Mark Johnson</td>
            <td>35</td>
            <td class="">Paris</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>

I hope this solution proves helpful!

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

Display web content using ASP.NET Razor cshtml files

I am currently working on a web application developed with ASP.NET 4.6.1 and utilizing the Razor view engine. Is there a way to trigger a command that will convert all my template files (.cshtml files) into static HTML? For specific stages in my build pro ...

Adding a command to open a new browser tab using JavaScript code can easily be accomplished by following these steps. By using Terminal, you can quickly and efficiently implement this feature

I'm new to coding and trying to create a terminal simulation. You can check out my code here: https://codepen.io/isdampe/pen/YpgOYr. Command: var coreCmds = { "clear": clear }; Answer (to clear the screen): function clear(argv, argc ...

execute a function upon selecting a radio button

Below is the HTML code that includes two radio buttons. The default checked button is the "lease" option. <input id="quotation_request_payment_option_lease" class="choose_payment_option" name="quotation_request[payment_option]" type ...

Finding the chosen selection in AngularJs

I've been working on this script for hours and I'm struggling to output the text instead of the value of a select option in AngularJS in HTML through data binding. Despite my efforts, I keep getting the value instead of the text. How can I resolv ...

Transmitting Data to PHP Using JQuery/AJAX

I'm struggling with this issue. Here is the HTML code that I have: <input type="text" class="studno"><input type="button" value="Check" class="chstudno"> How can I send this data to PHP using JQuery/AJAX? Below is the code that I current ...

A tutorial on utilizing fopen in PHP to write text lines to a .txt file

Struggling with PHP code utilizing fopen to write text lines into a textarea and save them to a .txt file. Below is the code I've been working on, hoping to get some assistance! <?php session_start(); if (!isset($_SESSION['username&ap ...

AngularJS - Create Alert Pop-Up for Missing Input Fields

I've been struggling to set up my app so that it displays an alert when the user attempts to submit a form with an empty input box. Despite having a confirmation popup working in another part of the application, I can't seem to figure out why thi ...

Using :before and :after in CSS, three dots can be created in a horizontal line

I am currently trying to display three horizontal dots on my webpage. I have created a demonstration on jsfiddle. span { position: relative; background-color: blue; border-radius: 5px; font-size: 0; margin-left: 20px; padding: 5px; ...

What is the reason behind the browser crashing when a scrollbar pseudo-class is dynamically added to an iframe?

1. Insert a new iframe into your HTML: <iframe id="iframe-box" onload=onloadcss(this) src="..." style="width: 100%; border: medium none; "></iframe> 2. Incorporate the following JavaScript code into your HTML file ...

Struggling to effectively use XPath to target LI elements that contain specific text

Below is the HTML code for the list item in question: <li class="disabled-result" data-option-array-index="1" style="">4" (0)</li> Here is my attempt at using JavaScript to hide this list item, but it's not working as expected: var xpat ...

Cannot get the input padding override to work in MUI autocomplete

I've been attempting to modify the padding within the MUI autocomplete component, but unfortunately, it doesn't appear to be functioning correctly const icon = <CheckBoxOutlineBlankIcon fontSize="small" />; const checkedIcon = ...

Unable to view the HTML division

I am struggling with my website creation as I encounter issues with the .Main div. Despite setting background-color: orange; in the CSS, the color is not visible on the page. The inspector shows that it is positioned at 1440 x 0. Any assistance would be gr ...

Choosing an option from a PHP MySQL table based on a JavaScript value

I am attempting to create a select box that displays a value based on whether the database has a "yes" or "no" in the specified column. Despite my efforts, I am unable to identify any syntax errors causing this code snippet to not function properly. JavaSc ...

Tips for making a 2D grid on a webpage

Is there a way to implement a 10x10 grid on a webpage where users can click anywhere on the grid and have their (x, y) position recorded with one decimal place accuracy, covering values between 0.0-10.0 or 0.1-9.9? Appreciate any guidance! ...

Retain the previously selected option in a PHP combobox even when changing pages

Can someone please assist me with my PHP code? I am having trouble keeping my combobox unchanged after a page reload. It works fine until I change pages in my pagination. Can anyone help? THIS IS MY PHP. PHP ...

Struggling to align button text vertically in the center

I've searched through various sources, including stackoverflow.com and other websites, to solve this problem I'm encountering. Despite trying everything I found, I still can't get the text to center vertically in Firefox when styling buttons ...

What is the best way to isolate the CSS of individual components in Angular 2?

For the first component: CSS-- ngx-dnd-container { color:black; background-color: white; } HTML- <ngx-dnd-container [model]="targetItemsA" dropZone="multiple-target-a" </ngx-dnd-container> For the sec ...

Find the elements of <a> tags specifically without the attribute href

Currently, I am extracting the class of all <a> elements in an HTML document of a webpage using VB.net from a WinForm: Dim htmlLinks As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a") For Each link As HtmlElement In htmlLi ...

Viewing all album titles simultaneously using the Flickr API

After trying a different approach due to lack of responses to my previous question, I am still facing the same issue. My code successfully retrieves all album names and corresponding photos from Flickr API, but it displays all album names at once followed ...

What methods can I use to guarantee that a cloned HTML element retains a specific property during Unit Testing?

During my HTML cloning process, I am using the following code snippet: var clone = document.getElementById('tempID').cloneNode(true); After creating the clone, I need to modify its ID by assigning a new unique identifier with clone['id&apo ...