D DevBrainBox

CSS Fluid Layouts

CSS

What is a Fluid Layout?

A fluid layout is a layout that stretches and shrinks based on the size of the browser window or screen. It uses relative units (like %, vw, em) instead of fixed units (like px).

Why Use Fluid Layouts?

  • Works on any screen size (mobile, tablet, desktop)
  • Eliminates horizontal scrolling
  • Keeps your design flexible and responsive
UnitWhat it means
%Relative to parent element
vw% of viewport width
vh% of viewport height
em / remRelative to font size

Example 1: Fluid Container

.container {
  width: 90%;          /* Takes 90% of the screen */
  max-width: 1200px;   /* But doesn’t go beyond 1200px */
  margin: 0 auto;      /* Center align */
}

This container adapts to screen size but stays readable on large screens.

Example 2: Fluid Image

img {
  width: 100%;         /* Scales with the parent */
  height: auto;        /* Keeps the aspect ratio */
}

Image scales smoothly on different devices.

Example 3: Fluid Columns

.column {
  width: 50%;          /* Two equal columns */
  float: left;
}

Columns adjust as the screen resizes.

Combine with Media Queries

Use media queries to improve fluid layouts on breakpoints:

@media (max-width: 600px) {
  .column {
    width: 100%;       /* Stack columns on small screens */
  }
}
FeatureBenefit
Relative unitsAutomatically adjust to screen size
% & vw/vhIdeal for flexible layouts
Fluid imagesResize without distortion
Combine with media queriesFor better control

On this page