"CSS inheritance: When the parent style takes precedence over

I am facing a CSS issue where I have nested tables with different colors based on the data inside them. I assigned a class to each table, but the color depends on the order in which the classes are declared in the CSS. If color 1 is declared before color 2, it will override the child color.

Here is a sample: http://jsfiddle.net/b8T6V/2/ (complete version)

 <table border="0" class="Grid0">
        <tr class="even">
                    <td>table 1, row 1</td>       
        </tr>
        <tr class="even">
            <td>
                <table border="0" class="Grid1">
                    <tr class="even">
                        <td>table 2, row 1</td>       
                    </tr>
                    <tr class="odd">
                        <td>table 2, row 2</td>       
                    </tr>
                </table>
            </td>        
        </tr>
        <tr class="odd">
            <td>table 1, row 2</td>       
        </tr>
        <tr class="odd">
             <td>
                <table border="0" class="Grid1">
                    <tr class="even">
                        <td>table 2, row 1</td>       
                    </tr>
                    <tr class="odd">
                        <td>table 2, row 2</td>       
                    </tr>
                </table>
            </td>     
        </tr>
    </table> 


table.Grid0 tr.even
{
    background-color: RED ;
}
table.Grid0 tr.odd
{
    background-color: BLUE;
}

table.Grid1 tr.even
{
    background-color: BLACK;
}
table.Grid1 tr.odd
{
    background-color: WHITE;
}

If you swap Grid1 and Grid0, the colors will not display correctly.

JSFiddle sample:

The first table starts with Grid0 and the second with Grid1. The second table should be red/blue.

Thank you for any assistance you can provide.

Answer №1

Due to the nested structure where your inner tr is enclosed within two tables, one styled with class Grid0 and the other with class Grid1, both CSS rulesets are applied. The background colors set later in the file take precedence over earlier ones.

To resolve this issue, a simple solution is to incorporate > tbody > into all relevant rules. This tweak ensures that only the parent table's class is taken into account, eliminating any influence from ancestor tables.

Answer №2

To enhance the styling of your second table, ensure to include more specificity in the selector like this:

table.Grid0 tr.even, table table.Grid0 tr.even
{
    background-color: RED ;
}
table.Grid0 tr.odd, table table.Grid0 tr.odd
{
    background-color: BLUE;
}

table.Grid1 tr.even, table table.Grid1 tr.even
{
    background-color: BLACK;
}
table.Grid1 tr.odd, table table.Grid1 tr.odd
{
    background-color: WHITE;  /*background-color: blue;*/
}

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

ReactJS not responding to Bootstrap toggler button

My Bootstrap toggler button is not working in ReactJS. I did not include the jQuery scripts because I imported Bootstrap at the top. The button should only appear and function on small devices. import React from 'react'; import 'bootstrap/d ...

execute a jQuery number counter when scrolling is detected

My jQuery number counter is working perfectly, but it starts running as soon as the page loads. I'm trying to figure out how to make it run only when the element comes into view. Here is my current code: EDIT After some tinkering, I achieved the des ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...

How can you position text to the right of an image using CSS?

Trying to right-align text next to an image; CSS .p1 { position: absolute; width: 100px; height: 50px; top: 40%; left: 70%; } HTML <img src=../images/diagram1.png alt="Diagram"/> <span class="p1">This is a tes ...

add the closing </div> tag using jquery only

Having a slight issue here, it seems that jQuery is being overly clever. Within my HTML code, I am attempting to insert this string into a div container: </div><div class="something"> You'll notice that the closing tag comes first, foll ...

The height of the article automatically adapts to fit in seamlessly with the

Check out this snippet of Javascript code: for(var i=1;i<4;i++) { var img= ".img"+i; $(img).on("click", function(){ var art = $(this).closest('article'); art.css('height', 'auto&apo ...

Text that has been shortened to fit within a flexible container width

I am looking to create a two-column list with variable width on the left and fixed width on the right. The text on the left column should be truncated based on the window size: +----------------------+-----------+ |Variable width text...|Fixed width| +--- ...

``To enhance the scrolling functionality, dynamically apply a specific class when the page has reached

I am currently working with a piece of code that dynamically adds and removes the classes .nav-down and .nav-up to #nav based on the user's scrolling behavior. jQuery(document).ready(function($){ var didScroll; var lastScrollTop = 0; var delta ...

How can I vertically align text in React-bootstrap Navbar Nav-items?

I have been struggling to find a solution to my problem without resorting to custom CSS classes. I'm wondering if it's possible to achieve what I need using only flex classes. Here is the code for a simple navbar component: <Navbar bg ...

What is the best way to change the background color of a table cell in HTML and CSS?

Trying to adjust the background image of each data cell to a selected image has been challenging. Using the method displayed below, only the top left corner of the photo appears. The code snippet demonstrates setting the background of each data cell to th ...

What is the process for adding unique colors to the background of a PdfPCell?

I have a snippet of iTextSharp code that sets the background of a cell in a PDF file to green: PdfPCell cellSec2 = new PdfPCell(parSectionHeading2); cellSec2.BackgroundColor = BaseColor.GREEN; The issue I am encountering is that "BaseColor.GREEN" produce ...

Connecting icons in Laravel: A step-by-step guide

I have experience linking style CSS using the code: {{ HTML::style('css/styles.css') }} However, I am unsure how to link an icon in Laravel since it is not a standard CSS or JS file. Can anyone provide guidance on this? ...

Populate the div element with asterisks and prepare for printing

Is there a way to create a div filled with asterisks (*) without setting an exact width, but still keeping it on a single row like a border? I understand that using an asterisk image for the borders would be ideal, but due to constraints involving only ha ...

The GitHub Pages website is not displaying exactly like it does when running locally with a live server

Currently in the process of creating a compact website where users can input an anagram and receive all potential words that can be formed from it. Additionally, upon receiving these words, there is an option to click on one for its definitions. The site ...

Instructions for making all the cells in a Bootstrap 3 row the same height

I'm currently working on a grid of cards that can be flipped and have varying content. I'm attempting to make the height of cards with less content equal to the card with the most content. However, regardless of where I use height:100%, the small ...

Challenges related to height

Having an issue with height in my code. My footer is set up like this: <html> <body> <div class='wrap'> <div class=principal></div> <div class='clear'></div> <foo ...

Getting rid of the background color and style attribute on a webpage

I have a complete HTML page, but now I need to make several changes: First, I want to remove all the bgcolor and style attributes from the tables and body tags. I am attempting it like this: $('#container').find("table").removeAttr("style"); ...

Using React components to activate a click effect on the parent component

I have a React component called Card that has an onclick function triggered when the card is pressed. However, within this component, there are child elements such as sliders and buttons that perform their own actions. When clicking on these child elements ...

Utilizing Angularjs for dynamic data binding in HTML attributes and style declarations

Can someone help me figure out how to use an AngularJS model as the value for an HTML attribute? For example: <div ng-controller="deviceWidth" width={{width}}> </div> Additionally, how can I achieve this within <style> markup? Where ...

Unresponsive AJAX calls in jQuery causing div elements to not display properly

I have created a simple form that dynamically retrieves data and sends it to another page. I want certain divs to be displayed on my page based on the retrieved data. The divs are being displayed successfully when checking without AJAX, but when AJAX is im ...