Open In App

if/else condition in CSS

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 responsive design.

Method 1: Using Classes for Conditional Styling

This method involves using classes in the HTML file to apply conditional styling. You define different class names according to the conditions you want to implement in CSS.

  • Suppose we want to change the color of text according to the line number then the if-else condition will be:
if(line1){
   color : red;
}else{
   color : green;
}
  • By using the above-discussed method, we will create classes and then apply CSS in it:
.color-line1{
   color : red;
}
.color-line2{
   color : green;
}

So, the above classes will execute only for HTML tags in which these classes are used.

Example: In this example we applies conditional styling using CSS classes. The first line is styled in red with .color-line1, and the second line in green with .color-line2. It showcases basic CSS styling.

html
<!DOCTYPE html>

<html>

<head>
    <title>
        If-else condition in CSS
    </title>

    <!-- Applying CSS -->
    <style>
        /* First line CSS */
        .color-line1 {
            color: red;
        }

        /* Second line CSS */
        .color-line2 {
            color: green;
        }
    </style>
</head>

<body style="text-align:center;">

    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        If-else condition in CSS
    </h3>
    <span class="color-line1">This is first line</span>
    <br><br>
    <span class="color-line2">This is second line</span>

</body>

</html>

Output:

Method 2: Using SASS for Conditional Logic

CSS preprocessors like SASS allow you to write conditional statements within your stylesheets. Although SASS conditions (e.g., @if, @else) are processed at compile time, not at runtime, they offer more flexibility and control in styling.

Syntax:

$type: line;
p {
  @if $type == line1 {
    color: blue;
  } @else if $type == line2 {
    color: red;
  } @else if $type == line3 {
    color: green;
  } @else {
    color: black;
  }
}

Explanation: In this example, the $type variable is checked against different conditions, and the appropriate styles are applied based on the matched condition. This approach allows for more dynamic and maintainable styling rules.

To know more about SASS click here
To read about if-else in SASS click here
Supported Browsers:

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

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.


Next Article

Similar Reads