close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than whatโ€™s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

CSS in 2024: Emerging Trends

CSS in 2024: Emerging Trends
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

๐ŸŽฏ

Explore the emerging trends in CSS for 2024, including container queries, new color systems, CSS variable enhancements, subgrid, variable fonts, :has() and :is() selectors, CSS nesting, cascade layers, modern frameworks, and the :focus-visible pseudo-class.

In 2024, CSS is evolving with new features and trends that aim to make web design more flexible, responsive, and user-friendly. Here's a quick overview of what to expect:

  • Container Queries: A game-changer for responsive design, allowing components to adapt based on their own size.
  • New Color Systems (LCH, LAB, HWB): Offering more control and options for vibrant designs.
  • CSS Variables Enhancements: For dynamic theming and easier design system integration.
  • Subgrid: Making CSS Grid layouts even more powerful and easier to manage.
  • Variable Fonts: Allowing for more responsive and efficient typography.
  • :has() and :is() Selectors: Simplifying complex CSS selections.
  • CSS Nesting: For cleaner and more organized code structures.
  • Cascade Layers: Providing more control over style sheet organization.
  • Modern Frameworks and Tools: Such as Tailwind CSS, StyleX, and Chakra UI, to speed up development and ensure consistency.
  • :focus-visible Pseudo-Class: Enhancing accessibility for keyboard navigation.

These updates are set to make CSS more powerful and user-friendly, enabling designers and developers to create better, more responsive, and personalized websites with ease.

Key Benefits

  • More reusable components: Since these pieces adjust based on where they are placed, you can use the same design in many spots without redoing your work.
  • Greater flexibility: With container queries, your website can change its look not just when the whole screen size changes, but also based on smaller sections, giving you more control.
  • Easier to maintain: When each part handles its own changes, it's simpler to keep your website looking good without messing with the rest.

Browser Support

Right now, container queries are something new and not all web browsers can use them yet. Some browsers like Safari Technology Preview and Chrome Canary are testing them out. But don't worry, there are tools like CQFill and the @supports rule that let you start using these cool features even if some browsers aren't ready.

Usage Example

Here's a quick look at how container queries work:

.card {
  container-type: inline-size;
  container-name: card;
}

@container card (max-width: 30em) {
  .card-title { 
    font-size: 2rem;
  }
} 

This code makes the text size in a card change based on the card's width. It's a neat way to make sure your website looks good in different places.

Container queries are set to make web design more about fitting into different spaces rather than just changing with the screen size. They're all about making things simpler and more flexible.

The Evolution of CSS Variables

CSS Variables, also known as CSS Custom Properties, are a way for web developers to store certain values, like colors or fonts, and use them over and over in their websites. Think of them as a shortcut for keeping your website's look consistent without having to repeat yourself. Since they came around in 2015, CSS Variables have become a big deal for making websites. Looking into 2024, here's how they might change and get even better.

More Dynamic Theming

CSS Variables make it easier to change how a website looks by tweaking a few settings. In the future, websites might use these variables to let users change the website's theme themselves, like adjusting colors or font sizes, with just a few clicks. This means websites can offer a more personal touch without needing a complete redesign.

Design System Integration

CSS Variables fit really well into design systems, which are like big collections of design choices that make it easier to keep websites looking good. By using standard names for variables, like --color-brand-primary, it's easier to make sure everything matches up across different parts of a website. As more people use design systems, CSS Variables will become even more important.

Interactivity and Animations

CSS Variables can also make websites more interactive and fun. They can be used to change styles on the fly, which can make websites respond to user actions in cool ways, like changing themes or creating animations. This makes websites feel more alive and engaging.

Browser Support Improvements

Right now, most web browsers support CSS Variables, but not all of them. As we move into 2024, we can expect better tools to make sure CSS Variables work everywhere, even in older browsers. This means more websites can use them without worrying about leaving some users out.

CSS Variables are set to become even more useful as web developers find new ways to use them for dynamic styling, making websites more personal, and ensuring they look great everywhere. It's an exciting time for CSS Variables, and they're definitely something to watch.

Enhanced Color Palettes and Functions

CSS in 2024 will let web designers use a wider range of colors with new color systems like HWB, LAB, and LCH. These systems give you more ways to pick colors than the usual RGB method. Also, CSS will have smart functions like clamp(), min(), and max() to make handling color values easier.

New Color Models

We're getting some new color systems that fix some issues with RGB and give you more control:

  • HWB - Lets you choose colors by Hue (the type of color), Whiteness (how much white), and Blackness (how much black). This makes it easy to lighten or darken a color.
  • LAB - This system is based on how we see colors. It can show a wide range of colors that we can tell apart.
  • LCH - Similar to LAB but uses Lightness, Chroma (how vivid it is), and Hue. It's good for making smooth color transitions.

These new systems are starting to be supported by some web browsers, offering more options for the future.

Handy Color Functions

CSS is getting some functions that make color work simpler:

  • clamp() - Keeps a value within a set range. Handy for controlling how bright or dark a color is.
  • min() and max() - Choose the smallest or largest value from a set of colors. Useful for setting color limits.

Here's how you might use them:

background: clamp(0, var(--brand-grey), 15%); 
color: max(white, var(--text-color), #eee);

These tools make it easier to adjust colors without a lot of work.

Overall, the new color features coming to CSS mean designers will have more ways to create vibrant, eye-catching designs. As web browsers get better at supporting these features, the @supports rule can help make sure your site looks great for everyone.

CSS Grid and Subgrid Advances

CSS Grid has really changed how we make websites look good and work well on all kinds of devices, from phones to big computer screens. Almost every new website now uses CSS Grid because it's supported by all the main web browsers. Looking into 2024, CSS Grid is about to get even better with something called Subgrid.

What is Subgrid?

Subgrid is a cool addition to CSS Grid that lets the smaller grids inside a bigger grid follow the same layout as the big one. This means you can make more complicated designs more easily, without having to set up the layout for each part separately.

For instance, if your main page layout has 12 columns, any smaller grid inside it will automatically have 12 columns too. You won't have to tell each one how to behave at different screen sizes; they just know.

Key Benefits of Using Subgrid

Subgrid makes designing with grids easier in a few key ways:

  • Consistency across breakpoints - Your designs stay the same across different screen sizes without extra work.
  • Flexible nesting - You can have grids within grids and they all work together nicely.
  • Granular control - You can tweak the smaller grids without messing with the big picture.
  • Easier maintenance - It's simpler to manage your layout with one set of rules for the whole thing.

Here's a basic example of how Subgrid works:

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
}

.subgrid {   
  display: grid; 
  grid-column: span 6; /* Takes up 6 columns of the parent grid */
}

This .subgrid automatically gets the 12-column setup from its parent, making your job a lot easier.

Current Subgrid Browser Support

Right now, not all browsers can use Subgrid, but the big ones like Chrome and Firefox are starting to. There are some tricks, like polyfills, that let you use some of its features even in browsers that don't fully support it yet.

As more browsers start to support Subgrid by 2024, it's going to be a big deal for making websites. It lets you create complex and flexible designs much more easily, and when you throw in other smart grid features, you can do a lot with less hassle.

Variable Fonts and Typography Innovations

Variable fonts are a smarter type of font that lets you have lots of styles from just one font file. This means you don't need separate files for each style like bold or thin. Looking into 2024, variable fonts could make text on websites look better and work better in many ways.

More Fluid and Responsive Type

The cool thing about variable fonts is they can change smoothly between styles. For example, text can go from thin to thick style or from straight to slanted when you hover over it. This makes text adjust better based on the screen size, what the device can do, and what the user prefers.

Here's a simple example:

h1 {
  font-variation-settings: 'wght' 400, 'ital' 0deg;
  transition: font-variation-settings 0.5s;
}

h1:hover {
   font-variation-settings: 'wght' 900, 'ital' 10deg; 
}

As the screen size changes, the text style can change too, making it easier to read.

Advanced Layout Control

Variable fonts give you more control over how text looks. You can adjust how wide or heavy text is, or how slanted it is. This helps make websites look good on all screen sizes.

On smaller screens, text can be made narrower to fit better, then wider on bigger screens. This helps make reading easier and nicer.

File Size Optimizations

Since variable fonts combine many styles in one file, websites can load faster because they don't need to load as many font files. This saves time and makes websites quicker to load.

These fonts are made smaller with special formats for the web, which helps keep websites fast and simple.

Looking Ahead

Variable fonts are still new, but as more people start using them, we'll see even cooler ways to use text on websites. Better support from web browsers will make these fonts work for more people. So, the future of how text looks and works on websites is getting better with variable fonts!

The Emergence of CSS :has() and :is() Selectors

The :has() and :is() pseudo-classes are new tools in CSS that help us target elements on a webpage more easily. As more web browsers start to support these features in 2024, they're going to make writing CSS simpler.

How :has() Works

The :has() pseudo-class lets you pick out parent elements that have certain elements inside them. This is a game-changer because it means you don't have to write long, complicated selectors to target those parents.

Here's a straightforward example:

/* Select article elements that have an element with class 'highlight' inside */
article:has(.highlight) {
  border: 1px solid #fee599; 
}

Before :has(), you'd have to write it in a more roundabout way, which could get messy.

Use Cases for :has()

Some cool things you can do with the :has() pseudo-class include:

  • Making parent containers look different if they have certain types of content inside
  • Showing or hiding elements based on what's inside them
  • Changing how things are laid out if specific content is present

This makes it easier to do stuff that used to need extra JavaScript.

How :is() Works

The :is() pseudo-class lets you apply the same CSS rules to different elements at the same time. You just list the elements inside the parentheses, separated by commas.

For example:

/* Make buttons and custom inputs look the same */
:is(button, .custom-input) {
  border-radius: 4px;
  padding: 8px 12px; 
}

This saves time by letting you write one rule for multiple things.

Use Cases for :is()

The :is() pseudo-class is great for:

  • Applying the same styles to different elements without repeating yourself
  • Avoiding tricky issues with too-specific selectors
  • Cutting down the amount of CSS you have to write

Together, :has() and :is() are going to be really handy for making websites with less hassle. They help you write CSS in a simpler, more straightforward way, which is awesome as browsers start to support them more.

sbb-itb-bfaad5b

CSS Nesting for Cleaner Code

Nesting

CSS nesting lets you put CSS rules inside each other, kind of like how you can put boxes inside bigger boxes. This helps make your CSS code neater, easier to read, and you don't have to write the same thing over and over again.

Key Benefits

  • Eliminates repetition - You don't have to write the same parent selector over and over. This cuts down on repeating yourself.
  • Better structure - By nesting, you group related rules together under their main rule. This organizes your code better.
  • Improved readability - Because everything is grouped and there's less repetition, it's easier to quickly see what's going on in your code.
  • Less prone to errors - With less repetition, there's less chance you'll make a mistake.
  • Better maintainability - If you need to change a selector, you only have to do it once, and it updates everywhere it's nested.

Browser Support

CSS nesting works in Chrome, Firefox, and Safari Technology Preview right now. There's also a tool called postcss-nested that lets you use nesting in other browsers. As more browsers start to support it, using nesting will get even easier.

Usage Example

.card {
  padding: 20px;

  &:hover {
    box-shadow: 0 0 10px rgba(0,0,0,.1); 
  }
  
  .title {
    font-size: 1.3rem;
  }
}

In this example, .title is inside .card, so you don't have to repeat .card when writing .title. The & symbol stands for the parent selector.

Nesting makes your CSS tidier and easier to manage by cutting down on repetition. With browsers getting better at supporting it, it's becoming a great tool for making your code easier to handle. Plus, it helps everyone work together more smoothly when dealing with stylesheets.

The Importance of Cascade Layers

Cascade layers are a way for people who build websites to sort their CSS styles into different groups. This helps them have more control over which styles show up first and makes it easier to manage styles as projects get bigger.

How Cascade Layers Work

You can use the @layer rule to name and order your layers like this:

@layer utilities, components;

Then, you can assign your stylesheets to these layers:

@import "utilities.css" layer(utilities);
@import "buttons.css" layer(components); 

The order you put the layers in decides which styles get used first. Styles in layers that come later can override the ones before them.

Benefits of Cascade Layer Usage

Easier to keep things organized - You can group your styles in a way that makes sense, which helps when you have to go back and change things.

More control - Instead of trying to figure out which style wins based on complicated rules, you can just decide which layer goes on top.

Less need for !important - Since you can control which style comes first more easily, you don't have to use !important as much to make sure your style is used.

Working together is smoother - Teams can decide how to organize their layers in a way that works best for them.

Browser Support and Fallbacks

Most browsers can handle cascade layers, but there are ways to make sure your site works for everyone:

  • You can order your @import statements as a simple backup plan
  • Use tools like postcss-cascade-layers to make older browsers understand layers
  • Check if browsers support layers with @supports and only use them if they do

Example Usage

/* Core styles */
@import "reset.css"; 

/* Layers */
@layer utilities, components, layout;

/* Stylesheets */  
@import "utilities.css" layer(utilities);
@import "buttons.css" layer(components);
@import "grid.css" layer(layout); 

In this setup, the utilities layer goes over the basic reset styles, components goes over utilities, and so on.

Cascade layers make it easier to manage your styles, especially when you have a lot of them. Even though not all browsers might support them yet, there are ways to work around that.

Modern Frameworks and Tools

Frameworks and tools like Tailwind CSS, StyleX, and Chakra UI are becoming more popular with web developers. These tools are great for building websites faster and making sure they look good on all devices.

Tailwind CSS

Tailwind CSS is all about giving you ready-to-use classes for things like spacing, colors, and fonts. It's designed to help you build interfaces quickly without writing a lot of custom CSS.

Key Features:

  • Ready-made classes for quick design changes
  • Easy to customize
  • Works well with different screen sizes
  • Automatically removes unused styles to keep websites fast
  • Can be used with popular frameworks like React

Here's an example of using Tailwind to make a button:

<button 
  class="bg-blue-500 text-white font-bold py-2 px-4 rounded"
>
  Button
</button>

Some people might find the class names a bit long, but it helps avoid problems with CSS conflicts.

StyleX

StyleX is about setting goals for how you want things to look and letting the tool figure out the best way to do it. It's really good for making sure things line up right and look good on any device without a lot of manual tweaking.

Key Features:

  • You tell it what you want, and it figures out how
  • Makes things line up without needing to set breakpoints
  • Works with CSS variables

For example, to center something, you would just say:

centerVertical()
centerHorizontal() 

StyleX is all about making your life easier by handling the tricky parts of layout and spacing for you.

Chakra UI

Chakra UI is a set of tools for React developers that makes building websites faster and ensures they are easy to use. It's especially focused on making sure websites are accessible to everyone.

Key Features:

  • Focuses on accessibility
  • Lets you easily change the look of components
  • Comes with a bunch of ready-to-use components
  • Includes hooks for managing state
  • Doesn't make your website slow

Here's how you might customize a button in Chakra UI:

<Button 
  colorScheme="purple"
  variant="outline"
>
  Click Me  
</Button>

These tools are all about making web development easier and faster, especially when you're working with components. They fit well with the way modern websites are built.

The :focus-visible Pseudo Class

The :focus-visible pseudo class is a cool feature that makes websites easier to use for people who navigate with a keyboard. It lets you add special styles to the element you're focused on when you use the keyboard to get there. This is great because it makes it super clear which part of the website you're interacting with, without confusing mouse users.

As we move into 2024, :focus-visible is expected to be used a lot more because it's really good at telling apart keyboard focus from mouse focus. This is especially helpful for keyboard users who might get mixed up when focus styles appear only because of mouse clicks.

Key Benefits

  • Enhanced accessibility: Makes it easier for keyboard users to see where they are on the site, which is important for making websites that everyone can use.

  • Better user experience: Stops confusion by showing focus styles only when they make sense for how you're browsing.

  • Wide browser support: Most new browsers can use this, so lots of people will benefit.

  • Easy to implement: It's a simple bit of CSS that doesn't take much work to add to your site.

Usage

Here's how to use :focus-visible to make sure keyboard users get the focus styling they need:

button:focus {
  /* Style for all focus */
}

button:focus-visible {
  /* Style just for keyboard focus */  
}

This helps separate the focus styles based on how you're browsing, making things clearer.

Looking Ahead

As :focus-visible gets more popular in browsers and websites throughout 2024, it's going to be key for making sites that are easy and fair to use for everyone. By making it clear which element has focus, it fixes a big problem and makes the web better for keyboard users.

Conclusion

Looking into the future of CSS in 2024, it's exciting to see how many new ways we'll have to make websites look and work better. From making websites that adjust better to different screen sizes with container queries, to using new color methods like LAB and LCH, to the cool things we can do with variable fonts, CSS is getting more powerful and easier to use.

But with all these new features, we have to remember to keep websites easy for everyone to use and work well on any device or browser. Luckily, we have tools and methods, like nesting and the @supports rule, that help us use new CSS features while making sure everyone can still visit our sites.

By using the latest CSS tricks along with good old-fashioned care for making websites accessible and easy to read, we can build sites that look amazing and are great for everyone. CSS is always getting better, and while we might never make the perfect website, it's fun to keep trying as CSS keeps improving.

The internet and websites are always changing, and so are the ways we build them. Even after 25 years, there's still a lot more we can do with CSS. Let's keep learning and trying new things as we make the web a better place for everyone in 2024 and beyond.

What is new in CSS 2024?

In 2024, CSS Grid Subgrid is a cool new feature that lets you put a grid inside another grid. This means the inside grid will automatically use the same settings as the outside grid, making it easier to keep everything aligned without extra work.

Which CSS framework should I learn in 2024?

Here are some CSS frameworks worth checking out in 2024:

  • Chakra UI: Great for making sure your site is accessible and works well for everyone.
  • Bulma: Easy to use, based on Flexbox, and lets you pick the parts you need.
  • Emotion: A lightweight way to style your components right in your JavaScript code.
  • Foundation: Offers a flexible grid system and lots of customizable parts.

Choose a framework that fits what you need, whether it's making your site accessible, easy to customize, or fast.

What is CSS and its features?

CSS stands for Cascading Style Sheets and it's what makes websites look good. It lets you:

  • Change text styles like color, size, and font.
  • Arrange things on your page with tools like positioning, grids, and flexbox.
  • Add cool visual effects with transforms, transitions, and animations.
  • Make your site work on all devices with responsive design.
  • Use the same styles across your site with variables and functions.

CSS is all about controlling how your website looks and feels.

What is new in CSS?

Some of the newest things in CSS include:

  • Nesting rules, which let you group styles together more neatly.
  • Container queries that help parts of your site look good no matter where they are.
  • New color functions like lab() and lch() for more color options.
  • The Subgrid feature for easier layout inside grids.
  • Custom properties, so you can reuse styles easily.
  • Handy layout tools like gap and place-content for spacing things out.

These updates make CSS more flexible and powerful for designing websites.

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more