Parent style CSS taking precedence over child style due to conflicting rules

Feeling a bit puzzled by another answer on SO...

I have two tables, with one nested inside the other. (a table within a table)

<table class="master">
  <tr>
    <td>ID</td><td>Name</td><td>Information</td>
  </tr>
  <tr>
    <td>1</td>
    <td>John</td>
    <td>
       <table class="detail">
         <tr>
             <td>ID</td><td>Order</td>
         </tr>
         <tr>
             <td>1</td><td>CA09-WYOMING-BR</td>
         </tr>
       </table>
    </td>
  </tr>
</table>

Regarding styling...

<style>
    table.detail{
        border:1px solid red;
        border-collapse: collapse;
        /* etc...around 20 lines of code */
    }
    table.detail td{
        border:1px solid red; 
        background:red;
        /* etc...around 20 lines of code */
    }
    table.master {
        border:1px solid black;
        border-collapse: collapse;
    }
    table.master td {
        border:1px solid black;
        background:gray;
    }
</style>

The style of the detail table is not displaying correctly due to being overridden by the parent table's CSS. I'm aware of using the !important tag to prevent this, but should I add it to all 20 lines of CSS code?

Any suggestions?

http://jsfiddle.net/vxdM3/

Answer №1

It's best to avoid using !important in this case. Consider reversing the order of your styles by placing the css for the main table before the sub table. Currently, the css for the sub table is taking precedence over the main table, causing conflicts.

See an example with reversed CSS here

If rearranging the css isn't feasible, you can update the selectors to table.main table.sub{} and table.main table.sub td{}.

Check out a fiddle with updated selectors here

Answer №2

My personal preference is to use the > symbol in CSS to prevent inadvertently styling nested elements.

.master > tr > td
{
    ....
}

In my case, I always include tbody in the selector:

.master > tbody > tr > td

I prefer to be explicit in my CSS to avoid surprises and ensure clarity in my styling choices. While this may result in slightly slower performance, I believe it ultimately benefits the user experience and allows me to work more efficiently. Of course, others may have different approaches.

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

Providing an Object as the Output of an Event Handler, in cases where the data cannot be transformed into another format

My goal is to have the second dropdown menu, labeled Item, dynamically update when a category is selected from the first dropdown menu, labeled Type. If I could access the array I created within the change event, I could easily populate the second dropdow ...

The drop-down menu appears to be falling behind the 'content' div element

Whenever I try to hover over the dropdown menu, it frustratingly appears behind the content div that I painstakingly created. Despite all my efforts with z-index adjustments, the issue still persists. Below you'll find all the code for my website, but ...

Display or conceal a div element depending on the user's choice

I am looking to hide inactive divs, but whenever I reload the page, all tab contents merge into one. The screenshot below shows the issue I'm facing. Screenshot Below is the code snippet: $('.tab a').on('click', function ...

Dynamic element substitution

Can you provide guidance on how to create a smooth transition in the height of a container based on the child element's height? Currently, my code does not include any animation effects. setTimeout(() => { document.getElementById("page1").st ...

Using d3 to create an SVG with a Bootstrap dropdown feature

I am seeking to create a graph that is not a tree, as some nodes may have multiple parents. When a node on the graph is clicked, a dropdown menu should be displayed with a list of text options. Though I am new to d3.js, I have observed examples of bootstra ...

Is there a way to align both the horizontal and rotated lines to intersect at a common point in HTML and CSS?

As a beginner, I'm looking to create a structure resembling a thigh, leg, and ankle using thick lines. My aim is to rotate them with animation, ensuring that the joints between the lines mimic those of a knee joint and hip joint. Below is the code sn ...

AngularJS: The dynamic setting for the stylesheet link tag initiates the request prematurely

I'm encountering a problem that is somewhat similar (although not exactly the same, so please be patient) to the one discussed in Conditionally-rendering css in html head I am dynamically loading a stylesheet using a scope variable defined at the sta ...

What methods are available to modify HTML content using Node.js?

I am looking to modify the contents of my HTML file (index.html) below: <html> <body> <div id="greeting">Hello world</div> </body> </html> I want to change this using Node.js in the backend. Below is the Node.js cod ...

The offset values of $(element) keep increasing indefinitely when they are updated repeatedly

After researching how to utilize JavaScript drag functionality to move elements, the goal is to dynamically adjust the cursor position within a square when dragged. In an attempt to simplify the process and avoid storing x and y offsets as separate variabl ...

Incorporate a div block using JavaScript

When calling the function below, I attempt to include a div block but struggle with setting the left position. The alert function displays a message of '600px', however, the block appears in a different position on my screen. function show(){ ...

Could anyone explain to me why the "thumbnails" in my code link are not aligning in a row format as intended?

Why are my "thumbnails" not aligning correctly in the row format I want on this code link? <div class="row"> <div class= "col-md-6"> <div class="thumbnail"> <a target="_blank" href="http://codepen.io/ ...

MTG Life counter. Display fluctuations in count

I am currently working on a fun project creating an MTG (Magic The Gathering) life tracker, even though the code is quite messy. Despite its flaws, it still gets the job done. Click here to view the MTG life tracker https://i.stack.imgur.com/Su17J.png ...

Steps for creating an offset in Bootstrap 4

Apologies for any confusion due to my not-so-great English skills. I'm currently utilizing the beta version of Bootstrap 4 (the latest, not alpha) and so far it's been great. However, I've noticed that in this new version, the offset class ...

The getCssValue() method in Selenium WebDriver

In my current exercise, I am using the cssGetValue method to extract the value from a specific web element's CSS property. I have two main inquiries: Why did the cssGetValue method return the value 13px, and which exact web element is being refere ...

What is the best way to create a flexible Ul-LI menu?

Is it possible to create a flexible menu where clicking on an item does not cover other items in the menu? #demo { margin: 30px 0 50px 0; font-family: sans-serif; } #demo .wrapper { display: inline-block; width: 180px; height: 20px; position ...

Having trouble accessing listview capabilities in jquerymobile?

I am currently delving into the world of jquerymobile. I am experimenting with a sample project where I aim to showcase a listview. Below is the code that I have been working on. <!doctype html> <html> <head> <link rel="stylesheet" h ...

Tips for shifting a div to the left with AngularJS

I am working with a div structure that looks like the following: <div class="col-xs-1 scroll-button"><i class="glyphicon glyphicon-chevron-left"></i> </div> <div class="customer-ust-bant col-xs-22" id="letters"> <box n ...

Issue with Domsanitizer bypasssecuritytruststyle functionality on Internet Explorer 11 not resolving

CSS:- Implementing the style property from modalStyle in TypeScript <div class="modal" tabindex="1000" [style]="modalStyle" > Angular Component:- Using DomSanitizer to adjust height, display, and min-height properties. While this configuration work ...

How can we achieve the same functionality as React Native's { flex: 1 } in React JS?

I've been diving into React Native development, and now I'm exploring React.js for web applications. However, I'm facing a challenge in creating a simple View that takes up the entire screen to begin experimenting with different components. ...

Is there a way to navigate and scale HTML elements like Google Maps or WordSquared?

I need to find a way to pan and zoom HTML elements, similar to the functionality on WordSquared.com. I have searched for a jQuery plugin or other resources but have not found anything helpful yet. Perhaps I am missing a simple solution? Any guidance on h ...