Back to blog
Web Development

How to Make a Website Mobile Responsive Using Only CSS

Make any website mobile responsive with pure CSS: viewport setup, fluid units, wrapping Flexbox and Grid layouts, and content-driven media query breakpoints.

AdminOctober 19, 202612 min read2 views
How to Make a Website Mobile Responsive Using Only CSS

How to Make a Website Mobile Responsive Using Only CSS

Most sites that break on phones are not broken because of missing JavaScript or a missing framework. They break because of a fixed width somewhere, a missing viewport tag, or an image that refuses to shrink. Mobile responsive design is the practice of building one HTML document whose layout, type, and media adapt fluidly to any screen width, and you can achieve all of it with plain CSS.

Quick Answer: To make a website mobile responsive using only CSS, add the viewport meta tag, write mobile-first base styles, size layouts with relative units like percentages, rem, and fr, make images fluid with max-width: 100%, use Flexbox or Grid that wraps naturally, and add min-width media queries only where the design actually breaks.

What Does Mobile Responsive Actually Mean in CSS?

A responsive page is one whose layout responds to the space available instead of assuming a fixed canvas. In CSS terms that comes down to four building blocks: a correct viewport, fluid units, flexible layout modules, and conditional rules through media or container queries. Everything else is refinement on top of those four.

The viewport meta tag is the piece beginners miss most often. Without <meta name="viewport" content="width=device-width, initial-scale=1"> in the head, mobile browsers render the page at a virtual desktop width, usually around 980 pixels, and then shrink it. Your media queries never fire because the browser believes it is wide. Once the tag is present, the CSS pixel width matches the device, and your rules start behaving. If you are deciding how to structure the layout itself, our guide to choosing between Flexbox and Grid walks through which module suits which pattern.

Fluid units are the second building block. Percentages size things relative to the parent, rem sizes relative to the root font size, vw and vh size relative to the viewport, and fr divides free space inside a grid. A layout built with these units stretches and compresses on its own, which means you need far fewer breakpoints. Media queries then become the exception for genuine layout changes, not a patch for every pixel width.

The term "mobile-first" simply means your default CSS, written outside any media query, targets the smallest screen. You then layer enhancements for larger screens with min-width queries. This order matters because small-screen styles are usually simpler: a single column, full-width elements, stacked navigation. Adding complexity as space grows produces less override code than removing complexity as space shrinks.

The Core CSS Techniques That Make a Layout Responsive

Responsive CSS is a short list of techniques applied consistently. These are the ones I apply on every build, in roughly the order I apply them.

  1. Reset box sizing. Add *, *::before, *::after { box-sizing: border-box; } so padding and borders are included in an element's width. Without it, a 100% wide box with padding overflows the screen and triggers horizontal scrolling.
  2. Make media fluid. Set img, video, svg { max-width: 100%; height: auto; display: block; }. Images then shrink inside their containers but never upscale beyond their natural size and blur.
  3. Constrain content width, not page width. Use a wrapper such as width: min(100% - 2rem, 70rem); margin-inline: auto;. It fills small screens with a gutter and caps line length on large ones in a single declaration.
  4. Use fluid typography. font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); on headings scales smoothly between a minimum and maximum, removing a whole class of heading breakpoints.
  5. Let layouts wrap. display: flex; flex-wrap: wrap; gap: 1rem; or grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); produce card grids that reflow from one to four columns with zero media queries.
  6. Add min-width media queries at content breakpoints. Resize the browser slowly and add a query where the design starts to look wrong, not at a list of device widths.
  7. Use container queries for components. With container-type: inline-size on a parent, a card can switch from stacked to horizontal based on its own container width, so it works in a sidebar and a main column alike.
  8. Size tap targets generously. Buttons and links should have enough padding to be tapped accurately. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels, and larger is more comfortable.

Notice that only one item on that list is a media query. That is deliberate. Teams that start with breakpoints tend to accumulate dozens of fragile overrides. Teams that start with fluid units and wrapping layouts usually need two or three queries for an entire site.

Comparing CSS Units and Tools for Responsive Layouts

Each responsive tool solves a specific problem, and choosing the wrong one creates extra work later. The table below summarizes where each fits, based on how they behave in production builds.

TechniqueWhat It Responds ToBest UseCommon Pitfall
PercentagesParent element widthColumn widths, fluid imagesNested percentages compound unexpectedly
rem and emRoot or parent font sizeSpacing, type, breakpointsem compounds inside nested elements
clamp() with vwViewport width within limitsFluid headings and section paddingPure vw text ignores user zoom settings
Media queriesViewport size and user preferencesPage-level layout changesTargeting device widths instead of content
Container queriesNearest container's sizeReusable cards and widgetsForgetting to declare container-type on the parent
auto-fit with minmax()Available grid spaceCard and gallery gridsMinimum value too large for small phones

A practical rule: reach for intrinsic tools first (percentages, clamp, auto-fit) and conditional tools second (media and container queries). Intrinsic tools adapt continuously, while conditional tools only change at specific thresholds.

One more note on the clamp row. Text sized only in vw does not grow when a user zooms, which is an accessibility failure. Mixing a rem term into the middle value, as in 1.2rem + 2.5vw, keeps zoom working while still scaling with the viewport.

Why Mobile-First CSS Matters: Practitioner Analysis

Google has used mobile-first indexing for essentially all sites since its rollout completed in 2023, which means the mobile rendering of your page is the version Google evaluates. If content or structured markup is hidden or broken on small screens, that is what gets indexed. That alone justifies treating the phone layout as the primary layout rather than an afterthought.

Beyond search, my experience across client builds is consistent: mobile-first stylesheets are shorter and easier to maintain. When the base styles are a single column, the larger-screen queries mostly add properties such as grid-template-columns. When the base styles are a desktop layout, the small-screen queries must undo widths, floats, absolute positioning, and hidden overflow, and every undo is a chance for a bug. Desktop-first sheets also tend to rely on max-width queries that overlap at boundary pixels, producing a one-pixel range where neither rule applies cleanly.

Performance follows the same pattern. A mobile-first approach pushes you to question every decorative element before it reaches the smallest, slowest devices. Responsive images with srcset and sizes are HTML rather than CSS, but they pair with your CSS sizing so phones download smaller files.

Navigation is where most responsive builds get stuck, because a horizontal menu rarely fits on a phone. A CSS-only approach can stack links or use a details element, but if you need a toggle, our walkthrough on building a navigation bar that collapses on small screens covers both the CSS and the small script involved. The rest of the page, as the sections above show, rarely needs anything beyond CSS.

Common Responsive CSS Mistakes and How to Avoid Them

Most responsive bugs come from a handful of repeated mistakes. Here are the ones I see most in code reviews, with the fix for each.

  • Fixed widths on containers. A width: 1200px wrapper guarantees horizontal scrolling on phones. Replace it with max-width or the min() pattern shown earlier.
  • Using 100vw for full-width sections. On desktop, 100vw includes the vertical scrollbar, so the element overflows by the scrollbar's width. Use width: 100% on a block element instead.
  • Breakpoints copied from device lists. Phones and tablets ship in too many sizes to target. Set breakpoints where your content breaks, and express them in em so they respect user font settings.
  • Hiding content on mobile. display: none on important sections removes them for mobile users and for mobile-first indexing. Reorganize instead of deleting.
  • Height-locked sections. A hero with height: 100vh clips text on short phones or landscape orientation. Use min-height and consider 100svh, which accounts for browser toolbars.
  • Unbreakable strings. Long URLs or code snippets push layouts wide. Add overflow-wrap: anywhere to text containers and overflow-x: auto to code blocks and tables.
  • Testing only by shrinking the desktop browser. Device emulation is useful but does not reproduce touch behavior, real font rendering, or dynamic toolbars. Check on at least one real phone before launch.

The fastest way to find the culprit behind horizontal scrolling is to temporarily add * { outline: 1px solid red; } and scroll sideways. The element poking past the edge is usually obvious within seconds.

Step-by-Step: Converting a Fixed Layout to a Responsive One

Here is the exact sequence I use when a client hands over a desktop-only site and asks for it to work on phones without a rebuild. It works on static HTML, WordPress themes, and hand-coded templates alike.

Step 1: Audit. Open the page at a 375 pixel width in developer tools. Note every element that overflows, every text block that is too small, and every interaction that requires hovering. This becomes your checklist.

Step 2: Add the foundation. Insert the viewport meta tag, the border-box reset, and the fluid media rule. On many legacy sites these three changes alone fix half the problems.

Step 3: Replace fixed widths. Search the stylesheet for px widths above roughly 300. Convert container widths to max-width, column widths to percentages or fr, and remove floats in favor of Flexbox or Grid where columns exist.

Step 4: Rebuild the column layout mobile-first. Move the multi-column rules into a @media (min-width: 48em) block so the default becomes a single column. For example, a three-column features row becomes grid-template-columns: 1fr by default and repeat(3, 1fr) inside the query.

Step 5: Make type fluid. Set body text to at least 1rem, apply clamp() to headings, and keep line length between roughly 45 and 75 characters with a max-width in ch units on text blocks.

Step 6: Fix tables and embeds. Wrap data tables in a container with overflow-x: auto, and give video embeds aspect-ratio: 16 / 9; width: 100%; height: auto; so they scale without padding hacks.

Step 7: Test real interactions. On a physical phone, tap every link and button, fill in every form, and rotate the device. Check that nothing relies on hover to reveal information.

On a typical brochure site, this process removes horizontal scrolling entirely and needs only two or three media queries in total. The result is easier to maintain than the original because the CSS now describes relationships rather than fixed coordinates.

Key Takeaways

  • The viewport meta tag is mandatory; without it, mobile browsers ignore your responsive CSS by rendering at a desktop width.
  • Fluid units, wrapping Flexbox, and auto-fit Grid handle most responsiveness before a single media query is written.
  • Mobile-first CSS produces shorter stylesheets because larger screens add layout rules rather than undoing them.
  • Set breakpoints where your content breaks, expressed in em units, not at a list of popular device widths.
  • Container queries make components responsive to their own space, so the same card works in any column.

Frequently Asked Questions

Can I make a website responsive without any framework?

Yes. Every responsive technique frameworks use is standard CSS: media queries, Flexbox, Grid, relative units, and container queries. Frameworks only package these into predefined classes. Writing them yourself produces smaller stylesheets and teaches you exactly why a layout behaves the way it does on each screen.

What is the most important line of code for mobile responsiveness?

The viewport meta tag in your HTML head is the most important line. Without width=device-width and initial-scale=1, mobile browsers render your page at a virtual desktop width and shrink it, so your media queries never trigger. It is HTML rather than CSS, but nothing responsive works without it.

How many media query breakpoints does a website need?

Most well-built sites need only two or three breakpoints when they use fluid units and wrapping layouts. Add a breakpoint only where your content visibly breaks while resizing the browser. Component-level changes are often better handled by container queries, which keeps page-level media queries to a minimum.

Should I use px, em, or rem in media queries?

Use em in media queries. Em-based breakpoints scale with the user's default browser font size, so someone who enlarges their text gets the layout designed for that effective width. Pixel breakpoints ignore that preference. Inside media queries, em and rem behave the same because both reference the browser default.

Why does my website scroll sideways on mobile?

Sideways scrolling almost always comes from one element wider than the viewport. Common causes are fixed pixel widths, 100vw sections, images without max-width, long unbroken URLs, and wide tables. Add a temporary outline to every element in developer tools and scroll horizontally to find the overflowing one quickly.

What is the difference between media queries and container queries?

Media queries respond to the browser viewport size, so they suit page-level layout decisions. Container queries respond to the size of a parent element you mark with container-type, so they suit reusable components. A card using container queries adapts correctly whether it sits in a narrow sidebar or a wide main column.

Conclusion

The single most important shift is to stop designing for devices and start designing for available space. When your CSS describes how elements relate to the space around them, through fluid units, wrapping layouts, and a handful of content-driven breakpoints, the page works on screens you never tested. Your next step is to run the seven-step conversion on one existing page, then confirm the result works for everyone by making responsive pages accessible to keyboard and screen reader users too.

Chat on WhatsApp