Open In App

How to apply class conditionally in CSS ?

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In web development, the styling of elements on a page is a crucial aspect of creating a visually appealing and interactive website. The traditional method of styling elements involves applying the same styles to all elements of the same type. However, this approach can become limiting when creating dynamic and interactive pages. For instance, you might want to style an element differently based on its state, such as when a user hovers over it with a mouse, or based on the viewport size or type of device being used. This is where the concept of conditionally applying a class in CSS comes into play. By conditionally applying a class, you can select elements based on certain conditions and apply different styles to each condition. This allows you to create dynamic and interactive pages that respond to user interactions and viewport size changes.

The problem, then, is finding the best way to conditionally apply a class in CSS. For this, there are several methods available, including using JavaScript or jQuery, using CSS preprocessors such as Sass or Less, or using CSS itself. In this article, we will focus on the best way to conditionally apply a class using CSS.

Used Property: The two main properties used in conditionally applying a class in CSS are pseudo-classes and media queries.

  • Pseudo-classes: Pseudo-classes are used to select elements based on their state or position. They are represented by a colon (:) followed by the name of the pseudo-class, such as :hover or :active. By using pseudo-classes, you can apply styles to an element based on its state or position, such as changing the color of a link when the user hovers over it with a mouse. 

 

Example 1: The below example demonstrates the above approach. It uses :hover pseudo-class to conditionally apply a class in CSS. The :hover pseudo-class changes the background color of the boxes to yellow, red, and cyan when the mouse pointer is hovering over them. The default background is black.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100vw;
            height: 30vh;
            gap: 30px;
        }
  
        .box {
            width: 150px;
            height: 150px;
            background-color: black;
        }
  
        #yellow:hover {
            background-color: yellow;
        }
  
        #red:hover {
            background-color: red;
        }
  
        #cyan:hover {
            background-color: cyan;
        }
    </style>
</head>
  
<body>
    <h1 align="center" style="color:green">
        GeeksforGeeks
    </h1>
    <h3 align="center">
        Conditional application of class
    </h3>
      
    <div class="container">
        <div class="box" id="yellow"></div>
        <div class="box" id="red"></div>
        <div class="box" id="cyan"></div>
    </div>
</body>
  
</html>


Output:

 

  • Media Queries: Media queries are used to apply styles based on the viewport size or the type of device being used. They are represented by the @media keyword followed by a media query that specifies the viewport size or device type. By using media queries, you can apply styles to an element based on the viewport size or device type, such as adjusting the width of a container for larger screens or for smaller screens. 

If the max-width of the screen is:

  • Reduced to 1100px, the background color will turn yellow.
  • Reduced to 900px, the background color will turn cyan.
  • Desktop size, the background color will be white.

Example 2: This is another example that illustrates the best way to conditionally apply a class by implementing the media query to different device widths.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
  
    <style>
        * {
            margin: 0;
            padding: 0;
        }
  
        @media screen and (max-width: 1100px) {
            body {
                background-color: yellow;
            }
        }
  
        @media screen and (max-width: 900px) {
            body {
                background-color: cyan;
            }
        }
    </style>
</head>
  
<body>
    <h1 align="center" style="color:green;
        margin-top:50px;
        font-size:3rem;">
        GeeksforGeeks
    </h1>
    <h3 align="center" style="color:green;
              margin-top:50px;
              font-size:3rem;">
        Conditional application of class
    </h3>
</body>
  
</html>


Output:

 

Conclusion: By using these techniques, you can conditionally apply a class in CSS, making it easier to create dynamic and interactive web pages that respond to user interactions and viewport size changes.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads