Back to Blog

Modern CSS Techniques

Discover the latest CSS features and how to use them effectively in your projects. Grid, Flexbox, and custom properties explained.

CSS Design Frontend

Modern CSS Techniques

CSS has evolved significantly in recent years. Let’s explore modern features that make styling easier and more powerful.

CSS Grid

Grid is perfect for two-dimensional layouts:

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

Key Benefits

  • Powerful layout control
  • Responsive without media queries
  • Semantic markup

Flexbox

Great for one-dimensional layouts:

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

Custom Properties (CSS Variables)

Create maintainable, dynamic styles:

:root {
  --color-primary: #14b8a6;
  --spacing-unit: 1rem;
  --font-heading: 'Inter', sans-serif;
}

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

Benefits

  • Easy theming
  • Dynamic updates with JavaScript
  • Better maintainability

Container Queries

Style components based on container size:

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

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

Modern Selectors

:has() Selector

Parent selection is now possible:

/* Style form when input is focused */
form:has(input:focus) {
  border-color: blue;
}

:is() and :where()

Simplify complex selectors:

/* Instead of: */
h1, h2, h3 {
  font-weight: bold;
}

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

Logical Properties

Write direction-agnostic CSS:

.card {
  margin-block: 1rem;  /* top and bottom */
  padding-inline: 2rem; /* left and right */
}

CSS Nesting

Native nesting support (in modern browsers):

.card {
  padding: 1rem;

  & h2 {
    font-size: 1.5rem;
  }

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

Best Practices

  1. Use CSS Grid and Flexbox instead of floats and positioning
  2. Leverage custom properties for theming and consistency
  3. Write mobile-first CSS with min-width media queries
  4. Use logical properties for better internationalization
  5. Keep selectors simple and maintainable

Browser Support

Most modern CSS features have excellent browser support. Always check:

  • caniuse.com
  • Use progressive enhancement
  • Provide fallbacks when needed

Conclusion

Modern CSS is more powerful than ever. These features enable us to write cleaner, more maintainable code while creating beautiful, responsive designs.

Experiment with these techniques in your next project!