Master modern CSS techniques that will revolutionize your web development workflow. Learn how to create responsive, maintainable, and scalable styles using the latest CSS features. 🎨
Container Queries 📱
/* Base container styles */
.card-container {
container-type: inline-size;
container-name: card;
}
/* Responsive card layout */
.card {
display: grid;
gap: 1rem;
padding: 1rem;
background: var(--card-bg);
border-radius: var(--card-radius);
}
/* Container query breakpoints */
@container card (min-width: 400px) {
.card {
grid-template-columns: 200px 1fr;
}
.card__image {
aspect-ratio: 1;
object-fit: cover;
}
}
@container card (min-width: 600px) {
.card {
grid-template-columns: 300px 1fr;
gap: 2rem;
padding: 2rem;
}
.card__title {
font-size: clamp(1.5rem, 2.5vw, 2rem);
}
}
Cascade Layers 🎯
/* Define layer order */
@layer reset, base, components, utilities;
/* Reset layer */
@layer reset {
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Modern CSS reset */
img,
picture,
video,
canvas,
svg {
display: block;
max-width: 100%;
}
input,
button,
textarea,
select {
font: inherit;
}
}
/* Base layer */
@layer base {
:root {
/* Colors */
--color-primary: hsl(220 90% 56%);
--color-primary-light: hsl(220 90% 66%);
--color-primary-dark: hsl(220 90% 46%);
/* Typography */
--font-sans: 'Inter', system-ui, sans-serif;
--font-mono: 'JetBrains Mono', monospace;
/* Spacing */
--space-1: clamp(0.25rem, 0.5vw, 0.5rem);
--space-2: clamp(0.5rem, 1vw, 1rem);
--space-3: clamp(1rem, 2vw, 2rem);
--space-4: clamp(2rem, 4vw, 4rem);
/* Shadows */
--shadow-sm: 0 1px 2px hsl(0 0% 0% / 0.1);
--shadow-md: 0 4px 6px hsl(0 0% 0% / 0.1);
--shadow-lg: 0 10px 15px hsl(0 0% 0% / 0.1);
}
body {
font-family: var(--font-sans);
line-height: 1.5;
color: hsl(220 10% 10%);
}
}
/* Components layer */
@layer components {
.button {
display: inline-flex;
align-items: center;
gap: var(--space-1);
padding: var(--space-2) var(--space-3);
background: var(--color-primary);
color: white;
border: none;
border-radius: 0.5rem;
font-weight: 500;
transition: background 0.2s;
&:hover {
background: var(--color-primary-dark);
}
&:active {
transform: translateY(1px);
}
}
.button--outline {
background: transparent;
border: 2px solid var(--color-primary);
color: var(--color-primary);
&:hover {
background: var(--color-primary);
color: white;
}
}
}
/* Utilities layer */
@layer utilities {
.flow > * + * {
margin-top: var(--space-2);
}
.grid {
display: grid;
gap: var(--space-2);
}
.flex {
display: flex;
gap: var(--space-2);
}
}
Custom Properties and Functions 🎛️
/* Theme configuration */
:root {
/* Color system */
--hue-primary: 220;
--saturation-primary: 90%;
--lightness-primary: 56%;
--color-primary: hsl(
var(--hue-primary)
var(--saturation-primary)
var(--lightness-primary)
);
/* Generate color variations */
--color-primary-lighter: hsl(
var(--hue-primary)
var(--saturation-primary)
calc(var(--lightness-primary) + 10%)
);
--color-primary-darker: hsl(
var(--hue-primary)
var(--saturation-primary)
calc(var(--lightness-primary) - 10%)
);
/* Fluid typography */
--font-size-base: clamp(1rem, 1vw + 0.75rem, 1.25rem);
--font-size-lg: clamp(1.25rem, 1.5vw + 1rem, 1.5rem);
--font-size-xl: clamp(1.5rem, 2vw + 1.25rem, 2rem);
/* Fluid spacing */
--space-base: clamp(1rem, 2vw, 2rem);
--space-xs: calc(var(--space-base) * 0.25);
--space-sm: calc(var(--space-base) * 0.5);
--space-md: var(--space-base);
--space-lg: calc(var(--space-base) * 2);
--space-xl: calc(var(--space-base) * 4);
}
/* Dark theme */
@media (prefers-color-scheme: dark) {
:root {
--lightness-primary: 66%;
color-scheme: dark;
}
}
Modern Layout Techniques 📐
/* Grid layout with auto-fit */
.gallery {
display: grid;
grid-template-columns: repeat(
auto-fit,
minmax(min(250px, 100%), 1fr)
);
gap: var(--space-md);
}
/* Masonry layout */
.masonry {
columns: auto min(350px, 100%);
gap: var(--space-md);
& > * {
margin-bottom: var(--space-md);
break-inside: avoid;
}
}
/* Flexible sidebar layout */
.with-sidebar {
display: grid;
gap: var(--space-md);
@media (min-width: 768px) {
grid-template-columns: minmax(min-content, 200px) 1fr;
& > :first-child {
position: sticky;
top: var(--space-md);
align-self: start;
max-height: calc(100vh - var(--space-md) * 2);
overflow-y: auto;
}
}
}
/* Holy grail layout */
.holy-grail {
display: grid;
min-height: 100vh;
grid-template-rows: auto 1fr auto;
& > header,
& > footer {
padding: var(--space-md);
}
& > main {
display: grid;
grid-template-columns:
minmax(var(--space-lg), 1fr)
min(65ch, 100%)
minmax(var(--space-lg), 1fr);
& > * {
grid-column: 2;
}
& > .full-width {
grid-column: 1 / -1;
}
}
}
Advanced Animations 🎬
/* Keyframe animations */
@keyframes slide-up {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Animation utilities */
.animate {
animation-duration: 0.5s;
animation-fill-mode: both;
&.slide-up {
animation-name: slide-up;
}
&.delay-1 { animation-delay: 0.1s; }
&.delay-2 { animation-delay: 0.2s; }
&.delay-3 { animation-delay: 0.3s; }
}
/* Scroll-triggered animations */
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(40px);
}
}
.reveal {
opacity: 0;
@media (prefers-reduced-motion: no-preference) {
animation: fade-in 1s ease forwards;
animation-timeline: view();
animation-range: entry 20% cover 50%;
}
}
/* Interactive animations */
.card {
--lift: 0;
--rotate: 0;
transform:
translateY(calc(var(--lift) * -1px))
rotateY(calc(var(--rotate) * 1deg));
transition: transform 0.2s;
@media (hover: hover) {
&:hover {
--lift: 10;
}
}
}
/* Mouse position effect */
.follow-cursor {
--x: 50%;
--y: 50%;
background: radial-gradient(
circle at var(--x) var(--y),
hsl(220 90% 70%),
hsl(220 90% 50%)
);
}
Performance Optimizations ⚡
/* Content-visibility */
.card {
content-visibility: auto;
contain-intrinsic-size: 200px;
}
/* Image loading */
.image {
aspect-ratio: 16 / 9;
background: hsl(220 10% 90%);
@supports not (aspect-ratio: 16 / 9) {
&::before {
float: left;
padding-top: 56.25%;
content: "";
}
&::after {
display: block;
content: "";
clear: both;
}
}
}
/* Font loading */
@font-face {
font-family: "Inter";
src: url("/fonts/Inter.woff2") format("woff2");
font-display: swap;
}
/* Print styles */
@media print {
.no-print {
display: none;
}
a[href]::after {
content: " (" attr(href) ")";
}
}
Best Practices 📝
- Use logical properties
- Implement progressive enhancement
- Write maintainable selectors
- Optimize performance
- Follow BEM methodology
- Use modern layout techniques
- Implement proper theming
- Write accessible styles
- Optimize for print
- Follow CSS architecture
Additional Resources
Modern CSS has evolved significantly, providing powerful features for creating responsive, maintainable, and performant web applications. This guide covers the latest techniques and best practices for writing CSS in 2025.