The Future of CSS

November 15, 2024 • CSS

CSS is evolving faster than ever. Features that once required preprocessors like Sass or complex JavaScript workarounds are now becoming native to the web platform. Let's look at what's reshaping the way we style the web.

Native Nesting

One of the most requested features is finally here. You can now nest CSS rules directly in your stylesheets, making them cleaner and easier to read.

.card {
  background: white;
  
  & .title {
    color: black;
  }
  
  &:hover {
    background: #f9f9f9;
  }
}

Container Queries

Media queries allow us to style based on the viewport size. Container Queries allow us to style elements based on the size of their parent container. This is a game-changer for component-based design, where a component might be used in a wide sidebar or a narrow card.

@container (min-width: 400px) {
  .card-content {
    display: grid;
    grid-template-columns: 2fr 1fr;
  }
}

CSS Layers (@layer)

Managing specificity has always been a pain. @layer allows you to define explicit layers of specificity (e.g., reset, framework, utilities). Styles in a higher layer win, regardless of selector specificity within the layers.

Conclusion

These features are just the tip of the iceberg. With new color functions like color-mix(), viewport units like dvh, and the :has() pseudo-class, CSS is becoming a full-fledged styling language that requires fewer external tools.