Creating stylish horizontal stripes in a table using CSS

I've been attempting to assign a unique color to every nth row, but my efforts with the :nth-child property are affecting columns instead of rows. Despite trying various solutions and scouring the internet for answers, I have yet to find a successful resolution. Any input on this matter would be greatly appreciated.

https://i.sstatic.net/L4eUgtdr.png

I've experimented with adjusting the tbody and tr elements and applying styles to them, but unfortunately, my attempts have been fruitless. My goal is to style every even-numbered row, rather than every column.

table,
thead,
tbody {
  width: 100%;
}

table {
  border: 2px solid black;
  border-collapse: collapse;
  overflow: auto;
}

tr {
  display: flex;
  justify-content: center;
}

th {
  background-color: darkgray;
  width: 16.7%;
  text-align: center;
  border: 1px solid black;
  padding: 5px;
}

td {
  width: 16.7%;
  text-align: center;
  border: 1px solid black;
  padding: 5px;
}

tbody {
  display: table;
}

tbody tr :nth-child(odd) {
  background-color: #222831;
}

tbody tr :nth-child(even) {
  background-color: #31363F;
}
<table>
  <thead>
    <tr>
      <th>S.no</th>
      <th>Visitor name</th>
      <th>Patient name</th>
      <th>reason</th>
      <th>enter time</th>
      <th>leave time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Guy 1</td>
      <td>Guy 2</td>
      <td>example</td>
      <td>12:00am</td>
      <td>1:30pm</td>
    </tr>
    <tr>
      <td>1</td>
      <td>Guy 1</td>
      <td>Guy 2</td>
      <td>example</td>
      <td>12:00am</td>
      <td>1:30pm</td>
    </tr>
  </tbody>
</table>

Answer №1

Make sure to eliminate the space between tr and :nth-child in your CSS.

The selector tbody tr :nth-child(odd) targets children/descendants of tr that are odd-numbered. This means it selects the odd-numbered tds, not the odd-numbered trs. By applying styles to every other cell within each row, it creates the effect of styling every other column.

What you really want is the selector tbody tr:nth-child(odd), which identifies tr elements that are also odd-numbered children. This will style every other row instead.

table, thead, tbody{
    width: 100%;
}

table{
    border: 2px solid black;
    border-collapse: collapse;
    overflow: auto;
}

tr{
    display: flex;
    justify-content: center;
}

th{
    background-color: darkgray;
    width: 16.7%;
    text-align: center;
    border: 1px solid black;
    padding: 5px;
}

td {
    width: 16.7%;
    text-align: center;
    border: 1px solid black;
    padding: 5px;
}

tbody{
    display: table;
}

tbody tr:nth-child(odd){ /* no spaces between tr and :nth child */
    background-color: #222831;
}

tbody tr:nth-child(even){
    background-color: #31363F;
}
<table>
    <thead>
        <tr >
            <th>S.no</th>
            <th>Visiter name</th>
            <th>Patient name</th>
            <th>reason</th>
            <th>enter time</th>
            <th>leave time</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Guy 1</td>
            <td>Guy 2</td>
            <td>example</td>
            <td>12: 00am</td>
            <td>1: 30pm</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Guy 1</td>
            <td>Guy 2</td>
            <td>example</td>
            <td>12: 00am</td>
            <td>1: 30pm</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Guy 1</td>
            <td>Guy 2</td>
            <td>example</td>
            <td>12: 00am</td>
            <td>1: 30pm</td>
        </tr>
    </tbody>
</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

Saving Selected Radio button values into Jquery Array

In my HTML table, there are multiple rows with radio buttons representing the sexes in the 0th position of <td>. I am trying to store the values of sex (either 1 or 0) in an array. Below is a snippet for a table with 3 rows. HTML Code: <table> ...

How to Change the Appearance of Elements in an Array Using React.js

My situation involves an array receiving data from an API, and I'm looking to customize each button's appearance by giving them different background colors. In addition, my project includes Material UI... Below is the snippet of code I have - {op ...

Tips for placing a scroll-to-top button on a webpage

How can I ensure that the gotop stays within the confines of the story - specifically at the bottom right of the middle div, while maintaining its show/hide functionality? The dimensions of the three divs - top, story and footer - are not fixed. I have exp ...

Using a batch file to send an email

I have a goal to create an autorun file that can be placed on an external drive, flash disk, or CD. When executed, this file will run a series of commands that eventually send the computer's IP information back to me. Here is what I have so far... Th ...

Struggling to display the array after adding a new item with the push method

Seeking assistance in JavaScript as a newcomer. I have written some code to print an array once a new item is added, but unfortunately, it's not displaying the array. I am puzzled as there are no errors showing up in the console either. In my code, I ...

What is the process to replace the image in these templates?

I recently downloaded a template from the internet and am in the process of developing a website. Can someone please guide me on how to change the icons highlighted in the black and red boxes as shown in the image? I am using Visual Studio and ASP.NET fo ...

Obscured by the overflow

How can I make my navbar stay sticky at the top all the time while scrolling without the content of my cards overlapping it? I am using Bootstrap 5 and have tried setting the position to sticky and overflow to hidden, but the issue persists. Any suggestion ...

Using Bootstrap 4 navigation pills within a navbar

I am just starting to learn about bootstrap and I am trying to integrate nav pills into my navigation bar to switch the content of the page, similar to tabs. Everything works fine without the navbar, but I really want it to be part of my navbar. Here&apos ...

Is there a way to customize the text color in a dynamic AJAX Barchart display?

I'm working on creating a bar chart using AJAXToolkit to showcase data related to our system. However, I'm facing an issue with changing the color of the text within the chart. The text is currently in black against a grey background, making it d ...

Determining whether a mouse click occurred in the left or right side of a DIV element

Is there a way to determine whether a mouse click occurred on the left or right side of a div element? I'm curious about calculating this: $("div").click(function(e){ // code to calculate left or right half }); <div>Content that can change&l ...

Exploring the functionality of the onblur HTML attribute in conjunction with JavaScript's ability to trigger a

How do the HTML attribute onblur and jQuery event .trigger("blur") interact? Will both events be executed, with JavaScript this.trigger("blur") triggering first before the HTML attribute onblur, or will only one event fire? I am using ...

Enhancing User Interfaces with React and CSS Styling

I need to incorporate two unique React components on my webpage: Component1 and Component2. Code for Page1: <Component1/> <Component2/> While the individual components will have their own CSS files for styling, I also have a page1.css file f ...

Adjust the DIV to match the dimensions of the background image

Can anyone help me figure out how to make the purple box match the size of the background image in this screenshot using Bootstrap and its flex classes? I want it to maintain that size when resizing the screen. If you'd like to try it out yourself, I ...

Dealing with interstitial advertisements on mobile devices: What is the best approach for handling zoom?

I am facing a challenge with displaying interstitial ads on mobile sites. Different publishers may have varying viewport settings and initial scales on their mobile sites, making it difficult to ensure that the ad appears correctly on all devices with the ...

Is C# code allowed in a CSS file for ASP.NET MVC 5?

Looking for the best approach to define a set of colors in one place and utilize functions to create different shades of these colors, then incorporate them into CSS files. Transitioning from Ruby on Rails to ASP.NET has prompted me to seek alternatives. ...

What is the best way to arrange these divs one on top of another?

I have combed through numerous resources but still haven't found a solution to my problem. I am struggling with figuring out how to stack the "top_section" div on top of the "middle_section" div. Currently, "middle_section" is appearing above "top_sec ...

Enhance your website with a sophisticated jQuery Fancybox gallery - customize your image settings

How can I ensure that an open photo always remains visible in the center regardless of page zoom on phones with desktop version enforcement enabled? You can find the website at: To view the photo without approximation, click here: without approximation ...

Trouble with bringing in the solana/web3.js library

After successfully importing the solana/web3.js package and running it within a NodeJS file, everything was working fine. However, things took a turn when attempting to connect this file with basic HTML, resulting in an error being thrown. import { Tra ...

Comparison: CSS3 versus Angular Material CSS

My ul and li design in CSS is functioning perfectly on Bootstrap. However, when I apply the same code to an Angular Material project, the Angular Material CSS seems to be overriding my styles. CSS Code: li { width: 2em; height: 2em; text-alig ...

Replacing CSS properties using Python in a file

I'm facing a challenge with 2 files in my project: config.txt and a CSS file. My goal is to extract the color attribute from the config file and update it in the CSS file accordingly. The content of the Config file is as follows: color: #ffffff; H ...