CSS: CSS Responsive Layout

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

Properties in this section: min-width

1. Responsive Images

(1) Basic Responsive Image

CSS
img {
/* Size properties */
  /* Size properties */
  /* Size properties */
  /* Size properties */
  max-width: 100%;
/* Size properties */
  /* Size properties */
  /* Size properties */
  /* Size properties */
  height: auto;
}

(2) Responsive Background Image

CSS
.banner {
/* Background style */
  /* Background style */
  /* Background style */
  /* Background style */
  background-image: url("desktop-bg.jpg");
/* Background style */
  /* Background style */
  /* Background style */
  /* Background style */
  background-size: cover;
/* Positioning */
  /* Positioning */
  /* Positioning */
  /* Positioning */
  background-position: center;
}

/* Media query */
/* Media query */
/* Media query */
/* Media query */
@media (max-width: 768px) {
  .banner {
/* Background style */
    /* Background style */
    /* Background style */
    /* Background style */
    background-image: url("mobile-bg.jpg");
  }
}

2. Responsive Tables

Tables can overflow on small screens. Use overflow-x: auto for horizontal scrolling:

▶ Example

▶ Try it Yourself

3. Show/Hide Elements

Use media queries to show/hide elements at different breakpoints:

▶ Example

▶ Try it Yourself

▶ Example

Responsive Flexbox grid that adapts columns to screen size:

▶ Try it Yourself

4. Testing Responsive Layouts

  1. Browser resize: Drag browser window edges
  2. Chrome DevTools: F12 → Toggle device toolbar (Ctrl+Shift+M)
  3. Real devices: Test on actual phones/tablets when possible

Properties in this section: min-width

❓ FAQ

Q How do I build a responsive CSS layout? What are the common approaches?
A Three pieces: fluid units (% / fr), fluid images (max-width: 100%), and media query breakpoints. Build the page skeleton with Grid — grid-template-columns: 1fr single column on phones, then 1fr 300px main + sidebar inside a wide-screen media query; center the container with max-width: 1200px; margin: 0 auto. Go mobile-first and enhance with min-width step by step.
Q How do I make a responsive navbar?
A Mobile-first: stack the menu vertically by default, then inside @media (min-width: 768px) turn the navbar into display: flex; justify-content: space-between with the menu items laid out horizontally (display: flex; gap: 20px). For a hamburger version, add a button plus a bit of JS to toggle the menu; a checkbox hack can do it with zero script.
Q How do I build a responsive card grid?
A Grid plus media queries adding columns: default grid-template-columns: 1fr, repeat(2, 1fr) at 600px, repeat(3, 1fr) at 900px, 4 columns at 1200px. Give card images width: 100%; height: 200px; object-fit: cover so images don't distort the card. The lazy one-liner: repeat(auto-fill, minmax(260px, 1fr)) — no breakpoints needed.
Q How do I use CSS clamp() for fonts?
A font-size: clamp(16px, 3vw, 32px) — minimum, preferred, maximum. Narrow viewports get the 16px floor, wide ones cap at 32px, and in between the size tracks 3vw continuously. Smoother than breakpoint media queries and one line for responsive headings; don't use it for body text, which needs a stable size on phones.
Q How to make images responsive?
A Basic: img { max-width: 100%; height: auto; } so images never overflow; pair with object-fit: cover when you need uniform cropped heights. Advanced: <picture> and <source> load different resolutions per screen size, saving mobile data. The basic version is enough for most cases.
Q Must a responsive layout have three versions?
A No. Get the mobile experience right first, then enhance on larger screens. Mobile + desktop is enough for many pages; tablet is often a natural transition. The principle is content first, device adaptation — not responsive for its own sake.
Q How do I test responsive layouts?
A Fastest is dragging the browser window; for precision use Chrome DevTools device emulation (F12, then Ctrl+Shift+M) to switch between iPhone/iPad sizes. But real touch feel and notches or dynamic islands occluding content only surface on actual devices — run key pages on a real phone before shipping.
Q My page gets a horizontal scrollbar on phones — how do I fix it?
A Check in order: ① an element with a fixed width (like width: 800px) — switch to max-width or percentages; ② images missing max-width: 100%; ③ long words or URLs bursting the container — add overflow-wrap: anywhere; ④ flex children not shrinking — set min-width: 0; ⑤ wide tables wrapped in overflow-x: auto for local horizontal scrolling.
Q Should I use Grid or Flexbox for responsive grids?
A One dimension (navbars, toolbars) → Flexbox; two dimensions (page skeleton, card grids) → Grid. Grid's repeat(auto-fill, minmax(260px, 1fr)) is responsive out of the box; Flex's flex-wrap: wrap also does card flows. Don't treat it as either/or — real projects mix them: Grid for the skeleton, Flex inside for content.

📖 Summary

📝 Exercises

  1. Create a "responsive personal blog" page with header navigation, article list, sidebar recommendations, and footer copyright. Requirements: mobile single column, tablet two columns, desktop three columns + fixed sidebar.
  2. Use min-width breakpoints to change a card grid from 1 column to 4 columns, observing at each breakpoint.
  3. Create a responsive table page where tables can scroll horizontally on small screens (using overflow-x: auto).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏