DailyDevDiet

logo - dailydevdiet

Learn. Build. Innovate. Elevate your coding skills with dailydevdiet!

New CSS Selectors in 2024: You Need To Know

new css selectors in 2024

Introduction

CSS stands for Cascading Style Sheet. It provides an easy way to find or select HTML elements you wish to style. Over the years, CSS has evolved to include powerful new selectors that significantly enhance the way developers write code.  In 2024, CSS has witnessed the introduction of several powerful new selectors like :has(), :is(), and :where() are revolutionizing how we approach layout, styling, and interactions.

This article delves into these new CSS selectors in 2024, providing detailed explanations and practical examples to help you harness their capabilities.

Understanding CSS Selectors

Before we dive into the new selectors, let’s recap the basics. A CSS selector is a pattern used to identify specific HTML elements to which styles will be applied. For example, you can use a class selector (.class-name) to target all elements with a specific class or an ID selector (#id-name) for a unique element.

Traditionally, CSS selectors allowed developers to target elements directly or through their relationships, such as sibling, child, or descendant selectors. However, the introduction of advanced selectors like :has() and :is() has expanded the capabilities of CSS, making it more dynamic and flexible.

New CSS Selectors in 2024

1. :has() Selector

The :has() selector is a game-changer. The :has() pseudo-class is a new addition to CSS allowing you to style elements based on their relationship to other elements within the DOM.

How It Works:

The :has() selector works by selecting elements that contain a specific child element or meet a particular criteria. For example, you can style a <div> if it contains an image, a certain number of children, or any other HTML element.

Example:

/* Style a div that contains an image */
div:has(img) {
    background-color: lightgray;
    border: 2px solid #333;
}

/* Highlight a section only if it contains more than 3 articles */
section:has(> article:nth-child(4)) {
    border: 1px solid green;
}

Use Cases for :has():

  1. Form Validation: Display error messages based on the presence of invalid fields:
form:has(input:invalid) {
    border: 2px solid red;
}
  1. Interactive Elements: Style a parent container when a specific child is hovered over or selecte:
nav:has(> a:hover) {
    background-color: #f8f9fa;
}

With :has(), developers can create complex layouts and interactions without relying heavily on JavaScript, resulting in faster load times and simpler code.

2. :is() Selector

The :is() Selector reduces the redundancy in CSS. It simplifies the process of writing multiple selector patterns by allowing you to combine them into a single rule which makes your code more concise and easier to read.

How It Works:

The :is() selector accepts a list of selectors and applies styles to any element that matches one or more of them. Instead of repeating styles for multiple elements, you can group them under one rule.

Example:

/* Apply the same style to multiple heading levels */
:is(h1, h2, h3) {
    font-family: Arial, sans-serif;
    color: #333;
}

Use Cases for :is():

  1. Universal Button Styling: Style all buttons, no matter their type or class, in a single rule:
:is(.btn-primary, .btn-secondary, .btn-danger) {
    padding: 10px 20px;
    border-radius: 5px;
}
  1. Consistent Form Inputs: Apply consistent styling to various types of form inputs without repetition:
:is(input[type="text"], input[type="email"], textarea) {
    width: 100%;
    padding: 10px;
}

By reducing redundancy, :is() makes your CSS more maintainable and easier to scale as your project grows.

3. :where() Selector

The :where() Selector acts as a low-specificity selector for flexible styling. It functions similarly to :is(), but with one key difference: it has zero specificity. This means that styles applied using :where() can easily be overridden by more specific rules, making it ideal for low-priority styling that won’t interfere with other CSS rules.

How It Works:

The :where() selector allows you to apply styles to multiple elements without adding specificity to the rule. This is particularly useful when you want to provide base styles that can be easily overridden by more specific rules later in your stylesheet.

Example:

/* Apply default margin to various headings, without increasing specificity */
:where(h1, h2, h3) {
    margin-bottom: 20px;
}

Use Cases for :where():

  1. Reset Styles: Apply reset styles across multiple elements without affecting specificity:
:where(h1, h2, h3, p) {
    margin: 0;
    padding: 0;
}
  1. Framework Base Styles: When building a CSS framework or component library, use :where() for base styles that users can easily override:
:where(.btn) {
    padding: 10px;
    font-size: 14px;
}

The :where()> selector ensures that your base styles are easily adaptable, giving developers more control over how styles cascade throughout their project.

Practical Examples of Using New CSS Selectors

Let’s look at a practical example below that combines the :has(), :is(), and :where() selectors to create a responsive navigation bar:

/* Style a nav element that has a submenu */
nav:has(> ul) {
    border-bottom: 2px solid #ccc;
}

/* Apply consistent styles to all list items, but make the active one stand out */
nav ul li:is(.active) {
    background-color: #007BFF;
    color: white;
}

/* Provide default styles for all links, with low specificity */
:where(nav a) {
    text-decoration: none;
    padding: 10px 15px;
    color: #333;
}

In this example:

  • :has() checks if the navigation bar contains a submenu and applies a border.
  • :is() simplifies targeting the active list item for styling.
  • :where() applies base link styles that can be overridden as needed.

Conclusion

The introduction of new CSS selectors like :has(), :is(), and :where() unlocks a new level of power and flexibility in web development. These selectors simplify complex styling scenarios, reduce the need for JavaScript, and make CSS code more maintainable. As you adopt these modern techniques in 2024, you’ll be able to create more dynamic, interactive, and responsive websites with cleaner, more efficient code.

By leveraging these new CSS selectors, you can streamline your workflow, enhance performance, and take your web development skills to the next level. Whether you’re building responsive layouts, validating forms, or styling interactive elements, the potential of these new CSS selectors is immense.

Related Articles:

CSS Units Explained: A Beginner’s Guide to px, rem, em, vh, vw, and Percentages

What is CSS Flexbox?: A Beginner’s Guide to Responsive Layouts

Top 10 CSS Libraries You Should Know As Web Developer

How to Center Element in div: A Complete Guide

Scroll to Top