Unable to get `tr:nth-child` to function properly within an MVC view

I have come across many questions regarding my issue, but none seem to offer a solution that works for me. The problem lies with the styling in the upper section of my view (which I plan to transfer to bootstrap later on). My main objective is to implement alternate row shading in my table. At this point, I am not concerned about the specific colors, just the functionality.

The website runs on both Mozilla and Chrome browsers, and I have already tried clearing the cache.

<style>
    table, th, td {
        border: 2px solid gray;
        border-collapse: collapse;
        border-right: none;
        background: white;
        color: #333333; /*#333333*/
    }

    table {
        border-left: none;
    }

    th {
        text-align: center;
    }

    tr:nth-child(odd)       { background:#eee; }
    tr:nth-child(even)      { background:#fff; }
</style>

The table structure is as follows:

<div class="tableIndex">
        <table id="tableBe">
            <tr>
               <th></th>
            </tr>
            @if (Model.Count() == 0)
            {
                <tr>
                    <td colspan="25" , align="left" style="border-left:none !important">
                        <b>No issues match search criteria</b>
                    </td>
                </tr>
            }
            else
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td></td>
                    </tr>
                }
            }
        </table>
    </div>

Everything seems to be working fine, except for the nth-child styling. I would appreciate any assistance on this matter. Apologies for the lengthy code snippet, but it was necessary to highlight the presence of an if statement in the code.

When inspecting the element, I noticed that there are no references to the tr:nth-child commands, even though the other styles within the <style> tag are being applied. Any help will be greatly appreciated. Thank you in advance!

Answer №1

The reason is due to the presence of the CSS rule background: white; within the td element.

To resolve this issue, relocate the background: white; declaration from table,th,td { and instead place it within table {.

Answer №2

Your code is currently assigning color to your tr elements based on a condition. However, the styles you have applied for th and td background colors are overwriting it. To fix this issue, update your CSS like so:

    tr:nth-child(odd) td{ background:#eee; }
    tr:nth-child(even) td { background:#fff; }

View Demo

Answer №3

You previously set the background of td. Therefore, it is recommended to set the background of td instead of tr. The following code should achieve the desired effect:

tr:nth-child(odd) td  { background:#eee; }
tr:nth-child(even) td { background:#fff; }

Complete Code Snippet

table, th, td {
    border: 2px solid gray;
    border-collapse: collapse;
    border-right: none;
    background: white;
    color: #333333; /*#333333*/
}

table {
    border-left: none;
}

th {
     text-align: center;
}

tr:nth-child(odd) td {
     background: #eee;
 }

tr:nth-child(even) td {
     background: #fff;
}
<table>
    <tr><td>111111111</td><td>2222222222</td></tr>
    <tr><td>111111111</td><td>2222222222</td></tr>
    <tr><td>111111111</td><td>2222222222</td></tr>
    <tr><td>111111111</td><td>2222222222</td></tr>
</table>

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

Showing image URL data fetched in JSON format using AJAX library JQuery

I am using Ajax to retrieve data from the database in JSON format. I am able to retrieve various data fields successfully, but I am facing an issue with displaying an image. Although I can see the image URL in the JSON response, the image is not being disp ...

Utilizing ASP.NET, leveraging the 'this.Page' property as a condition to dynamically hide labels within a

I'm a beginner in ASP.NET and I have a query regarding how to obtain the Parent page from a user control and utilize it as a condition to hide certain content. I attempted the following: var Pagechk = this.Page; Page page = HttpContext.Current.Handl ...

Javascript Dilemma: Unable to Access 'object.style.left' and 'object.style.top' Values - What's the Issue?

Why am I unable to access and modify the object.style.left & object.style.top values? I am currently attempting to dynamically reposition a button on a webpage. In order to set new values for the style.left & style.top, I can utilize JavaScript l ...

ngx-bootstrap: Customizable horizontal sorting with templates

I'm encountering a problem with the ngx-bootstrap sortable component. My goal is to utilize the sortable component in a horizontal layout instead of the vertical one shown in the documentation examples. ngx-bootstrap sortable If anyone has a solutio ...

Several buttons, each requiring to show distinct text

Currently, I am attempting to enhance an existing project that was graciously created for me. I must admit, I am quite new to this, so please be patient with me! In my project, there are 9 buttons, each triggering the display of a different image upon bei ...

Is there a case of Bootstrap Offcanvas fade appearing twice in various sections of the website?

Currently in the process of updating my website using Ruby on Rails, I was given the task of integrating Bootstrap instead of using custom CSS styling. While this change posed no issue, a problem arose when I implemented the Offcanvas menu from Bootstrap. ...

Reordering Bootstrap 4.1 columns for improved visibility on smaller screens

I'm having trouble adjusting the column order for small screen resolutions in Bootstrap 4.1. I've added the order prefix but nothing seems to be changing. Here is a snippet of my HTML code. Can anyone offer assistance? <div class="container-f ...

Tips for arranging the items within the slider according to the specified direction (rtl, ltr) in the Owl Carousel slider for Angular

Is there a way to dynamically change the direction of the owl-carousel-o angular slider based on the selected language? Currently, I have set rtl: true in the owl-carousel configuration during initialization. However, when the user switches to a different ...

Is it possible to have an icon change color in a TableRow column when hovering over any part of that particular row?

I am currently using material-ui version 4.9.5. To illustrate my issue, I have created a simplified example here. I have a Table that is populated with basic JSON data. Each row in the table consists of an icon element along with its corresponding color a ...

When working with basic shapes such as "rect" or "circle," the inline SVG may not be displayed

This is the code we are using to style our checkboxes: .checkbox-default { display: inline-block; *display: inline; vertical-align: middle; margin: 0; padding: 0; width: 22px; height: 22px; background: url('data:image/ ...

Is there a way I can decrease the width of the input field on the left side?

body { background: white; font-family: 'Ubuntu', sans-serif; } .cas { display: inline-block; border: 2px solid #0C8346; background: #0C8346; font-weight: 300; font-size: 20px; padding: 5px; width: 200px; text-align: center; ...

Should you choose a pre-built CSS framework or create your own from scratch?

Have you come across a framework that meets your project needs with just a few tweaks, or have you forged your own path along the way? What advice do you have for someone facing this decision and focusing on priorities like: Implementing a CSS reset Refi ...

Prevent users from copying and pasting text or right-clicking on the website

I have been struggling to completely disable the ability for users to copy and paste or select text on my website across all devices. After much searching, I came across a solution that I implemented on my site. Below the <body> tag, I inserted the ...

Adding code containing various Google Maps elements in a fresh browser window using JavaScript

I've encountered an issue while trying to create a new page with a Google map and title based on the button clicked. Interestingly, when I copy/paste the HTML in the "newhtml" variable into an actual HTML file, it works perfectly fine. However, it doe ...

Having trouble with the background-image not displaying and the image not showing up as expected?

I am experiencing an issue with my background image not displaying on my website. I am utilizing HTML5 along with CSS. Below is the code snippet in question: body { width: 1000px; background-image: url(background.jpg); background- ...

Combining a Python backend with an HTML/CSS/JS user interface for desktop applications: the perfect synergy?

Is it possible to seamlessly combine Python code with HTML/CSS/JS to develop desktop applications? For instance, I want to create a function in Python that displays "Hello World!" and design a visually appealing user interface using HTML/CSS/JS. How can I ...

Picking an item for alignment at the center with flexbox styling

What was the reason for specifying .footer-background to have a display: flex property in order to center the .copyright-text? Why did setting display: flex on .footer not achieve the desired result? .footer-background { background-color: #00008B; ...

Create a precise layout for a footer design

I recently revamped a footer using a new UI framework in an attempt to improve it. Despite my efforts to align it properly, I encountered issues with overlapping on the right side. I experimented with using <div> and applying styles to create a diffe ...

Rearrange the sequence of numbers using JQuery when an HTML element is deleted

I'm currently working on a simple functionality where I have 5 rows, each with its own number. So initially, the rows are numbered from 5 to 1. If I remove a value like 3, the sequence should adjust to 4, 2, 1, indicating that I now have only 4 rows ...

Issue with Quantity Box not displaying values in Shopify store

I am currently seeking assistance with resolving an issue I have encountered on a Shopify theme that I recently customized. The problem lies within the quantity box on the live site where despite being able to adjust the quantity using the buttons, the act ...