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 use CSS variables with TailwindCSS ?
Next article icon

How to use a sass variable in nth child class ?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

SASS is one of the most popular CSS extension languages which provides superpower to the normal CSS. The nth-child() class is a pseudo-class, when some HTML element has more than two elements of the same kind then we can use the nth-child class pseudo-class to provide styling to those elements without defining a separate class.

For example, if a div has 5 <p> element then we can use nth-child(), For this article, we will use the sass variable in an nth-child class.

Syntax:

<css selector> : nth-child(an + b){ css properties };
 

Parameters:

  • 2n: It will affect the even number of the elements
  • 2n + 1: It will affect the odd number of the elements
  • 10n – 1: It will affect the one less than the tenth element, eg: 9th, 19th, etc.

Example 1: In this example, we will add 6 <p> elements in the <div> and create a variable of the name text-color and use it in the nth-child(2n) class to change the colors of the text which will change the colors of the even number of text from the list.

style.scss

CSS

$text-color : green;
  
p:nth-child(2n){
    color: $text-color;
}
                      
                       

Compiled CSS file.

style.css

CSS

p:nth-child(2n) {
    color: green;
}
                      
                       

Index.html

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sass variable in nth-child class</title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <h2 style="color: green;">GeeksforGeeks</h2>
  
    <div class="main">
        <p class="text1">1. GeeksforGeeks</p>
        <p class="text2">2. GeeksforGeeks</p>
        <p class="text3">3. GeeksforGeeks</p>
        <p class="text4">4. GeeksforGeeks</p>
        <p class="text5">5. GeeksforGeeks</p>
        <p class="text6">6. GeeksforGeeks</p>
    </div>
</body>
</html>
                      
                       

Output:

 

Example 2: In this example, we will use the two different sass variables for both the odd and even number of elements in the nth child class, green color for even elements and red color for odd elements.

style.scss

CSS

$even-text-color : green;
$odd-text-color : red;
  
p:nth-child(2n){
    color: $even-text-color
} 
p:nth-child(2n-1){
    color: $odd-text-color;
}
                      
                       

Complied CSS file

style.css

CSS

p:nth-child(2n) {
    color: green;
}
  
p:nth-child(2n-1) {
    color: red;
}
                      
                       

Index.html

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sass variable in nth-child class</title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <h2 style="color: green;">GeeksforGeeks</h2>
  
    <div class="main">
        <p class="text1">1. GeeksforGeeks</p>
        <p class="text2">2. GeeksforGeeks</p>
        <p class="text3">3. GeeksforGeeks</p>
        <p class="text4">4. GeeksforGeeks</p>
        <p class="text5">5. GeeksforGeeks</p>
        <p class="text6">6. GeeksforGeeks</p>
    </div>
</body>
</html>
                      
                       

Output: 

 



Next Article
How to use CSS variables with TailwindCSS ?
author
iamgaurav
Improve
Article Tags :
  • CSS
  • Technical Scripter
  • Web Technologies
  • CSS-Questions
  • SASS
  • Technical Scripter 2022

Similar Reads

  • Explain how to define a variable in SASS
    SASS is a short form of Syntactically Awesome Style Sheet which is an extension to Cascading StyleSheet (CSS). It is a preprocessor of CSS. It is compiled to CSS & is fully compatible with every version of CSS. Sass reduces the repetition of CSS and hence saves time. It allows using the variable
    7 min read
  • How to use a not:first-child Selector in CSS?
    The :not(:first-child) selector in CSS targets all elements that are not the first child within their parent. It allows you to style elements except the first one, making it useful for applying styles to subsequent items in a list or layout. Syntax:not( element ) { // CSS property } Example: In this
    2 min read
  • How to use Sass Variables with CSS3 Media Queries ?
    SASS Variables: SASS Variables are very simple to understand and use. SASS Variables assign a value to a name that begins with $ sign, and then just refer to that name instead of the value. Despite this, they are one of the most useful tools SASS provides. SASS Variables provide the following featur
    3 min read
  • How to use CSS variables with TailwindCSS ?
    Tailwind CSS  allows users to predefined classes instead of using the pure CSS properties.  We have to install the Tailwind CSS.  Create the main CSS file (Global.css) which will look like the below code. Global.css: In the following code, the entire body is wrapped into a single selector. The entir
    1 min read
  • How to define colors as variables in CSS ?
    In CSS, we can define custom properties (often known as CSS variables), that offers us great flexibility to define a set of rules and avoid rewriting them again and again. We can also use custom properties to define colors. Example 1: <!DOCTYPE html> <html> <head> <title> How
    2 min read
  • SASS Variable in CSS calc() function
    CSS is itself a good language for web designing but stylesheets are becoming more complex, larger, and even harder to maintain. That’s where a preprocessor like Sass comes in action and helps us to use features which don’t even exist in CSS. The calc() function is a function that takes a single expr
    2 min read
  • How to add lines besides a vertical text using SASS ?
    In this article, we will see how to add lines besides some vertical text using SASS. Approach: The HTML code is used to depict the basic structure of the body. Here, we define a division element with a class of wrapper to contain subsequent division elements. Another division element with a class of
    6 min read
  • SASS | Use variables across multiple files
    To use variables across multiple files in SASS is carried out by @import rule of SASS. @import rule which import Sass and CSS stylesheets for providing variables, @mixins and functions.Such that combine all stylesheets together for compiled css. It also imports URL such as frameworks like Bootstrap
    3 min read
  • How to Use Sass with CSS?
    SASS (Syntactically Awesome Style Sheets) is a pre-processor for CSS that provides various features such as nesting and mixins. SASS compiles to regular CSS and that CSS is used in your project and finally by the browser to style the web page. How SASS Works?Let's understand how Sass works: The SASS
    3 min read
  • What is a Mixin and how to use it in SASS ?
    In this article, we are going to explain and see the usage of Mixin. Mixin is used in Sass, which is a CSS preprocessor. Mixin works as a function in Sass and helps us to reduce the writing same code over and over. Syntax@mixin <name>(<parameter>){ // Code... } name: We have to provide t
    4 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