Problem with alternating row colors in my table

Trying to add some style to my table on the webpage by alternating row colors, but I'm running into some issues. I've followed tutorials and tried using different CSS selectors like nth-of-type(even), nth-of-type(2n+0), and nth-of-type(odd). However, instead of coloring alternate rows vertically, it's doing so horizontally. There seems to be a problem with my code but I can't quite pinpoint it. Any suggestions or insights would be greatly appreciated. Let me know if you need more code as this is part of a larger front and back-end system. Thanks!

Table Screenshot - https://i.stack.imgur.com/lJsTt.jpg (Issue with uploading image on Stack)

Below is the snippet of the relevant code:

.customer_table {
        border-collapse:collapse;
        margin-left:auto;
        margin-right:auto;
        margin-top: 50px;
        font-size: 18px;
      }
    
      .customer_table thead tr {
        background-color: #4cb59c;
        text-align: left;
      }
    
      .customer_table th,
      .customer_table td {
        padding: 12px 15px;
      }
    
      .customer_table tbody tr {
        border: 0.5px solid #16a583;
      }
    
      .customer_table tr :nth-of-type(even) {
        background-color: #dcc79e;
      }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>   
    <meta http-equiv="X-UA-Compatible" content="IE=edge">       
    <meta name="viewport" content="width=device-width, initial-scale=1.0">          
    <title>Document</title>      
</head>
<body>         
    <table width="60%" class="customer_table">        
        <thead>           
            <tr>              
                <th>ID</th>
                <th>Name</th>
                <th>Address</th>
                <th>Phone</th>
                <th>Email</th>        
                <th>Orders</th>                   
                <th>Edit</th>                  
                <th>Delete</th>           
            </tr>         
        </thead>     
    </table>
</body>
</html>

Answer №1

Implement the following code for each <td> and <th>, as shown in Figure I. If it doesn't have the desired effect, there may be other CSS rules with higher specificity. Refer to Figure II:

Figure I

th:nth-of-type(odd),
td:nth-of-type(odd) {
  background-color: #16a583;
}

th:nth-of-type(even),
td:nth-of-type(even) {
  background-color: #dcc79e;
}

Figure II

Add a class to <table>

<table class="classA">

Create duplicate class selectors in CSS

table.classA.classA th:nth-of-type(odd),
table.classA.classA td:nth-of-type(odd) {
  background-color: #16a583;
}

table.classA.classA th:nth-of-type(even),
table.classA.classA td:nth-of-type(even) {
  background-color: #dcc79e;
}

If this approach fails, consider using !important which overrides most styles, but use it cautiously as it can create more issues than solutions. See Figure III.

Figure III

th:nth-of-type(odd),
td:nth-of-type(odd) {
  background-color: #16a583 !important;
}

th:nth-of-type(even),
td:nth-of-type(even) {
  background-color: #dcc79e !important;
}

Additionally, remove any previous CSS properties related to background within the <table> or convert them into comments (e.g.,

/* CSS content made inert by being a comment */
).

Example

table {
  border-collapse: collapse;
  margin: 50px auto 0;
  font-size: 18px;
}

th, td {
  padding: 12px 15px;
  border: 1px solid #930;
}

th:nth-of-type(odd),
td:nth-of-type(odd) {
  background-color: #16a583;
}

th:nth-of-type(even),
td:nth-of-type(even) {
  background-color: #dcc79e;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Orders</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    </tbody>
  </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

The outer border of the Angular Material Mat Grid List is beautifully highlighted

In my project, I have a mat grid list where users can define the number of rows and columns. My goal is to display borders around each mat-grid-title cell. However, I am struggling to properly display the outermost black border for the entire mat-grid-lis ...

Having trouble getting JavaScript to select a stylesheet based on time?

I am attempting to implement two different styles based on the time of day. Here is the code I am using to select the CSS file depending on the current time: <script type="text/javascript"> function setTimedStylesheet() { var currentTim ...

Developing Wordpress plugins involving images - path not found

I'm currently developing a WordPress plugin and encountering some issues with images. My plugin is located in wp-content/plugins/my-plugin/ directory, and within that, there's a folder named images/test.png - how can I properly reference this ima ...

How to allow users to input strings on web pages with JavaScript

I created a Language Translator using java script that currently translates hardcoded strings in an HTML page. I want to enhance its flexibility by allowing users to input a string into a textbox/textarea for translation. Any assistance would be greatly a ...

Using Bootstrap 3 with ResponsiveSlides.js

I've been struggling to center the ResponsiveSlides slider within a bootstrap column. Despite resizing my large images to 80% in the CSS, they still refuse to align properly since they're no longer fitting the entire screen at 100%. Can someone l ...

Calculate the quantity of rows within a specific div container

Looking at the image provided, there are multiple divs inside a main div. I am trying to figure out how many rows the main div contains. For instance, in this particular scenario, there are 5 rows. It is important to mention that the inner div is floated ...

Having trouble accessing DOM elements in Durandal

Running on Durandal, this mobile app features names and images on an HTML page. The function below helps format the page: define(function (){ function getAutors(myUrl) { $.ajax({ type: "GET", url: myUrl, data: { num ...

Effortless pagination across various pages, utilizing diverse CSS selectors

I've integrated simple pagination into my website, but I've encountered a issue. My navigation consists of CSS tabs, each holding a unique pagination component. Here is the jQuery pagination code snippet: $(function(){ var perPage = 8; var open ...

Encountering difficulties linking to a stylesheet or a script in an HTML file delivered by Express server

Currently, I'm facing the challenge of breaking down my Express app code into multiple files. Unfortunately, I am unable to utilize <link href> or <script src> for linking stylesheets or scripts. Below is the relevant snippet from my inde ...

Enhance the serialization with more information when submitting

I'm trying to submit form data along with some extra information using serializeArray, but for some reason it's not working as expected. $("#submitBtn").submit(function(ev) { ev.preventDefault(); var info = $(this).serializeArray(); info ...

What is the best way to implement dynamic generation of Form::date() in Laravel 8?

Is there a way to dynamically generate the Form::date() based on the selection of 1? If 1 is selected, then display the Form::date() under the Form::select(). However, if 0 is selected, then hide the Form::date() in this particular view. For example: Sel ...

Why aren't CSS media queries functioning in the existing stylesheet?

I have a good grasp on responsive design and CSS techniques. However, I am encountering an issue where the media query for mobile dimensions is not working as expected. When monitoring the applied styles in Chrome, only the min-device-width styles are be ...

Exploring the asp.net MVC framework with the integration of HTML5 date input

Currently, I am working on an ASP.NET MVC project using Visual Studio 2013. One of the issues I am encountering is related to a date input field. When I click on the field and focus on it, the automatic HTML5 datepicker does not appear. If I enter the da ...

What are the steps to utilize vue.js for dynamically adjusting my sidebar based on a URL input?

Greetings to the amazing community of Vue.js enthusiasts, I am a novice looking to develop a single-page web application using vue.js. This application will consist of a fixed header and dynamic body content that changes based on user interactions. Here&a ...

Enclosing two smaller boxes within a larger box

I'm attempting to replicate the layout shown in the desired.png file (specifically with an outer box surrounding two inner boxes). Currently, my code doesn't display the expected outer box around the two inner boxes. How can I modify it to achie ...

Tips for modifying the appearance of a text file within an iframe

Lately, I've been facing a dilemma on my website. I have an iframe code that looks like this: <iframe src ="http://hokuco.com/cde/user.txt" id ="myIframe"></iframe> However, the consolas font used in the iframe makes my website appear ...

Should PHP be avoided in CSS files?

I've recently developed a CSS page named style.php and at the beginning, I included this line: <?php header("Content-type: text/css"); ?> What are your thoughts on this approach? Is it considered a bad idea? The reason behind doing this is tha ...

When previewing a card on Twitter's validator tool, the image displays properly. However, when attempting to share the image on

Below is the code that I have written on my HTML page: <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@name" /> <meta name="twitter:title" content="Twitter - image share" /> <meta name="twi ...

"PHP MySQL news error" - An unexpected error occurred

Just incorporated a news system using MySQL/PHP, it functions flawlessly; however, encounters errors when inserting HTML. For instance, inserting a YouTube video results in the addition of / or \ Can someone advise on what changes need to be made in ...

Image uploading in Angular is not compatible with Internet Explorer, however, it functions correctly in Google Chrome

As of now, my implementation has been successful in all browsers except for Internet Explorer 11. Despite being able to successfully upload .jpeg, .jpg, and .png images in Google Chrome, I am facing issues when trying to upload them in IE 11. The code wo ...