CSS: CSS Responsive Layout
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 Yourself3. 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 Yourself4. Testing Responsive Layouts
- Browser resize: Drag browser window edges
- Chrome DevTools: F12 → Toggle device toolbar (Ctrl+Shift+M)
- Real devices: Test on actual phones/tablets when possible
❓ 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
- Responsive layout's core is "flexible adaptation"
- Through flexible grids (% / fr), flexible images (max-width), and media queries (@media), you can build pages that perform well on all screens
- In practice, "mobile-first" is recommended: write mobile styles first, then use
min-widthto progressively add styles for tablet and desktop
📝 Exercises
- 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.
- Use
min-widthbreakpoints to change a card grid from 1 column to 4 columns, observing at each breakpoint. - Create a responsive table page where tables can scroll horizontally on small screens (using
overflow-x: auto).