How to apply class conditionally in CSS ?
Last Updated :
25 Jul, 2024
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.
Similar Reads
How to conditionally apply CSS styles in AngularJS ? In AngularJS we can build the dynamic single-page application with interactive CSS embedded in it. But as usual, while developing the application, the CSS is once statically defined and used, we are not changing the CSS when the application is running. But, in AngularJS we can dynamically change the
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
5 min read
How to append class if condition is true in Haml? We will be looking at two ways of appending a class to an element depending on some condition's evaluation. Essentially, the logic behind both of them is the same. Using ternary operator Using the "unless" conditional check Initial code: First, let's write some basic HAML code. It will contain a sty
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
How to add a style on a condition in Tailwind CSS ? In this article, we will see how to integrate conditional styling into our web projects using Tailwind CSS. There are various Tailwind CSS classes are available, although, it usually involves dynamically adding or removing classes using JavaScript. There are different approaches to implementing the
3 min read