CSS: CSS Responsive Basics

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

Responsive design makes web pages look good on all devices — from mobile phones to desktop monitors.

Properties in this section: font-size

1. Viewport Meta Tag

Must-have tag for responsive design:

HTML
<!-- Meta information -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Without this tag, mobile browsers render pages at desktop width (usually 980px) and shrink to fit — text becomes unreadable.


2. Media Queries

Apply different styles at different screen sizes:

▶ Example

▶ Try it Yourself

3. Relative Units

Unit Reference Best For
% Parent element Container widths
em Parent font size Padding, margins
rem Root font size Font sizes
vw Viewport width Full-width elements
vh Viewport height Full-screen sections

▶ Example

▶ Try it Yourself

4. Mobile-First Approach

Design for mobile first, then enhance for larger screens:

CSS
/* Base styles (mobile) */
.container {
/* Size properties */
  /* Size properties */
  /* Size properties */
  /* Size properties */
  width: 100%;
/* Box model properties */
  /* Box model properties */
  /* Box model properties */
  /* Box model properties */
  padding: 10px;
}

/* Tablet */
/* Media query */
/* Media query */
/* Media query */
/* Media query */
@media (min-width: 768px) {
  .container {
/* Size properties */
    /* Size properties */
    /* Size properties */
    /* Size properties */
    width: 750px;
/* Box model properties */
    /* Box model properties */
    /* Box model properties */
    /* Box model properties */
    margin: 0 auto;
  }
}

/* Desktop */
/* Media query */
/* Media query */
/* Media query */
/* Media query */
@media (min-width: 1200px) {
  .container {
/* Size properties */
    /* Size properties */
    /* Size properties */
    /* Size properties */
    width: 1170px;
  }
}

▶ Example

Mobile-first responsive typography:

▶ Try it Yourself

5. Flexible Images

Make images responsive:

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

Properties in this section: font-size

❓ FAQ

Q How do I write a CSS media query?
A Basic syntax: @media (max-width: 768px) { .sidebar { display: none; } } — the condition goes in parentheses, the styles that apply in braces. Stack conditions with @media (min-width: 576px) and (max-width: 767px) to target one width range. Media queries can sit anywhere in your CSS, but usually right after the component styles they modify.
Q What breakpoints should I use for responsive design?
A Common reference points: mobile < 576px, tablet 768px, laptop 992px, desktop 1200px — Bootstrap uses this set. The practical way: drag your browser window narrower and set a breakpoint wherever content starts to squish. Breakpoints follow your content, not specific devices.
Q What does mobile-first mean — and should I prefer it over desktop-first?
A Mobile-first writes phone styles as the default, then enhances upward with @media (min-width: 768px) for tablet and desktop; desktop-first goes the other way with max-width. Prefer mobile-first: it handles the most constrained screen first, usually produces shorter CSS, and mobile traffic already exceeds desktop.
Q Should I use min-width or max-width in media queries?
A Follow your strategy: min-width for mobile-first (small screens default, wide screens enhance), max-width for desktop-first. Don't mix both in the same codebase — one rule "applies above X" and another "applies below Y" will confuse whoever maintains it next. Pick one convention project-wide.
Q My media query isn't working — what are the common causes?
A Check in this order: ① missing viewport meta tag — the phone renders at 980px desktop width and the condition never fires; ② the condition is inverted, like min-width: 1200px targeting phones; ③ a later rule with equal specificity overrides it — move the query later or strengthen the selector; ④ the viewport never reaches your breakpoint — confirm actual width in devtools responsive mode.
Q What's the best way to handle font sizes on mobile?
A Hang fonts on rem so one media query scales everything: @media (max-width: 768px) { html { font-size: 14px; } } — every 1rem/2rem size follows from 16px down to 14px. Big display headings can use font-size: 8vw to track the viewport, but wrap it in clamp() so they stay readable on small screens.
Q What happens without the viewport meta tag?
A Mobile browsers render at desktop width (usually 980px) then shrink the whole page to fit — text becomes tiny and you need pinch-zoom to read. <meta name="viewport" content="width=device-width, initial-scale=1.0"> is the prerequisite for everything else; without it your media queries won't even trigger on phones.
Q How do I hide a sidebar or element on mobile?
A One media query: @media (max-width: 768px) { .sidebar { display: none; } }. In mobile-first you'd flip it — hidden by default, shown inside @media (min-width: 769px). When hiding navigation content, provide a phone-side replacement (hamburger menu), or the feature is really gone.
Q Do I have to use rem for responsive design?
A No. rem shines for fonts (change one root size, the whole page scales), while layout dimensions are more direct with %, fr, vw/vh. The real rule is no fixed px for layout sizing. A common mix: fonts in rem, containers in %, spacing in em or rem.

📖 Summary

📝 Exercises

  1. Create a page: three columns on desktop, two on tablet, one on mobile (using media queries).
  2. Set font sizes with rem, then modify root font-size in media query to see proportional scaling.
  3. Create a responsive navbar: horizontal on desktop, hidden (or hamburger) on mobile.
  4. Set max-width: 100% on images, resize browser to see responsive behavior.
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%

🙏 帮我们做得更好

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

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