Why is tr.myClass not functioning properly without the presence of >td? How can I determine the

In my HTML, I have a table as shown below:

var selector = $("#searchResultsArea");

$.getJSON(url, null, function (data) {
selector.empty();
var html = "<table id=\"tabRoom\" style=\"width: 950px; border:none\" 
            class=\"selectable\">
            <thead><tr>
            <th class=\"ui-widget-header\">Code</th>
            <th class=\"ui-widget- header\">Alias</th></tr></thead><tbody>";
html += "<tr onclick=\"editRoom(" + optionData.Id + ");\" 
         class=\"" + (i % 2 ? "gridrow" : "gridrow_alternate") + "\">
         <td style=\"width: 50%\">" + optionData.Code + "</td>
         <td style=\"width: 50%\">" + optionData.Alias + "</td></tr></tbody></table>";
selector.append(html);
});

I expected the following CSS code to work:

tr.myClass 
{
    background: red;
}

However, in the project I am working on, the above code is not functioning. Surprisingly, when I modify the code like this:

tr.myClass>td 
{
    background: red;
}

it works and changes the background color of all cells in the row to red. I am curious to know why omitting >td does not work as expected. With thousands of lines of CSS codes, I'm unsure where to start looking for the issue. Any guidance would be appreciated. Thank you!

EDIT: Appreciate highlighting the syntax error, but it was fixed while composing this question. The actual problem lies in understanding why >td is necessary.

EDIT2: Updated code provided.

Answer №1

You've made a small spelling mistake, the class should be capitalized in the class name selector myClass.

Consider using this revised version:

tr.myClass{
 background: red;
}
<table>
   <tr class="myClass">
    <td>John</td>
    <td>Smith</td>
   </tr>
</table>

Answer №2

Review the code snippet below:

tr.myClass 
{
    background: red !important;
}
tr.green 
{
    background: green;
}
tr.myClass td
{
    background: blue;
}
<table>
   <tr class="myClass green">
    <td>John</td>
    <td style="background: purple">Smith</td>
   </tr>
</table>

In the CSS, the order of styles is as follows:

  • tr.myClass (!important)
  • tr.green
  • tr.myClass td

We also have an inline style background: purple.

The tr element has classes myClass and green, both affecting the background color. Since .green is defined after .myClass, it should take precedence over .myClass. To prioritize .myClass, we use !important, but despite this, the background remains blue instead of turning red.

This unexpected result is due to specificity - tr.myClass td being more specific than just tr.myClass, leading to .myClass being overridden even with the use of !important.

Moreover, the second cell's background appears as purple instead of blue because of inline styling taking precedence.

You may want to explore more on Specificity - CSS | MDN.

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

CSS animation for image hover effect in Revolution Slider

I'm currently facing an issue while using Revolution Slider. To better illustrate, I am referring to the original demo found at this link: . When browsing through the 'portfolio' tab, you'll notice that the images shrink and darken upo ...

Is it possible to have multiple divs with unique IDs that can be individually controlled using a single JavaScript function?

I'm in the process of figuring out how to create a single JavaScript function that can manage multiple divs. I currently have several divs with distinct ids and I've written separate JavaScript functions for each one. However, they either do not ...

What could be causing the removal of style classes from my element after it is appended?

Could you please explain how it functions? I am attempting to append a div with content using innerHTML and it is working, but without any of the styling classes being applied. const element = document.createElement("div"); element.classList.add("col ...

Guide on creating a rectangle in HTML without using the canvas element

The concept is clearer in this image In the photo provided, I utilized canvas for drawing; however, I am interested in exploring a different approach that would allow me to create draggable rectangles with unique IDs. For instance, if I were to use div in ...

What is the best way to hide a div when an image is positioned on top of it?

Previously, I inquired about creating a "cursor mirror" where the movement of the cursor in the top section of a website would be mirrored in the opposite direction in the bottom section. You can find the original question here. Expanding on that concept, ...

What is the best way to incorporate a solid element onto a see-through background?

I have a link at "http://hastebin.com/julayefoto.xml" and I need assistance in making the content inside the bubble named "Test Test Test" non-transparent while keeping the bubble itself transparent. Any help would be greatly appreciated. Thank you :) ...

Creating a Pop-Up on Website Load with HTML, CSS, and Jquery

<script> // utilizing only jquery $(document).ready(function(){ $('#overlay-back').fadeIn(500,function(){ $('#popup').show(); }); $(".close-image").on('click', function() { $('#po ...

NextJS compilation sometimes results in undefined errors when using Sass styles

My peace lies in the sass code: .link display: inline-table text-align: center align-self: center margin: 5px 15px font-size: 20px color: black text-decoration: none transition: 0.1s ease-in-out .link:hover text-decoration: underline .l ...

troubleshooting problem with bootstrap 4 form submission

I am currently working on a website using bootstrap 4 and have added a form. After uploading it to my server, I tested it and it seems to be working, but with some issues. The form consists of four fields: first_name, last_name, email, and phone. While the ...

The value property of the input element is returning as undefined

The input value entered is always returning undefined. Despite trying various solutions found in similar questions, my jQuery code continues to return undefined. jQuery: $( document ).ready(function() { $('#Input').keypress( function(e ...

Styling for a table with one column set to a fixed width, one column designated as small, and the rest of the

Creating a table with fixed width first column, text-contracting second column, and equally splitting remaining space for the rest seems to be a challenge. Check out this fiddle for my current attempt: http://jsfiddle.net/B8LnP/ Two key issues to observe ...

Learn the method to conceal rows within a table simply by toggling a button

I need a function that will hide the rows below a row with a header and button, and only reveal them when another row with a header and button is clicked. When one of the +/- buttons is clicked, it should hide or expand all the rows with data content. http ...

Is there a glitch with CSS skew, shadow, and hover effects in Chrome browser?

   I am facing an interesting problem where hovering over a skewed li element with a CSS3 transform and a shadow applied causes the generated shadow to display incorrectly. You can see the issue for yourself here - http://jsfiddle.net/mabbage/BLD5Z/1/ ...

Having trouble centering elements correctly with Bootstrap 5

Just dipping my toes into the world of bootstrap and experimenting with some code. Encountered an issue when trying to center elements properly within columns: <div class="container"> <div class="row"> <div class= ...

Is it possible to add additional text to an input field without modifying its existing value?

I have a numerical input field labeled "days" that I want to add the text " days" to without altering the actual numerical value displayed. <input type="number" class="days" (keyup)="valueChanged($event)"/> Users should only be able to edit the num ...

Exploring the engaging section of interconnected images featuring rounded edges

I'm currently working on a unique widget that can be found at this link: http://codepen.io/JonnyNineToes/pen/zGYxwV This widget features an image that reveals additional information when clicked, with the extra info sliding out from behind the image. ...

Is there a way to move the <hr> element closer to the content in the header section?

I am looking to move the closer to the content in the header. I attempted using position-absolute but the disappeared. This marks my inaugural project with bootstrap. <!-- Bootstrap-5 --> <link href="https://cdn.jsdelivr.net/npm/<a href="/ ...

Bootstrap: Altering grid column layout for medium and small/extra-small screens

Hello everyone, I'm having a bit of trouble working with Bootstrap. My goal is to create a container with a row that contains three columns of equal width: <div class="row"> <div class="col-md-4" style="background:red;">logo</div& ...

Display Text Using a Variety of HTML Styles

Is there a way to achieve the following setup: HTML: <p>ABC</p> Output: DEF I am inquiring about this because I need to create an email template that includes the HTML snippet must contain <p>${__VCG__VAL__FIRST_NAME}</p> (where ...

Is there a way to recycle an image and ensure that the web browser only needs to download it once?

Is there a way to effectively reuse the same image multiple times on my website without inefficiently downloading it each time? ...