Skip to main content

Modern CSS Techniques for 2024

Explore the latest CSS features including container queries, cascade layers, and advanced grid layouts.

10 min read
312 views58 likes12 comments
Modern CSS Techniques for 2024

Modern CSS Techniques for 2024

Explore the latest CSS features that are transforming how we build web layouts and interfaces.

Container Queries

Container queries allow you to style elements based on the size of their parent container, not just the viewport:

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

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

CSS Nesting

Native CSS nesting is now supported in all major browsers:

.card {
  background: white;
  border-radius: 8px;
  padding: 1rem;

  & .title {
    font-size: 1.25rem;
    font-weight: bold;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  @media (prefers-color-scheme: dark) {
    background: #1a1a1a;
  }
}

The :has() Selector

The parent selector we've been waiting for:

/* Style a card differently when it contains an image */
.card:has(img) {
  grid-template-rows: 200px 1fr;
}

/* Style form groups with invalid inputs */
.form-group:has(:invalid) {
  border-color: red;
}

Subgrid

Subgrid allows child elements to participate in the parent's grid:

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

.card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
}

View Transitions

Add smooth transitions between page navigations:

@view-transition {
  navigation: auto;
}

.hero-image {
  view-transition-name: hero;
}

Scroll-Driven Animations

Create animations that respond to scroll position:

@keyframes fade-in {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.card {
  animation: fade-in linear both;
  animation-timeline: view();
  animation-range: entry 0% entry 100%;
}

Conclusion

Modern CSS has evolved dramatically. Container queries, nesting, :has(), subgrid, view transitions, and scroll-driven animations give us powerful tools that previously required JavaScript. Embrace these features to write cleaner, more performant, and more maintainable stylesheets.

Share this article

Subscribe to our newsletter

Get the latest articles, tutorials, and insights delivered straight to your inbox. No spam, unsubscribe anytime.

Related Articles

Getting Started with Next.js 15

Getting Started with Next.js 15

Learn about the latest features in Next.js 15 including async components, improved performance, and new routing capabilities.

Read more
Authentication Best Practices

Authentication Best Practices

Implement secure authentication with JWT, OAuth, and modern security patterns for web applications.

Read more
Tailwind CSS: Beyond the Basics

Tailwind CSS: Beyond the Basics

Advanced Tailwind CSS techniques including custom plugins, dynamic classes, and performance optimization.

Read more

Comments (0)