10 Modern CSS Layout Techniques Every Frontend Developer Must Know in 2026
CSS layout has evolved beyond media queries and flexbox.
Here are 10 modern techniques with real-world examples, browser support, and when to use each.
1. Container Queries: Component-Level Responsiveness
Media queries respond to viewport size. Container queries respond to parent container size.
The Problem Media Queries Can't Solve
/* Media query: responds to viewport */
@media (max-width: 768px) {
.card {
flex-direction: column;
}
}Issue: What if the card is in a sidebar at 300px wide, even on a 1920px screen?
Container Queries to the Rescue
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
.card {
display: flex;
gap: 1rem;
}
@container sidebar (max-width: 400px) {
.card {
flex-direction: column;
}
}Result: Card responds to its container, not the viewport.
Real-World Example: Product Card
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
container-type: inline-size;
}
.product-card {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 1rem;
}
@container (max-width: 350px) {
.product-card {
grid-template-columns: 1fr;
}
.product-card img {
width: 100%;
}
}Browser Support: Chrome 105+, Safari 16+, Firefox 110+
2. The :has() Selector: Parent Selection
CSS finally has a parent selector.
Select Parent Based on Child
/* Style card differently if it has an image */
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
}
/* Style card without image */
.card:not(:has(img)) {
padding: 2rem;
text-align: center;
}Form Validation Styling
/* Style label when input is invalid */
label:has(+ input:invalid) {
color: red;
}
/* Style form when any field is invalid */
form:has(input:invalid) .submit-button {
opacity: 0.5;
pointer-events: none;
}Interactive States
/* Change header when any navigation item is hovered */
header:has(.nav-item:hover) {
background: rgba(0, 0, 0, 0.95);
}
/* Different layout when sidebar is expanded */
.layout:has(.sidebar.expanded) {
grid-template-columns: 300px 1fr;
}
.layout:has(.sidebar:not(.expanded)) {
grid-template-columns: 60px 1fr;
}Browser Support: Chrome 105+, Safari 15.4+, Firefox 121+
3. clamp() for Fluid Typography
No more breakpoints for font sizes.
The Old Way
h1 {
font-size: 2rem;
}
@media (min-width: 768px) {
h1 {
font-size: 2.5rem;
}
}
@media (min-width: 1024px) {
h1 {
font-size: 3rem;
}
}The Modern Way
h1 {
font-size: clamp(2rem, 5vw, 3rem);
/* min: 2rem, preferred: 5vw, max: 3rem */
}How It Works:
- At narrow viewports: 2rem (minimum)
- Scales with viewport: 5vw
- At wide viewports: 3rem (maximum)
Complete Fluid Type System
:root {
--font-size-sm: clamp(0.875rem, 0.8rem + 0.25vw, 1rem);
--font-size-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
--font-size-lg: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);
--font-size-xl: clamp(1.5rem, 1.3rem + 1vw, 2rem);
--font-size-2xl: clamp(2rem, 1.5rem + 2vw, 3rem);
}
body { font-size: var(--font-size-base); }
h1 { font-size: var(--font-size-2xl); }
h2 { font-size: var(--font-size-xl); }
small { font-size: var(--font-size-sm); }Browser Support: All modern browsers
4. Subgrid: Nested Grid Alignment
Align nested grids with parent grid tracks.
The Problem
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: grid;
grid-template-rows: auto 1fr auto;
/* Not aligned with parent grid */
}Subgrid Solution
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
}Result: Card rows align with parent grid.
Real-World: Product Grid
.products {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-template-rows: repeat(3, auto); /* Image, title, button */
gap: 1rem;
}
.product {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
}
.product-image { grid-row: 1; }
.product-title { grid-row: 2; }
.product-button { grid-row: 3; align-self: end; }All buttons align at the bottom, regardless of title length.
Browser Support: Safari 16+, Firefox 71+, Chrome: Coming soon
5. aspect-ratio: Fixed Proportions
No more padding-bottom hacks.
The Old Hack
.video-wrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
height: 0;
}
.video-wrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}The Modern Way
.video-wrapper {
aspect-ratio: 16 / 9;
}
.video-wrapper iframe {
width: 100%;
height: 100%;
}Common Use Cases
/* Square thumbnails */
.thumbnail {
aspect-ratio: 1;
object-fit: cover;
}
/* Widescreen images */
.hero-image {
aspect-ratio: 21 / 9;
width: 100%;
}
/* Profile avatars */
.avatar {
aspect-ratio: 1;
border-radius: 50%;
}Browser Support: All modern browsers
6. gap with Flexbox: Finally
gap works with flexbox now, not just grid.
Before
.flex-container {
display: flex;
}
.flex-container > * {
margin-right: 1rem;
}
.flex-container > *:last-child {
margin-right: 0; /* Remove trailing margin */
}Now
.flex-container {
display: flex;
gap: 1rem;
}That's it.
Complex Layouts
.toolbar {
display: flex;
justify-content: space-between;
gap: 0.5rem;
}
.toolbar-left {
display: flex;
gap: 0.25rem;
}
.toolbar-right {
display: flex;
gap: 0.75rem;
}Browser Support: All modern browsers
7. Grid Auto-Fit vs Auto-Fill
Create responsive grids without media queries.
auto-fill: Always Fill Space
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
}Behavior: Creates as many columns as fit, leaves empty columns if needed.
auto-fit: Collapse Empty Columns
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}Behavior: Collapses empty columns, stretches items to fill space.
When to Use Each
Use auto-fill when:
- You want consistent item sizes
- Empty space is acceptable
Use auto-fit when:
- You want items to stretch
- You have a small number of items
Browser Support: All modern browsers
8. object-fit: Image Cropping
Control how images fill containers.
The Problem
<div class="card">
<img src="wide-image.jpg" alt="Product">
</div>.card {
width: 300px;
height: 300px;
}
.card img {
width: 100%;
height: 100%;
/* Image gets distorted */
}Solutions
/* Cover: Crop to fill (like background-size: cover) */
.card img {
width: 100%;
height: 100%;
object-fit: cover; /* Crops image */
object-position: center; /* Crop from center */
}
/* Contain: Fit within bounds (like background-size: contain) */
.card img {
object-fit: contain; /* Letterboxes if needed */
}
/* Scale-down: Use original size or contain, whichever is smaller */
.logo {
object-fit: scale-down;
max-width: 200px;
max-height: 100px;
}Browser Support: All modern browsers
9. CSS Grid Named Areas
Semantic grid layouts.
Traditional Grid
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
}
.header { grid-column: 1 / -1; grid-row: 1; }
.sidebar { grid-column: 1; grid-row: 2; }
.main { grid-column: 2; grid-row: 2; }
.aside { grid-column: 3; grid-row: 2; }
.footer { grid-column: 1 / -1; grid-row: 3; }Named Areas
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
gap: 1rem;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }Responsive Layout Changes
@media (max-width: 768px) {
.layout {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"aside"
"sidebar"
"footer";
}
}Just redefine the areas—no need to change individual element styles.
Browser Support: All modern browsers
10. Logical Properties: Internationalization
Write direction-agnostic CSS.
The Problem
.button {
margin-left: 1rem; /* Breaks in RTL languages (Arabic, Hebrew) */
}Logical Properties
.button {
margin-inline-start: 1rem; /* LTR: left, RTL: right */
}Complete Mapping
/* Physical → Logical */
margin-top → margin-block-start
margin-right → margin-inline-end
margin-bottom → margin-block-end
margin-left → margin-inline-start
padding-left → padding-inline-start
padding-right → padding-inline-end
border-left → border-inline-start
border-right → border-inline-end
width → inline-size
height → block-size
text-align: left → text-align: start
text-align: right → text-align: endExample
.card {
padding-block: 1rem; /* Top and bottom */
padding-inline: 2rem; /* Left and right (or right and left in RTL) */
border-inline-start: 3px solid blue; /* Left border in LTR */
}Browser Support: All modern browsers
Bonus: Combining Techniques
Here's a real-world component using multiple techniques:
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: clamp(1rem, 2vw, 2rem);
container-type: inline-size;
}
.product-card {
display: grid;
grid-template-rows: auto auto 1fr auto;
gap: 0.5rem;
padding-block: 1rem;
padding-inline: 1rem;
aspect-ratio: 3 / 4;
}
.product-image {
aspect-ratio: 1;
object-fit: cover;
}
.product-price {
font-size: clamp(1rem, 3cqi, 1.5rem); /* Container query unit */
}
@container (max-width: 300px) {
.product-card {
grid-template-rows: auto 1fr auto;
}
.product-description {
display: none;
}
}
.product-card:has(.sale-badge) {
border: 2px solid red;
}Features used:
- CSS Grid with auto-fit
- clamp() for responsive spacing
- Container queries
- Logical properties
- aspect-ratio
- object-fit
- :has() selector
All without a single media query.
Conclusion
Modern CSS is incredibly powerful:
- Container queries for component-level responsiveness
- :has() for parent selection
- clamp() for fluid typography
- Subgrid for nested alignment
- aspect-ratio for proportions
- gap with flexbox
- Grid auto-fit/auto-fill
- object-fit for images
- Grid named areas
- Logical properties for i18n
Stop reaching for JavaScript. CSS can handle it.
---
Resources: