leancn-ui
Frontend
9 min read

Modern CSS Techniques for Better Performance

Explore advanced CSS techniques including container queries, CSS Grid, and modern layout methods that improve both developer experience and site performance.

Modern CSS Techniques for Better Performance

Modern CSS offers powerful features that can significantly improve both performance and developer experience. This guide covers the latest CSS techniques and best practices.

Container Queries

Container queries allow you to style elements based on their container's size, not just the viewport.

Benefits:

  • More flexible component styling
  • Better responsive design
  • Reduced JavaScript dependencies

Example:

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

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

CSS Grid Advanced Patterns

CSS Grid provides powerful layout capabilities beyond basic grids.

Subgrid

Subgrid allows nested grids to align with their parent grid, creating consistent layouts.

Auto-fit and Auto-fill

Create responsive grids without media queries:

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

Modern Layout Techniques

Logical Properties

Use logical properties for better internationalization and maintainability:

.element {
  margin-inline-start: 1rem;
  padding-block: 1rem;
}

Aspect Ratio

Maintain aspect ratios without padding hacks:

.image {
  aspect-ratio: 16 / 9;
  width: 100%;
}

Performance Optimizations

Content Visibility

Improve rendering performance for off-screen content:

.long-list {
  content-visibility: auto;
}

Will-Change

Hint to the browser about upcoming changes (use sparingly):

.animated {
  will-change: transform;
}

CSS Custom Properties

Use CSS variables for theming and dynamic values:

:root {
  --primary-color: #0066cc;
  --spacing-unit: 1rem;
}

.button {
  background-color: var(--primary-color);
  padding: calc(var(--spacing-unit) * 2);
}

Modern Selectors

:has() Selector

Style parent elements based on children:

.card:has(.featured) {
  border: 2px solid gold;
}

:is() and :where()

Simplify complex selectors:

:is(h1, h2, h3) {
  font-weight: bold;
}

Best Practices

Minimize Repaints and Reflows

  • Use transform and opacity for animations
  • Batch DOM reads and writes
  • Use contain property for isolation

Optimize Selectors

  • Keep selectors simple
  • Avoid deep nesting
  • Use class names over complex selectors

Use Modern Features

  • Leverage browser-native features
  • Reduce JavaScript dependencies
  • Take advantage of hardware acceleration

Tools and Resources

CSS Validators:

  • W3C CSS Validator
  • Stylelint

Performance Tools:

  • Chrome DevTools
  • Lighthouse
  • WebPageTest

Learning Resources:

  • MDN Web Docs
  • CSS-Tricks
  • Can I Use

Conclusion

Modern CSS provides powerful tools for creating performant, maintainable stylesheets. By leveraging these techniques, you can reduce JavaScript dependencies, improve performance, and create better user experiences.