Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

HTML and CSS Responsive Design


HTML & CSS: Responsive Design

Responsive design ensures every layout looks and works great on any device.

Combine flexible grid systems, fluid typography, and media queries to adapt content gracefully.


Viewport basics

  • Add the viewport meta tag to control scaling: <meta name="viewport" content="width=device-width, initial-scale=1">.
  • Design mobile-first: start with a narrow viewport and progressively enhance for larger screens.
  • Use relative units (rem, vw, %) so elements scale naturally.

Note: Mobile-first means you write styles for phones first, then add media queries for tablets and desktops.

If you want to read more about Responsive Viewport or get an in-depth understanding, go to CSS Viewport in the CSS tutorial.


Fluid grids

  • Leverage Flexbox and Grid with fractional units to flow content.
  • Apply max-width to prevent overly wide text blocks.
  • Wrap tracks with minmax() and auto-fit/auto-fill for automatic column counts.

Note: Fractional units like 1fr share leftover space evenly, so columns resize smoothly.



Responsive typography

  • Use clamp() to set min/preferred/max values: font-size: clamp(1rem, 2vw, 1.5rem);.
  • Scale headings and spacing together to maintain rhythm.
  • Consider fluid spacing tokens, e.g., var(--space-fluid-sm).

Note: clamp() picks a value between a minimum and maximum, so text stays readable on every screen.


Media queries

Apply breakpoints based on design needs-not specific devices.

Note: A breakpoint is the screen width where your layout needs to change.

Choose widths that fit your content.

Mobile-first media queries

:root {
  --space: clamp(1rem, 2vw, 1.5rem);
}
.page {
  display: grid;
  gap: var(--space);
}
@media (min-width: 768px) {
  .page {
    grid-template-columns: 240px 1fr;
  }
}
@media (min-width: 1200px) {
  .page {
    grid-template-columns: 280px 2fr 1fr;
  }
}
Try it Yourself »

Learn more: CSS Media Queries.


Container queries (optional)

  • Use @container when component changes depend on parent width.
  • Wrap elements with container-type: inline-size;.
  • Still provide fallbacks for browsers without container query support.

Note: Container queries watch the size of a parent element instead of the whole screen, so components adapt exactly where needed.


Responsive media

  • Make images fluid using img { max-width: 100%; height: auto; }.
  • Serve different sources via <picture> or <source media>.
  • Preserve aspect ratios using object-fit or padding hacks for embeds.

Testing strategy

  • Use browser DevTools device mode to preview breakpoints quickly.
  • Test real devices when possible; pay attention to touch targets and orientation.
  • Audit performance-responsive assets should be optimized for mobile bandwidth.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.