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:
if/else condition in CSS
Next article icon

CSS Conditional Rules

Last Updated : 04 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

CSS Conditional Rules apply CSS styles only when certain conditions are met. So the condition here can be either true or false and based on the statements/style will get executed. These rules start with the @ symbol and are part of CSS at-rules.

The main conditional rules include:

  • @supports
  • @media
  • @document

1. CSS Supports Rule

The @supports rule in CSS allows you to apply styles only if the browser supports a specific CSS property or feature.

Syntax:

@supports ("condition") {
/* Style to apply */
}
html
<!--Driver Code Starts{--> <html> <head> <!--Driver Code Ends }-->      <style>         @supports (display: grid) {             section h1 {                 background-color: green;                 color: white;                 padding: 15px;             }         }     </style>  <!--Driver Code Starts{--> </head> <body>     <section>         <h1>GeeksforGeeks</h1>         <h3>A computer science portal for geeks</h3>     </section> </body> </html> <!--Driver Code Ends }--> 
  • The @supports rule checks if the browser supports the display: grid property.
  • If supported, it applies the specified styles to the <h1> element within the <section>, changing its background color to green, text color to white, and adding padding.

2. CSS Media Rule

The @media rule in CSS applies styles based on media queries, allowing for responsive design that adapts to different devices and screen sizes.

Syntax: 

@media screen and ("condition") {
/* Style to apply */
}
html
<!--Driver Code Starts{--> <html> <head> <!--Driver Code Ends }-->      <style>         @media screen and (max-width: 700px) {             section {                 background-color: green;                 color: white;                 padding: 15px;             }         }     </style>  <!--Driver Code Starts{--> </head> <body>     <section>         <h1>GeeksforGeeks</h1>         <h3>A computer science portal for geeks</h3>     </section> </body> </html> <!--Driver Code Ends }--> 
  •  The @media rule checks if the device’s screen width is 700 pixels or less.
  • If this condition is met, it applies the specified styles to the <section> element, changing its background color to green, text color to white, and adding padding.

3. CSS Document Rule

The @document rule in CSS is designed to apply styles to specific URLs or sets of URLs. However, it is currently experimental and primarily supported only in Firefox with the -moz- prefix. As a result, it is not recommended for cross-browser use.

Syntax: 

@-moz-document url("YOUR-URL") {
/* Style to apply */
}
html
<!--Driver Code Starts{--> <html> <head> <!--Driver Code Ends }-->      <style>         @-moz-document url("http://localhost/GfG/document-rule.html") {             section h1 {                 background-color: green;                 color: #fff;                 padding: 15px;             }         }     </style>  <!--Driver Code Starts{--> </head> <body>     <section>         <h1>GeeksforGeeks</h1>         <h3>A computer science portal for geeks</h3>     </section> </body> </html>  <!--Driver Code Ends }--> 
  • The @-moz-document rule targets the specific URL “http://localhost/GfG/document-rule.html”.
  • Within this URL, it applies styles to the <h1> element inside the <section>, setting a green background, white text color, and padding.

Note: The @document rule is not widely supported across browsers; currently, it is primarily implemented in Firefox using the @-moz-document prefix

Best Practices for Using CSS Conditional Rules

  • Use the @supports rule to apply styles only if the browser supports specific CSS features, ensuring fallback for unsupported browsers.
  • Leverage the @media rule for responsive design, targeting specific screen sizes or device characteristics.
  • Avoid over-reliance on experimental rules like @document, as they lack cross-browser support.


Next Article
if/else condition in CSS
author
aakashpawar1999
Improve
Article Tags :
  • CSS
  • Technical Scripter
  • Web Technologies
  • CSS-Properties
  • Web technologies

Similar Reads

  • if/else condition in CSS
    In CSS, traditional if/else conditions aren't directly available. Instead, conditional styling is managed through techniques like media queries, which apply styles based on screen size, and feature queries (@supports), which check for browser support of specific CSS features, allowing adaptable and
    3 min read
  • How to load css resources conditionally ?
    In the world of web development, CSS plays a crucial role in styling HTML code. Typically, the CSS code is linked in the head tag of an HTML document, allowing developers to easily apply styles to the content of a web page. However, there may be times when the desired styling is dependent on specifi
    6 min read
  • CSS At-Rules
    Sass supports every at-rule that is a part of the proper CSS. In order to stay flexible and future-compatible with the upcoming versions of CSS, Sass has given general support that covers almost every at-rules by itself. Syntax: @<rule> <value>, @<rule> { ... } OR @<rule>
    2 min read
  • React Conditional Rendering
    Conditional rendering allows dynamic control over which UI elements or content are displayed based on specific conditions. It is commonly used in programming to show or hide elements depending on user input, data states, or system status. This technique improves user experience by ensuring that only
    6 min read
  • R If Else Conditions
    The if-statement in Programming Language alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here comes the R Programming Language else statement. We can use the e
    5 min read
  • JavaScript - Conditional Statements
    JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition. 1. Using if StatementThe if statement is used to evaluate a particul
    4 min read
  • Conditional Statements in COBOL
    While writing a program a programmer needs to check for various conditions, if the condition is true then a particular block of statement/s are executed, or else another block of statement/s is execute. To check these conditions we use Conditional Statements. These statements return either true or f
    7 min read
  • CSS Naming Conventions
    CSS is foundational in web design, offering control over page appearance and interaction. While effective for style management, it's less ideal for complex projects, leading to the need for CSS naming conventions. These standards enhance stylesheet efficiency and organization, aiding team navigation
    3 min read
  • Conditional Inheritance in Python
    It happens most of the time that given a condition we need to decide whether a particular class should inherit a class or not, for example given a person, if he/she is eligible for an admission in a university only then they should be a student otherwise they should not be a student. Let's consider
    3 min read
  • Less.js @import At-Rules Inline
    Less.js (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is
    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