close
The Wayback Machine - https://web.archive.org/web/20241006163939/https://www.geeksforgeeks.org/css-conditional-rules/
Open In App

CSS | Conditional Rules

Last Updated : 02 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS Conditional Rules are nothing but a feature of CSS in which the CSS style is applied based on a specific condition. So the condition here can be either true or false and based on the statements/style will get executed.
These rules eventually come under CSS at-rule as they start with an @.
The Conditional Rules are: 
 

  • @supports
  • @media
  • @document

@supports: The @supports conditional rule is to check the support of a specific CSS property on the browser and apply the styling based on that.
Syntax: 
 

@supports ("condition") {
   /*  Style to apply  */
}

Example: 
 

html




<!DOCTYPE html>
<html>
<head>
    <title>Supports Rule</title>
    <style>       
        @supports (display: grid) {
            section h1 {
                background-color: green;
                color: white;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <section>
        <h1>GeeksforGeeks</h1>
        <h3>A computer science portal for geeks</h3>
    </section>
</body>
</html>


Output: 
 

BERJAYA

In the above example, the browser is supported by the display property as a grid.
@media: The @media conditional rule is a rule which is used to apply the style based on the media queries. It can be used to check the device width and/or height and apply the style specified based on that.
Syntax: 
 

@media screen and ("condition") {
   /*  Style to apply  */
}

Example: 
 

html




<!DOCTYPE html>
<html>
<head>
    <title>Media Rule</title>
    <style>
        @media screen and (max-width: 700px) {
            section {
                background-color: green;
                color: white;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <section>
        <h1>GeeksforGeeks</h1>
        <h3>A computer science portal for geeks</h3>
    </section>
</body>
</html>


Output: 
Screen width more then 700px: 
 

BERJAYA

Screen width less then or equal to 700px: 
 

BERJAYA

In the above example, when the width of the browser more then 700px, the style is not applied but when the browser window is made smaller than 700px, then the style gets applied.
@document: The @document conditional rule is used to apply style for specified URL i.e. the styling will only be applied on the specified URL.
Syntax: 
 

@document url(“YOUR-URL”) {
   /*  Style to apply  */
}

It is experimental and works only on Firefox with -moz- prefix i.e. @-moz-document.
Example: 
 

html




<!DOCTYPE html>
<html>
<head>
    <title>Document Rule</title>
    <style>
        @-moz-document url("http://localhost/GfG/document-rule.html") {
            section h1 {
                background-color: green;
                color: #fff;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <section>
        <h1>GeeksforGeeks</h1>
        <h3>A computer science portal for geeks</h3>
    </section>
</body>
</html>


Output: 
 

BERJAYA

In the above example, the style gets applied when the URL specified is the one being visited.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
     


Similar Reads

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> <value> { ... } The rule must always be an iden
2 min read
What are the rules of margin collapse in CSS ?
In this article we will learn the rules of margin collapse in CSS, Margin collapsing is a behavior of CSS where the vertical margins of block-level elements are combined into one i.e. to the margin of the element with the largest value. Sometimes when we assigned margins to elements it does not work in the way we think it to and that creates confus
5 min read
Primer CSS Lint Rules (WIP)
Primer CSS is a free open-source CSS framework that is built with the GitHub design system to provide support to the broad spectrum of Github websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure that the patterns are steady and interoperable with every other. Its a
2 min read
How to add CSS Rules to the Stylesheet using JavaScript ?
In this example, we will see how to add CSS rules to the stylesheet element using JavaScript. First, we will create an HTML document and then add some CSS rules using JavaScript. Syntax: let styleText = document.getElementById('GFG').sheet; let st = `.box { width: 100px; height: 100px; }`; styleText.insertRule(st, 0);Approach: First, we select the
2 min read
Less.js @import At-Rules css
Less.js 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 makes CSS more powerful. LESS provides cross-browser compatibility. A programming language called CSS pre-processor is used to enhance CSS and compile it for use by web br
3 min read
What is at-rules and why use of "at" in CSS ?
In this tutorial, we will learn about At-rules & their usage in CSS. At-rules are simply some CSS statements that instruct the CSS how to behave in particular conditions. ie., At-rules allow developers to change the design or layout of websites based on certain conditions. Every At-rule command begins with '@' followed by an identifier and end
7 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read
Vue.js Conditional Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Conditional Rendering in Vue makes
2 min read
How to use conditional operator in jQuery a template?
In this article, we will learn to use ternary or conditional operator in jQuery. The Ternary or Conditional operator takes three operands, a condition followed by question mark followed by two expressions to execute with a semicolon (:) in between the two expressions. Syntax: condition ? expression1 : expression2 Example: Now let us try an example
1 min read
CoffeeScript Conditional Statements
The CoffeeScript programming language is used to write a simpler syntax that compiles into JavaScript before getting executed. In any programming language, conditions play an important role in the execution of code in a particular sequence. In this article, we will see various conditional Statements such as If, Else, Else If, etc. that CoffeeScript
5 min read
Angular Conditional Class with *ngClass
In this article, we will see the basic implementation of the Conditional class with *ngClass in Angular, along with understanding their implementation with the help of examples. Conditional class with *ngClassAngular's *ngClass directive allows us to apply CSS classes conditionally to an element. It provides a convenient way to dynamically toggle c
5 min read
AngularJS ngClass Conditional
The ngClass is a directive in AngularJS that facilitates to implementation of the CSS classes on an HTML element dynamically, ie, by adding and removing CSS classes on that specific element. We can use the ngClass directive to provide certain conditions, to add different classes to the elements. In this article, we will see the multiple ways of app
2 min read
What are inline conditional expressions in ReactJS ?
In React conditional rendering is the process of showing components based on a particular condition. With inline conditional expression in React, we can write the condition in a single line. For example, While building the to-do list app, developers should show a task only if any pending task is available otherwise they can show a message like "The
4 min read
Conditional rendering component using Enums in ReactJS
In certain scenarios, a ReactJS developer may have to dynamically display or hide components depending on specific conditions. For instance, when creating a To-Do list application, the developer must render tasks if there are pending items; otherwise, they should display a message such as "No pending tasks available." Prerequisites:NodeJS or NPMRea
3 min read
How to handle Conditional Requests/Branching in Postman ?
Conditional requests or branching in Postman allows you to create logic within your requests to handle different scenarios based on conditions. This feature is useful for executing specific actions or tests depending on the response received from an API endpoint. In this article, we'll explore how to handle conditional requests or branching in Post
3 min read
What is the purpose of the && operator in conditional rendering?
The JavaScript conditional rendering concept allows you to display content in a dynamic way, based on predetermined conditions. The logic AND operator is often used to simplify code and improve readability for conditional rendering. This article will examine the purpose of && operator conditional rendering, and how to apply conditional rend
3 min read
What is a Conditional Statement in PHP?
A conditional statement in PHP is a programming construct that allows you to execute different blocks of code based on whether a specified condition evaluates to true or false. It enables you to create dynamic and flexible code logic by controlling the flow of execution based on various conditions. Conditional statements in PHP include:PHP conditio
2 min read
How to use Conditional Types in TypeScript ?
The conditional types in TypeScript are created using a condition that will only be created based on the condition check and its return value. They implement the extends keyword to check the condition and create a type based on the returned true or false value. The conditional types are mainly used to create flexible and generic type definitions. I
1 min read
React Conditional Rendering
React allows us to conditionally render components which means that the developer can decide which component to render on the screen on on the basis of some predefined conditions. This is known as conditional rendering. Table of Content Implementing Conditional Rendering 1. Conditional Rendering using if-else Statement2. Conditional Rendering using
5 min read
How to create conditional types in TypeScript ?
Conditional types in TypeScript enable defining types based on conditions, similar to conditional statements in code. They determine different types of values, making functions adaptable to various input types, and enhancing code flexibility and maintainability. Syntax: We can create conditional types with the used of the ternary operator and exten
3 min read
How to Implement a Generic Function with a Conditional Return Type ?
Implementing a generic function with a conditional return type involves creating a function that can accept multiple data types and return different types based on specified conditions. This allows for flexible behavior depending on inputs. The function's return type may vary dynamically, enhancing its versatility and utility in handling diverse sc
4 min read
Use of the Ternary Operator in Conditional Rendering.
In React, conditional rendering helps show different things based on certain conditions. The ternary operator is a concise way to do this quickly and clearly. In this article, we will learn step by step process to use the ternary operator for conditional rendering in React. Syntax: React developers have multiple approaches for implementing conditio
3 min read
How to use conditional statements with EJS Template Engine ?
Conditional statements in EJS templates allow you to dynamically control the content that is rendered based on specified conditions. Similar to JavaScript, EJS provides a syntax for using conditional logic within your HTML templates. In this article, we will see a practical example of using conditional statements with the EJS template. What are Con
3 min read
Web Search Bar Implementation Using Javascript Conditional Flow
Given a list of items and the task is to filter through the items and return the best match using the search bar. There are several approaches to implement this but, we will be using a simple if-else statement to implement our search bar. Approach:Create a folder called gfgSearchBar.Open the folder in your desired IDE or IDLE.Create a html file cal
2 min read
How to implement Conditional Rendering in React?
This article explores JavaScript's conditional rendering in React JS, including various methods, syntax, and applications, leveraging React's virtual DOM, reusable components, props, and rapid rendering capabilities in single-page UI applications. We will use the following approaches to implement conditional rendering in React Table of Content if-e
4 min read
Why do Empty JavaScript Arrays Evaluate to True in Conditional Structures?
In JavaScript, there are situations when we need to evaluate whether a variable is "truthy" or "falsy" within conditional structures like if statements. While some values, like 0, null, undefined, and "" (empty string), are considered falsy and that should be the case, but empty arrays do not follow such instead they evaluate to true. The following
2 min read
How to Define an Array with Conditional Elements in JavaScript?
Sometime we may need to define an array with elements that depend on certain conditions. Such situation may occur when we have to dynamically generate arrays based on runtime logic such as including or excluding elements based on specific criteria. In this article, we'll explore various ways to define arrays with conditional elements in JavaScript.
2 min read
Conditional Statements in JavaScript
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. There are several methods that can be used to perform Conditional Statements in JavaScript. Conditional StatementDescr
6 min read
TypeScript Conditional Types
TypeScript's Conditional Types enable types to be defined based on a condition. Using the syntax T extends U ? X: Y, the type evaluates to X if T extends U; otherwise, it evaluates to Y. This feature allows for more flexible and dynamic type definitions. Syntaxtype ConditionalType<T> = T extends Condition ? value 1: value 2;Where- Conditional
6 min read
Laravel | Validation Rules
Validating the data coming from the user’s end is very important as it can help the user in filling proper data and prevent them from submitting any improper or malicious request. In Laravel, there are Validation Rules which are predefined rules, which when used in Laravel application. There is a list of rules that can be used to validate the data
5 min read