What is the best way to design an HTML table that features alternating colors for each row and column?

I recently came across this image showcasing a beautifully designed table:

https://i.stack.imgur.com/6K63q.png

The current HTML code I have applies colors to every even line, including the year columns and subcolumns. Is there a way to assign unique mixed colors for the cells in year columns and even rows, as well as different mixed colors for the cells in year columns and odd rows, similar to what is shown in the image?

Manually setting colors for each column can be time-consuming, especially when dealing with pages containing over 200 rows. Is there an efficient CSS method to achieve this desired effect without having to specify individual colors?


                table {
                    font-family: arial, sans-serif;
                    border-collapse: collapse;
                }

                .beta td {
                    text-align: center;
                }

                TH.Title {
                    background-color: #A0E0A0
                }

                tr:nth-child(even) {
                    background-color: #bbffcc;
                }

                .Year2016 {
                    background-color: #ffff99
                }

                .Year2017 {
                    background-color: #ccff99
                }

                .Year2018 {
                    background-color: #ffff99
                }
            
        
<TABLE class="beta" cellspacing="2" cellpadding="6" border="1">
            <TR>
            <TH class="Title" rowspan="2">code</TH>
            <TH class="Title" rowspan="2">Text</TH>
            <TH class="Title" rowspan="2">Text</TH>
            <TH class="Title" rowspan="2">Text</TH>
            <TH class="Title" rowspan="2">Text</TH>
            <th class="Year2016" colspan="2">2016 Items</th>
            <th class="Year2017" colspan="2">2017 items</th>
            <th class="Year2018" colspan="2">2018 Items</th>
        </tr>

        <TR>
        <TH class="Year2016">Item 1</TH>
        <TH class="Year2016">Item 2</TH>
        <TH class="Year2017">Item 1</TH>
        <TH class="Year2017">Item 2</TH>
        <TH class="Year2018">Item 1</TH>
        <TH class="Year2018">Item 2</TH>
    </TR>

Answer №1

The CSS already includes rules for styling odd and even child elements.

Visit this link for more information

To streamline your styling process, you can assign an ID to the table and apply styles from there. For example, if you have a <table id="beta">, you can write your CSS as follows:

#beta tr:nth-child(even) {color:limegreen;}
#beta tr:nth-child(odd) {color:lime;}

This code will make even rows appear in lime green and odd rows in lime color. The same concept can be applied to <td> elements as well. You can also refer to this example using lists from a Stack Overflow answer.

Answer №2

In my opinion, utilizing the <col> tag allows you to easily color an entire column.

For more information on the syntax, I recommend checking out this link:

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

Steps for generating a dropdown menu using API JSON information

I'm new to using Webapi with AngularJS. I have a URL that returns data in JSON format, and I want to extract only the employee names from the data and display them in a dropdown list using AngularJS. Any assistance would be greatly appreciated. ...

How do I print a QTableWidget using QT5 and resize the table to fit perfectly on one side of an A4 sheet?

My QT5 application is designed to generate a monthly rota/timesheet in csv format, which can be easily read and printed using Excel or LibreOffice. However, I want to enhance the functionality by printing the table directly from Qt to the printer. I have ...

Preventing the automatic selection of the initial item in a PrimeNG dropdown menu

While utilizing the p-menu component from PrimeNG in popup mode ([popup]="true"), I encountered an unexpected issue where the first item in the menu is automatically selected and turns gray. Here is the code snippet that I am using: <p-menu #menu [popu ...

How to retrieve the row index from a table using JavaScript

This question has cropped up numerous times, and despite trying various solutions suggested before, none of them seem to be effective in my case. Within a modal, I have a table where users can make changes that are then used to update the database. The ta ...

In Groovy (or Java), learn the techniques to properly escape double quotes within HTML inner text while leaving attributes untouched

I am currently utilizing an HTML rendering engine that is based on Groovy within a Web Content Management (WCM) system. I have encountered a scenario where the user inputs rich text content into a form based on TinyMCE, which appears as follows: <p> ...

javascript function not being invoked

Currently, I have incorporated the following HTML <html> <head> <Title>EBAY Search</title> </head> <script language="JavaScript" src="ajaxlib.js"></script> <body> Click here & ...

What is the best method to position a modal in the exact center of the screen?

Is there a way to position the modal at the center of the screen? I have tried implementing this HTML and JavaScript code. Interestingly, it works fine in Chrome console but fails when I try to refresh the page. $('.modal').css('top', ...

Is it possible to assign numerical values to attributes in HTML code?

I'm unsure about something - is it possible to set an attribute value as a number? For example: <div data-check="1"></div> Is this the correct way to do it or not? I've heard conflicting opinions, with some people saying you shouldn ...

aligning columns within a flexible element that has a maximum height

Within this flex element, I have set flex: column wrap; and max-height: 150px;. The issue I'm facing is that justify-content doesn't seem to be working. However, when I switch to using height: 150px, the alignment works fine. This problem arises ...

Different methods of transferring PHP post information to JavaScript

Is there a cleaner and more elegant way to pass posted PHP variables to JavaScript for manipulation, rather than using inline JavaScript? I currently have a simple PHP form that posts data to specific variables and then uses inline JavaScript to handle the ...

Adjusting the slider with jQuery and CSS to extend beyond 100% while remaining within the div boundaries

Currently, I'm working on customizing a jQuery slider. My goal is to achieve the same functionality as the jQuery UI slider where you can set a max value like "2500" and the slider will smoothly adjust without going outside of the specified div or scr ...

Trouble with z-index functionality in jQuery datatable

I'm struggling to get the Action Box displayed as an upper layer. I've already tried using z-index but it doesn't seem to make any difference. https://i.stack.imgur.com/rJ1vL.png $(document).ready(function () { ...

The most efficient method for interacting with externally generated HTML in Rails

For my Rails page help documentation, I utilized the HelpNDoc help authoring tool to generate a folder containing HTML pages and additional folders for JavaScript, images, stylesheets, and libraries. Now I am seeking the most efficient way to integrate thi ...

auto-scrolling webpage as elements come into view

Here is some jQuery code I have: $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); $(".third").delay(1800).fadeIn(1000); $(".fourth").dela ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Loading a different webpage seamlessly without having to reload the current one

Here is a snippet of my code in okay.html: {% extends "sch/base.html" %} {% load staticfiles %} {% block content %} <div class="row" id="ada"> <form action="" method="post> {% csrf_token %} <div align="center" class="cont ...

Images in Angular Firebase causing scroll problems in Chrome

In regard to Angular 6, a common issue arises with slow image loading specifically in Chrome, not other browsers. The project utilizes Firebase and the snapshotchange method to fetch images for the Angular project. Image URLs used are like this: . The iss ...

Trying to open the DB URL in a new tab when using PHP and MySQL will not work as expected

Before I begin, I want to express my gratitude to the community of StackOverFlow users for providing valuable answers. As a novice in PHP/mySql, I am learning through books and examples from others. Currently, my issue revolves around a MySQL database wit ...

How to Align Text and Image Inside a JavaScript-Generated Div

I am attempting to use JavaScript to generate a div with an image on the left and text that can dynamically switch on the right side. What I envision is something like this: [IMAGE] "text" Currently, my attempt has resulted in the text showing ...

Can you explain the differences between offsetHeight, clientHeight, and scrollHeight for me?

Have you ever wondered about the distinction between offsetHeight, clientHeight, and scrollHeight? What about offsetWidth, clientWidth, and scrollWidth? Understanding these differences is crucial for working effectively on the client side. Without this kn ...