Alter the color of the font in every other row

I attempted using the tr:nth-child() selector but it didn't work as expected

Here is my JSP and CSS code. The JSP fetches two lists from a Java class, includes a validation if the list is empty, and then displays the values. The data-grid is a table class.

<table class="data-grid" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th colspan="6">Excesses to be actioned</th>
                </tr>
            </thead>
            <tr class="header">
                <th>Excess ID</th>
                <th>Date</th>
                <th>Client</th>
                <th>Product</th>
                <th>Amount</th>
                <th>DA</th>
            </tr>
            <tbody id="tbNa">
                <c:choose>
                    <c:when
                        test="${empty actionBean.excessList.newActiveCustomerExcessUIList}">
                        <tr>
                            <td colspan="6" align="center"><font color="red">No
                                    data available for Active Excess List</font></td>
                        </tr>
                    </c:when>
                    <c:otherwise>
                        <c:forEach
                            items="${actionBean.excessList.newActiveCustomerExcessUIList}"
                            var="customerExcess" varStatus="loop">
                            <c:set var="clientName" value="${customerExcess.clientName}"
                                scope="page"></c:set>
                            <c:set var="ultimateParent"
                                value="${customerExcess.ultimateParent}" scope="page"></c:set>
                            <c:set var="cif" value="${customerExcess.cif}" scope="page"></c:set>
                            <c:forEach items="${customerExcess.excessList}" var="excess">
                                <tr>
                                    <td><c:out value="${excess.excessId }"></c:out></td>
                                    <td><c:out value="${excess.openDate }"></c:out></td>
                                    <td><a href="#" onclick="openClientExcessPage('${cif}');"><c:out
                                                value="${clientName}"></c:out></a></td>
                                    <td><c:out value="${ultimateParent}"></c:out></td>
                                    <td><c:out value="${excess.excessAmount }"></c:out></td>
                                    <td><c:out value="${excess.excessDa}"></c:out></td>
                                </tr>
                            </c:forEach>
                        </c:forEach>
                    </c:otherwise>
                </c:choose>
                <c:choose>
                    <c:when
                        test="${empty actionBean.excessList.approvedCustomerExcessUIList}">
                        <tr>
                            <td colspan="6" align="center"><font color="red">No
                                    data available for Approved Excess List</font></td>
                        </tr>
                    </c:when>
                    <c:otherwise>
                        <c:forEach
                            items="${actionBean.excessList.approvedCustomerExcessUIList}"
                            var="customerExcess">
                            <c:set var="clientName" value="${customerExcess.clientName}"
                                scope="page"></c:set>
                            <c:set var="ultimateParent"
                                value="${customerExcess.ultimateParent}" scope="page"></c:set>
                            <c:forEach items="${customerExcess.excessList}" var="excess">
                                <tr>
                                    <td><c:out value="${excess.excessId }"></c:out></td>
                                    <td><c:out value="${excess.openDate }"></c:out></td>
                                    <td><a href="#" onclick="openClientExcessPage('${cif}');"><c:out
                                                value="${clientName}"></c:out></a></td>
                                    <td><c:out value="${ultimateParent}"></c:out></td>
                                    <td><c:out value="${excess.excessAmount }"></c:out></td>
                                    <td><c:out value="${excess.excessDa }"></c:out></td>
                                </tr>
                            </c:forEach>
                        </c:forEach>
                    </c:otherwise>
                </c:choose>
            </tbody>
        </table>

The data-grid is a table class. Below is the CSS code written for the table. I want to change the font color of the data in the even rows without changing the row color.

.data-grid {
    margin: 0px;
    padding: 0px;
    width : 60%;
    height : 40%;
    padding-top: 10%;   
    padding-left: 15%;
    white-space: nowrap;    
}

.data-grid select {
    padding: 3px 0px;
    border: 1px solid #DCDCDC;
}

.data-grid td {
    font-size: 11px;
    color: #000000;
    background-color: white;
    border: 0px;
    border-left: 1px;
    border-bottom: 1px;
    border-style: solid;
    border-color: #DBE5F1;
    padding: 5px;
}


.data-grid thead {
    color: #004080;
    font-size: 12px;
    font-weight: normal;
    border-color: #FFF;
    vertical-align: text-top;
    background-color: #C3C3C3;
    height: 20px;
    padding: 2px 5px;
}

.data-grid .header th {
    color: #002577;
    font-size: 11px;
    font-weight: bold;
    border-color: #FFF;
    text-align: center;
    vertical-align: text-top;
    background-color: #D5E0E6;
}

Answer №1

 .table-body tr:nth-child(2n) {color: #CCC}
 .table-body tr:nth-child(2n+1) {color: #FFF} 

Answer №2

Take a look at this illustration

<!DOCTYPE html>
<html>
<head>
<style> 
.myclass1 tr:nth-child(odd)
{
background:#ff0000;
}
.myclass1 tr:nth-child(even)
{
background:#0000ff;
}
.myclass2 tr:nth-child(odd)
{
background:#00ff00;
}
.myclass2 tr:nth-child(even)
{
background:#ff0000;
}
</style>
</head>
<body>

<table width="100%" border=1 class="myclass1">

<tr><td>aaaa</td></tr>
<tr><td>bbbb</td></tr>
<tr><td>cccc</td></tr>
<tr><td>dddd</td></tr>

</table>

<table width="100%" border=1 class="myclass2">

<tr><td>aaaa</td></tr>
<tr><td>bbbb</td></tr>
<tr><td>cccc</td></tr>
<tr><td>dddd</td></tr>

</table>

</body>
</html>

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

challenges encountered while using .css() to customize the height

Currently, I am working on a tutorial to learn about basic jQuery plugins. I am facing a challenge in understanding why my code is not adjusting the height of my <p></p>. The task requires creating a plugin that can set the height of specified ...

Continual running of a jquery command

Here is a snippet of jQuery code that may need some clarification. In this code, when the element with ID 'box1' is clicked, it triggers an event that directs to a modal dialog where there is a class called 'colors'. The issue here is t ...

Could images be positioned in this manner without using the float property?

Looking to arrange images in a left-to-right flow while keeping them centered on the screen: https://i.stack.imgur.com/WHBv9.png However, I'm struggling to achieve this as the last image always ends up centered horizontally as shown below: https://i ...

Tips for positioning a div under a floated div

I've searched extensively for a solution to this problem with no luck. I'm having trouble moving my main code under the Navigation Bar. Maybe my code is too unconventional, but I can't seem to figure it out. Can anyone assist me? Thank you i ...

Counting the number of elements in a list can be problematic when using Firefox

Currently, I am in the process of writing CSS to customize the appearance of an XML document. Specifically, I aim to alter how child elements are displayed and have them resemble HTML OL/UL/LI elements. The structure of my XML file is as follows: <?x ...

Tips on aligning carousel images and captions next to each other

Would anyone be able to assist me in creating a carousel similar to this? I haven't been able to locate any examples or tutorials that match the design of my desired carousel. Here is the design I am referring to ...

Guide to making a vertical clickable separator/line using bootstrap

Seeking advice on creating a layout where the left side consists of a sidebar menu occupying 24% and the right side contains main content taking up 75%, with a clickable vertical divider at 1% between them. When this line is clicked, the left part will hid ...

Unable to activate the navbar toggle feature on Bootstrap 4.0

****I have started working with bootstrap 4.0, but I am facing issues with the navbar toggle functionality. Even after trying to copy code from the official documentation and using the current alpha version of bootstrap, the toggle is not working as expe ...

Webpack2 now transforms sass/scss files into JavaScript rather than CSS during compilation

I've created a webpack script to compile all .scss files into one css file. I decided to use webpack instead of gulp or grunt for simplicity, as it can be configured in a single file. However, I am encountering an issue where scss files are being com ...

What is the best way to implement a dropdown in MUI and React that displays functional components as items?

Here is a list of dummy components: const OwnerList = () => { return ( <Box sx={{ display: 'flex', }} className="owner-container" > <Avatar src='https://hips.hearstapps.com/hmg- ...

Align images to the left in Typora for a cleaner look

Typora typically aligns all images in the center by default. https://i.stack.imgur.com/TrqIM.png In my search through the github.css, I was unable to locate any instances of img. $ grep img "/Users/me/Library/Application Support/abnerworks.Typora/themes ...

Adjusting the arrangement of div elements based on screen dimensions or orientation within an Ionic grid system

My HTML setup looks like this: <div class="row"> <div class="col-sm-12 col-md-6 col-xl-3"> </div> <div class="col-sm-12 col-md-6 col-xl-3"> </div> <div class="col-sm-12 col-md-6 col-xl-3"> </d ...

CSS - Maximizing One Column's Width While Allocating Percentage Space for Two Columns?

It's been quite some time since I delved into the world of web development and I seem to have forgotten how to tackle this particular issue. Despite hours spent searching online, I've gained knowledge about flex-boxes and other useful tools, but ...

Boost the scrolling velocity of my sidebar using Jquery

Hello there, I am completely new to the world of web development and particularly to using JavaScript and jQuery. I am interested in learning how to adjust the speed at which my Sidebar scrolls down as the user navigates through the page. Here is the jQue ...

Does the padding and margin of an element alter when the position is set to relative?

By utilizing relative positioning, an element can be moved in relation to its original position in normal flow. - Citation from the book "HTML&CSS: design and build websites" by John Duckett If an element has a relative position property, it opens up ...

What is the best way to get rid of a tooltip from a disabled button

I am facing an issue with my button that is disabled using ng-disabled. The problem is that the button still shows a tooltip even when it is disabled. I need to find a way to remove the tooltip when the button is disabled. Check out my Button ...

What steps can you take to convert your current menu into a responsive design?

I am currently developing a website using PHP, HTML, and CSS. The site is not responsive, and I want to make the menu responsive. My global navigation and admin interface are not adapting properly as they are designed using tables. Is there a method I can ...

What is preventing the input box from shrinking further?

I created a search box using CSS grid that is supposed to shrink when the page is resized. However, it doesn't seem to shrink as much as I expected it to. Here is how it appears when fully extended: https://i.stack.imgur.com/tPuCg.png And here is how ...

Incorporating the 'react-day-picker' DatePickerInput in a Bootstrap Modal Interface

I am attempting to incorporate a DatePickerInput component into a bootstrap modal. I am hoping to achieve a similar look as shown on the official website here. However, when placed inside the bootstrap modal, it appears like this. My assumption is that ...

Assign a class to a dynamically loaded element on a webpage

As I work on developing my website, I am facing an issue that I need help with. My header is stored in a separate HTML document and it looks like this: <div class="topnav" id="myTopnav"> <a href="Index.html" id=" ...