We Might Need Something Between Root And Relative CSS Units For Base Elements

Discover how container query units (cqw, cqh, cqi) solve the gap between root-relative (rem) and element-relative (em) units for truly modular, responsive components.

The Gap in Our CSS Unit Toolbox

Modern CSS provides a rich ecosystem of length units for styling web applications. From pixel-based absolute units to flexible relative units like em and rem, developers have choices for creating responsive, scalable designs. However, as component-based architectures become the norm and layouts grow more complex, a gap emerges in our unit toolbox.

We have units relative to the root element (rem), and units relative to the immediate parent element (em), but what about units relative to an intermediate container? This gap has significant implications for building truly modular, responsive components that work consistently across different contexts.

In this guide, we'll explore how container query units fill this critical gap and transform how we approach responsive component design in modern web development.

Understanding Your CSS Unit Options

A comparison of the three main relative unit categories for responsive design

Root-Relative (rem)

Always calculates relative to the root element's font size. Provides predictable, document-wide scaling from a single control point.

Element-Relative (em)

Calculates relative to the parent element's font size. Creates locally proportional designs with compounding behavior in nested elements.

Container-Relative (cqw, cqh)

Calculates relative to the nearest query container's dimensions. Enables truly modular, context-aware components that scale with their placement.

Root-Relative Units: The rem Foundation

The rem unit, which stands for "root em," has become the cornerstone of scalable typography and spacing in modern web development. Unlike em, rem always calculates its value relative to the font size of the root element (html), providing consistent, predictable scaling across an entire document.

The key advantage of rem lies in its predictability. When you set an element's font size to 1.5rem and the root font size is 16 pixels, that element will render at 24 pixels regardless of any parent element's font size.

When rem Works Best

  • Global typography scales and heading hierarchies
  • Consistent spacing patterns (margins, padding) across a site
  • Any values that should scale proportionally when users adjust their browser's base font size
  • Maintaining design consistency across multi-page applications

The Limitation

However, this predictability becomes a limitation in component-based architectures. A card component has no mechanism to "know" about its container's dimensions using rem--it only understands the root font size and its parent's font size.

rem vs em Behavior Comparison
1/* Root-relative (rem) - always uses html font-size */2html {3 font-size: 16px; /* Root font size */4}5 6.card-title {7 font-size: 2rem; /* Always 32px, regardless of parent */8}9 10/* Element-relative (em) - uses parent's font-size */11.card {12 font-size: 18px;13}14 15.card-title {16 font-size: 2em; /* 36px (2 × 18px parent) */17}18 19.card-title small {20 font-size: 0.5em; /* 18px (0.5 × 36px grandparent) */21}

Element-Relative Units: The em Approach

The em unit represents the font size of the current element, creating a parent-referential system that excels at creating locally proportional designs. When applied to font-size, an em value multiplies against the parent element's computed font size.

The classic use case for em involves creating button components with padding that scales with the button's font size. A button with font-size: 1em and padding: 0.5em 1em creates internal spacing that maintains its proportions as the font size changes.

The Compounding Challenge

However, em introduces complexity through its compounding behavior. Nested elements using em for font-size calculations can unexpectedly grow or shrink as the nesting depth increases:

/* Each level compounds: 1.2 × 1.2 × 1.2 = 1.728 */
.section { font-size: 1.2em; }
.article { font-size: 1.2em; }
.paragraph { font-size: 1.2em; }
/* Final computed size: 1.728× base font size */

When em Works Best

  • Component-local proportional sizing
  • Button padding and form input sizing
  • Nested component typography within the same hierarchy
  • Maintaining proportions between text and internal spacing

Container Query Units: The Middle Ground Solution

CSS container queries introduce a new category of relative units specifically designed to address this exact gap. Container query length units allow you to size elements based on the dimensions of their nearest query container rather than the viewport or parent element.

The Six Container Query Units

UnitDescriptionUse Case
cqw1% of container widthHorizontal-responsive typography and spacing
cqh1% of container heightVertical-responsive sizing
cqi1% of container inline-sizeWriting-mode-aware horizontal sizing
cqb1% of container block-sizeWriting-mode-aware vertical sizing
cqminSmaller of cqi or cqbConsistent sizing regardless of aspect ratio
cqmaxLarger of cqi or cqbMaximum responsive scaling

These units require a containment context established using the container-type property with inline-size or size values. Combined with modern CSS selectors like the CSS :has() selector, container query units unlock powerful new patterns for building adaptable interfaces.

Container Query Units in Action
1/* Step 1: Create a containment context */2.card {3 container-type: inline-size;4 container-name: card;5}6 7/* Step 2: Use container query units for responsive sizing */8.card-title {9 /* Scale from 1.25rem to 2rem based on container width */10 font-size: clamp(1.25rem, 4cqw, 2rem);11}12 13.card-content {14 /* Scale proportionally with container */15 font-size: clamp(0.875rem, 2.5cqw, 1rem);16 padding: 1cqw;17}18 19.card-button {20 /* Button scales with container */21 padding: 0.75cqw 1.5cqw;22 font-size: clamp(0.75rem, 2cqw, 1rem);23}24 25/* Step 3: Target specific containers by name */26@container card (width > 400px) {27 .card {28 display: grid;29 grid-template-columns: 1fr 1fr;30 }31}

Performance Benefits for Modern Web Development

In the context of modern web development with frameworks like Next.js, container query units align naturally with component-based architectures. Components become truly reusable because they carry their own responsive logic, independent of their placement in the page layout.

Reducing Media Query Complexity

Traditional responsive design relies heavily on media queries that respond to viewport dimensions. Container query units eliminate this limitation by moving the responsive boundary from the viewport to the component's container:

  • Component styling logic stays with the component
  • No more scattered media query breakpoints across stylesheets
  • Components work consistently regardless of page layout
  • Easier maintenance and refactoring

Improved Core Web Vitals

Container query units, combined with proper containment, can improve rendering performance:

  • Containment tells the browser that the container's contents won't affect external elements
  • Browsers can optimize rendering knowing layout is isolated
  • Reduced layout thrashing from viewport-relative calculations
  • More efficient reflows when components resize

For teams building modular CSS architectures, container query units pair excellently with approaches like CSS Modules for truly encapsulated component styling.

CSS Unit Comparison: Choosing the Right Tool
Unit TypeReference PointBest ForCompounding?Browser Support
px (absolute)Fixed pixelsBorders, precise spacingN/A100%
remRoot element font-sizeGlobal typography, spacing systemsNo100%
emParent element font-sizeComponent-local proportionsYes100%
cqw/cqh/cqiContainer dimensionsContext-aware componentsNo95%+
vw/vhViewport dimensionsFull-screen layoutsNo100%
%Containing block sizeFluid layoutsVaries100%

The Future of Responsive CSS

Container query units represent an evolution in how we think about responsive design. Rather than treating responsiveness as a page-wide concern addressed through viewport-based media queries, container query units enable component-level responsiveness that respects actual layout context.

This shift aligns naturally with modern component-based development patterns. As frameworks like Next.js continue to emphasize component architecture, the ability to create self-contained, responsive components becomes increasingly valuable.

The "something between root and relative CSS units" we've needed is not a hypothetical new unit but an entire category of units already available in modern browsers. Container query units fill the gap between root-relative and element-relative sizing, enabling truly modular, context-aware components that maintain their proportions across any placement.

The Complete Responsive System

The combination of all three unit types creates a complete system for responsive design:

  1. rem - For document-wide scaling from a single control point
  2. em - For component-local proportions within hierarchies
  3. cqw/cqh - For context-aware component sizing based on placement

Mastering when to use each unit type--and combining them strategically--transforms how you approach responsive web development, creating more maintainable, performant, and truly modular designs.

Frequently Asked Questions

Ready to Build Truly Modular Components?

Our web development team specializes in modern CSS architecture, component-based design, and performance-optimized responsive layouts.