Ferry M. Portfolio Ferry M. Portfolio

Ferry M. Portfolio Ferry M. Portfolio

Ferry M. Portfolio Ferry M. Portfolio

Portfolio

Ferry M.

Back to Blogs
Web Development

Modern CSS Techniques Every Developer Should Know in 2024

November 20, 2024
11 min read

Explore cutting-edge CSS features like container queries, cascade layers, and modern layout techniques that are changing web design.

Modern CSS Techniques Every Developer Should Know in 2024

CSS continues to evolve rapidly, introducing powerful new features that make styling more intuitive and maintainable. Let's explore the latest techniques every developer should master.

Container Queries

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

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

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

Cascade Layers

Organize your CSS with cascade layers for better specificity management:

@layer reset, base, components, utilities;

@layer components {
  .button {
    background: blue;
    color: white;
  }
}

@layer utilities {
  .text-center {
    text-align: center;
  }
}

Modern Layout with Subgrid

Subgrid allows nested grids to participate in their parent's grid:

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

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

CSS Nesting

Write more organized CSS with native nesting:

.card {
  background: white;
  border-radius: 8px;
  
  & .title {
    font-size: 1.5rem;
    font-weight: bold;
  }
  
  & .content {
    padding: 1rem;
    
    & p {
      margin-bottom: 1rem;
    }
  }
  
  &:hover {
    transform: translateY(-2px);
  }
}

Custom Properties with Property Registration

Define custom properties with types and default values:

@property --gradient-angle {
  syntax: '<angle>';
  initial-value: 0deg;
  inherits: false;
}

.gradient-button {
  background: linear-gradient(var(--gradient-angle), blue, purple);
  transition: --gradient-angle 0.3s ease;
}

.gradient-button:hover {
  --gradient-angle: 90deg;
}

Logical Properties

Use logical properties for better internationalization:

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

Advanced Selectors

Leverage new pseudo-selectors for better targeting:

.parent:has(.child) {
  background: lightblue;
}

.item:nth-child(3n+2) {
  color: red;
}

.item:nth-child(n+3):nth-child(-n+7) {
  font-weight: bold;
}

Color Functions

Use modern color functions for better color manipulation:

:root {
  --primary: oklch(0.7 0.15 200);
  --primary-light: oklch(from var(--primary) calc(l + 0.1) c h);
  --primary-dark: oklch(from var(--primary) calc(l - 0.1) c h);
}

.button {
  background: var(--primary);
  border: 1px solid var(--primary-dark);
}

.button:hover {
  background: var(--primary-light);
}

View Transitions API

Create smooth page transitions with the View Transitions API:

::view-transition-old(root) {
  animation: fade-out 0.3s ease-out;
}

::view-transition-new(root) {
  animation: fade-in 0.3s ease-in;
}

@keyframes fade-out {
  to { opacity: 0; }
}

@keyframes fade-in {
  from { opacity: 0; }
}

Performance Optimization

Content Visibility

Improve rendering performance with content-visibility:

.long-content {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px;
}

CSS Containment

Use containment to improve performance:

.widget {
  contain: layout style paint;
}

Browser Support Strategy

When using modern CSS features:

  1. Check browser support on caniuse.com
  2. Use progressive enhancement
  3. Provide fallbacks for critical features
  4. Use feature queries with supports
.grid {
  display: flex;
  flex-wrap: wrap;
}

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

Conclusion

Modern CSS offers powerful tools for creating better user experiences with cleaner, more maintainable code. Start incorporating these techniques into your projects and stay ahead of the curve in web development.

The key is to adopt these features gradually, ensuring backward compatibility while leveraging the power of modern CSS.