Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to set the table layout algorithm using CSS ?
Next article icon

How To Set Alternate Table Row Color Using CSS?

Last Updated : 21 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To set alternate table row colors, we can use the :nth-child() pseudo-class in CSS. This technique helps improve the readability of a table by making it easier to differentiate between rows. In this article, we’ll cover how to set alternate row colors using the :nth-child() pseudo-class and provide step-by-step instructions to implement it in your HTML and CSS code.

Using the :nth-child() pseudo-class

The :nth-child() selector targets elements based on their position in a group of sibling elements, which in this case refers to table rows (<tr>). You can specify whether you want to style even or odd rows using this pseudo-class.

Syntax:  

:nth-child(number) {     // CSS Property }

Where number is the argument that represents the pattern for matching elements. It can be odd, even or in a functional notation.  

  • odd: It represents the elements whose position is odd in a series: 1, 3, 5, etc

Syntax:

element:nth-child(even)
  • even: It represents the elements whose position is even in a series: 2, 4, 6, etc.

Syntax: 

element:nth-child(odd)

Example 1: In this example, we use the :nth-child(even) selector to apply a light green background to even-numbered rows, enhancing readability.

html
<!DOCTYPE html> <html>  <head>      <!-- CSS style to set alternate table              row using color -->     <style>         table {             border-collapse: collapse;             width: 100%;         }          th,         td {             text-align: left;             padding: 8px;         }          tr:nth-child(even) {             background-color: Lightgreen;         }     </style> </head>  <body>     <table>         <tr>             <th>Name</th>             <th>Designation</th>             <th>Salary</th>         </tr>          <tr>             <td>Steve</td>             <td>Manager</td>             <td>1,00,000</td>         </tr>          <tr>             <td>SURAJ</td>             <td>Assistant Manager</td>             <td>50,000</td>         </tr>          <tr>             <td>Khushboo</td>             <td>Analysist</td>             <td>65,000</td>         </tr>          <tr>             <td>Kartik</td>             <td>Worker</td>             <td>20,000</td>         </tr>          <tr>             <td>Saksham</td>             <td>Worker</td>             <td>20,000</td>         </tr>     </table> </body>  </html> 


Output: 

Set-Alternate-Table-Row-Color

Using the :nth-child() pseudo-class


Example 2: In this example, we use the :nth-child(odd) selector to apply a light green background to odd-numbered rows, providing a visual distinction between the rows.

html
<!DOCTYPE html> <html>  <head>     <title>         Set alternate row in table     </title>      <style>         table {             border-collapse: collapse;             width: 100%;         }          th,         td {             text-align: left;             padding: 8px;         }          tr:nth-child(odd) {             background-color: Lightgreen;         }     </style> </head>  <body>     <table>         <tr>             <th>Name</th>             <th>Designation</th>             <th>Salary</th>         </tr>          <tr>             <td>Steve</td>             <td>Manager</td>             <td>1, 00, 000</td>         </tr>          <tr>             <td>SURAJ</td>             <td>Assistant Manager</td>             <td>50, 000</td>         </tr>          <tr>             <td>Khushboo</td>             <td>Analysist</td>             <td>65, 000</td>         </tr>          <tr>             <td>Kartik</td>             <td>Worker</td>             <td>20, 000</td>         </tr>          <tr>             <td>Saksham</td>             <td>Worker</td>             <td>20, 000</td>         </tr>     </table> </body>  <html> 


Output:  

Using-nth-child-pseudo-class

Set Alternate Table Row Color Using CSS


Supported Browser:

  • Google Chrome 
  • Edge 
  • Firefox
  • Opera
  • Safari


Next Article
How to set the table layout algorithm using CSS ?

A

AkshayGulati
Improve
Article Tags :
  • CSS
  • Web Technologies
  • CSS-Misc

Similar Reads

  • How to Change the Background Color of Table using CSS?
    Changing the background color of a table using CSS can help improve the visual appearance of a website or web application. we will learn how to change the background color of the table using CSS with different approaches. These are the following approaches: Table of Content Using Inline CSSUsing Int
    2 min read
  • How to highlight alternate table row using jQuery ?
    In this article, we will set the highlight on an alternative table row using jQuery. The :nth-child(2n) selector is used to select the alternative row and the addClass() method is used to set the style of the alternative rows. Syntax: $(tr:nth-child(2n)").addClass("GFG"); Here, we will create a simp
    2 min read
  • How to set the table layout algorithm using CSS ?
    You can set the table layout algorithm using table-layout property which is used to display the layout of the table. In this article, you learn both auto and fixed layout algorithms to form table. Syntax: table-layout: auto|fixed; Approach 1: Using auto layout algorithm. Note: In the auto-layout alg
    2 min read
  • How to Align Right in a Table Cell using CSS ?
    Aligning right in a table cell means positioning the content of the cell to the right side. In CSS, this can be achieved by applying the text-align property to the table cell or using the td selector. Align right in a table cell Using text-align propertyTo align the contents of a table cell to right
    2 min read
  • How to Change the Color of Bullets using CSS?
    Changing the color of bullets using CSS means styling the bullet points in a list (<ul> or <ol>) to have a different color than the text. This can be done using pseudo-elements or setting the color property on the list item, enhancing design consistency. 1. Adding An Extra MarkupBy addin
    2 min read
  • Space between two rows in a table using CSS
    The space between two rows in a table can be done using CSS border-spacing and border-collapse properties. The border-spacing property is used to set the spaces between cells of a table and border-collapse property is used to specify whether the border of the table is collapsed or not. The border-sp
    2 min read
  • How to define the color of the border using CSS ?
    We can give the color of the border using border or border-color properties. We need to give border-style property. Approach 1: We will give the color of the border using the border property of CSS.We will give our CSS inside the tags which are also known as an inline style.We need to give the borde
    2 min read
  • How to create a table row using HTML?
    Define a row in a table by using a <tr> tag in a document. This tag is used to define a row in an HTML table. The tr element contains multiple th or td elements. Syntax:<tr> ... </tr>Example: In this example, a table row in HTML5, utilizes the <tr> tag within the <tbody
    1 min read
  • Power BI - Add alternative Row Colors to the Table
    In Microsoft Power Bi, “Alternate Row Color” is substantially used in a table to display their data more accurately. utmost of the association needs to present their data professionally. Applying alternate and unique colors to the data makes the waste more perfect, making the data view in various fo
    2 min read
  • How to make a Animated Table using HTML and CSS ?
    Table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. In this article, we are going to create a Table with animation over its columns. We are going to implement it using HTML and CSS. Approa
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences