Mobile first is an approach where you design for the narrowest screen first and add only what's needed as the screen gets wider. Designer Luke Wroblewski proposed it in 2009 and laid it out in his 2011 book Mobile First; today it's the default strategy for responsive web development. This guide covers the order to write your CSS in, how to convert existing desktop-first code, and how to build the UI you end up making on mobile again and again — hamburger menus, bottom tab bars and forms. The tools themselves — media query syntax, container queries, responsive images — are explained in responsive web design basics.
1. What is mobile first?
In CSS, mobile first is a question of writing order. The base styles, written without any media query, are for the narrowest screen, and rules for wider screens are added only "at this width and up", as in @media (width >= 768px). Desktop first is the reverse: wide-screen styles are the base, and max-width media queries undo them on small screens.
- Small screen as the base: styles outside media queries target a phone screen of around 360px.
- Progressive enhancement: layout and features are added as conditions improve — a wider screen, a mouse, a faster network.
- Content priority: deciding what to show first on a narrow screen forces you to make the core content and actions (buy, contact) obvious.
2. Why build up with min-width
The benefit of mobile first isn't a vague "better performance" — it comes from how the code is structured. The main reasons:
- Fewer overrides: the base style is a simple one-column layout, so you only add the properties large screens need inside min-width media queries. You write far less code undoing fixed widths, multi-column layouts and sticky positioning on small screens, as desktop first requires.
- Key actions first: designing for the narrow screen first makes you prioritize putting essentials like buy and contact buttons on the first screen.
- SEO: since July 2024, Google crawls every site with the smartphone Googlebot and indexes the mobile version. Whether your CSS uses min-width or max-width has nothing to do with rankings, but any body text, headings or image alt text that is missing or hidden on mobile is missing from the index too.
3. The same component, both ways
Let's write a page with a sidebar and a card list both ways. The behavior is identical: at 1024px and up, a sidebar plus three card columns; at 768–1023px, no sidebar and two card columns; narrower than that, one card column. First, desktop first. The "undo" comments mark declarations that cancel a value set for large screens.
/* Desktop first */
.layout { display: grid; grid-template-columns: 240px 1fr; gap: 2rem; }
.sidebar { position: sticky; top: 1rem; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; }
@media (max-width: 1023px) {
.layout { grid-template-columns: 1fr; gap: 1rem; } /* undo */
.sidebar { position: static; } /* undo */
.cards { grid-template-columns: repeat(2, 1fr); gap: 1.5rem; }
}
@media (max-width: 767px) {
.cards { grid-template-columns: 1fr; gap: 1rem; } /* undo */
}
Written mobile first, the same result has no column settings in the base styles at all, because a grid defaults to one column. The undo declarations disappear, and so does the fractional gap between 1023px and 1024px.
/* Mobile first */
.layout { display: grid; gap: 1rem; }
.cards { display: grid; gap: 1rem; }
@media (width >= 768px) {
.cards { grid-template-columns: repeat(2, 1fr); gap: 1.5rem; }
}
@media (width >= 1024px) {
.layout { grid-template-columns: 240px 1fr; gap: 2rem; }
.sidebar { position: sticky; top: 1rem; }
.cards { grid-template-columns: repeat(3, 1fr); gap: 2rem; }
}
With CSS nesting (supported by every major browser as of 2026), you can put media queries inside the component's rule and read how one component changes across screens in a single place.
.cards {
display: grid;
gap: 1rem;
@media (width >= 768px) {
grid-template-columns: repeat(2, 1fr);
gap: 1.5rem;
}
@media (width >= 1024px) {
grid-template-columns: repeat(3, 1fr);
gap: 2rem;
}
}
4. Converting existing code
For code already written desktop first, it's safer to migrate one component at a time than to switch everything at once. Work through these steps:
- Promote the narrowest range to the base: move the values inside the
max-width: 767pxblock into the base styles, outside any media query. - Move the old base into min-width: move the former desktop base values into an
@media (width >= 1024px)block, and the middle-range values into an@media (width >= 768px)block. - Delete undo declarations: declarations that reset to defaults —
position: static,float: none,width: auto,grid-template-columns: 1fr— are no longer needed, so remove them. - Clean up empty max-width blocks: delete any
max-widthmedia queries left empty. - Compare by width: screenshot the page at 360, 768, 1024 and 1440px before and after the change and compare.
5. Base widths and test devices
These are the CSS viewport widths you'll run into most often. A common approach is to start the mobile design at 360px, the narrowest width in wide use, and then check that nothing breaks at the other widths. Each width's conversion table lets you look up design px values in vw directly.
| CSS width | Typical devices | Use | Conversion table |
|---|---|---|---|
| 360px | Base Galaxy S models and many other Android phones | Mobile design base | 360px table |
| 375px | iPhone SE (2nd/3rd gen), iPhone mini | Compatibility with older 375px designs | 375px table |
| 390–393px | iPhone 14, 15, 16 base models | Checking recent iPhones | 390px · 393px table |
| 412px | Galaxy A series, Pixel and other large Android phones | Checking wide phones | 412px table |
| 430px | iPhone Plus and Pro Max models (the latest Pro Max is 440px) | Checking the widest phones | 430px table |
| 768px | Tablets in portrait | First min-width range | 768px table |
| 1024px | Tablets in landscape, small laptops | Desktop layout begins | 1024px table |
| 1440px | Typical laptops and monitors | Desktop design base | 1440px table |
Device lineups change every year, so for a live site check the screen resolution report in your analytics to see the real distribution. It also helps to save the widths you use most as custom devices in Chrome DevTools device mode. Whether to use 360px or 375px as your design base is compared in detail in responsive mobile font sizing.
6. Touch targets
A finger is far less precise than a mouse cursor. WCAG 2.2 sets a minimum touch target of 24×24px (2.5.8, Level AA), and 44–48px or more is recommended in practice. Even if the icon is small, make the button itself larger, and apply hover effects only in mouse environments to avoid hover states sticking after a tap.
.icon-button {
display: inline-grid;
place-items: center;
min-width: 44px;
min-height: 44px; /* the icon may be 24px, but the tappable area is 44px */
}
/* Only where hover is possible, like with a mouse */
@media (hover: hover) and (pointer: fine) {
.icon-button:hover {
background: #f0f0f0;
}
}
7. Hamburger menu
If you give a full-screen mobile menu height: 100vh, the bottom of the menu gets cut off or overflows by the height of the mobile browser's address bar. Use 100dvh, which follows the actual height as the address bar collapses and expands. A menu pushed off-screen also needs visibility: hidden, so keyboard Tab and screen readers can't land on links in the closed menu.
.mobile-nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100dvh; /* instead of 100vh: tracks address bar changes */
background: rgba(0, 0, 0, 0.9);
transform: translateX(-100%);
visibility: hidden; /* keeps closed-menu links out of the Tab order */
transition: transform 0.3s ease, visibility 0s linear 0.3s;
z-index: 1000;
}
.mobile-nav.active {
transform: translateX(0);
visibility: visible;
transition: transform 0.3s ease;
}
.mobile-nav a {
display: block;
padding: 1rem 2rem;
color: #fff;
font-size: 1.25rem;
text-decoration: none;
}
.hamburger {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 1001;
min-width: 44px;
min-height: 44px;
border: none;
background: none;
color: #333;
font-size: 1.5rem;
}
/* Stay visible on the dark background when the menu is open */
.hamburger[aria-expanded="true"] {
color: #fff;
}
.desktop-nav {
display: none;
}
/* Lock page scrolling behind the open menu (mobile range only) */
@media (width < 768px) {
body:has(.mobile-nav.active) {
overflow: hidden;
}
}
@media (prefers-reduced-motion: reduce) {
.mobile-nav,
.mobile-nav.active {
transition: none;
}
}
@media (width >= 768px) {
.mobile-nav,
.hamburger {
display: none;
}
.desktop-nav {
display: flex;
gap: 2rem;
}
}
Give the button aria-expanded to announce whether the menu is open and aria-controls to point at the element it toggles, and update the state on click. The :has() used for the scroll lock selects "a body that contains an open menu" without any JavaScript.
<button class="hamburger" type="button"
aria-label="Open menu" aria-expanded="false" aria-controls="mobile-nav">☰</button>
<nav class="mobile-nav" id="mobile-nav"> … </nav>
<script>
const button = document.querySelector('.hamburger');
const nav = document.getElementById('mobile-nav');
button.addEventListener('click', () => {
const open = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!open));
button.setAttribute('aria-label', open ? 'Open menu' : 'Close menu');
nav.classList.toggle('active', !open);
});
</script>
For the differences between 100vh, 100svh and 100dvh, see the viewport units guide.
8. Bottom tab bar and safe-area
To let the page fill the area behind the iPhone's home indicator at the bottom of the screen, add viewport-fit=cover to the viewport meta tag. With this setting (or in a web app added to the home screen), elements fixed to the bottom of the screen overlap the home indicator, so you need to add env(safe-area-inset-bottom) of extra padding.
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
Give body the same amount of bottom padding so the tab bar doesn't cover the end of the content. The active tab color is #1565c0, which has better than 4.5:1 contrast on white (about 5.7:1); at 768px and up the tab bar is hidden and the padding reset.
.bottom-nav {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-around;
padding: 0.5rem env(safe-area-inset-right) calc(0.5rem + env(safe-area-inset-bottom)) env(safe-area-inset-left);
background: #fff;
border-top: 1px solid #e0e0e0;
z-index: 1000;
}
.bottom-nav-item {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-width: 48px;
min-height: 48px;
color: #666; /* about 5.7:1 on white */
font-size: 0.75rem;
text-decoration: none;
}
.bottom-nav-item[aria-current="page"] {
color: #1565c0; /* about 5.7:1 on white */
}
body {
padding-bottom: calc(4rem + env(safe-area-inset-bottom));
}
@media (width >= 768px) {
.bottom-nav {
display: none;
}
body {
padding-bottom: 0;
}
}
9. Mobile forms
iOS Safari automatically zooms in when you tap an input whose font size is smaller than 16px. Adding maximum-scale=1 to the viewport to stop this prevents users from zooming at all, which is an accessibility failure — the right fix is to keep input text at 1rem (16px) or larger. Input borders need at least 3:1 contrast against a white background (WCAG 1.4.11), and focus should stay clearly visible with an outline.
.form-label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: #333;
}
.form-input {
width: 100%;
min-height: 48px;
padding: 0.75rem 1rem;
border: 2px solid #8a8a8a; /* about 3.5:1 on white */
border-radius: 8px;
font-size: 1rem; /* below 16px, iOS Safari zooms in automatically */
background: #fff;
}
.form-input:focus-visible {
outline: 3px solid #1976d2;
outline-offset: 2px;
border-color: #1976d2;
}
.form-button {
width: 100%;
min-height: 48px;
padding: 0.75rem 2rem;
border: none;
border-radius: 8px;
background: #1976d2; /* about 4.6:1 with white text */
color: #fff;
font-size: 1rem;
font-weight: 500;
}
@media (hover: hover) {
.form-button:hover {
background: #1565c0;
}
}
@media (width >= 768px) {
.form-button {
width: auto;
}
}
On mobile, input attributes decide which keyboard appears. Use type and inputmode to bring up the right keypad, autocomplete for autofill, and enterkeyhint to set the label on the keyboard's Enter key. Using type="number" for phone numbers or verification codes means maxlength is ignored and the mouse wheel can change the value, so use inputmode="numeric" instead.
<label class="form-label" for="phone">Phone number</label>
<input class="form-input" id="phone" name="phone" type="tel" autocomplete="tel">
<label class="form-label" for="otp">Verification code</label>
<input class="form-input" id="otp" name="otp" type="text"
inputmode="numeric" autocomplete="one-time-code" maxlength="6">
<label class="form-label" for="email">Email</label>
<input class="form-input" id="email" name="email" type="email" autocomplete="email">
<label class="form-label" for="q">Search</label>
<input class="form-input" id="q" name="q" type="search" enterkeyhint="search">
You can check color combinations with the contrast checker.
10. Images and LCP
LCP (Largest Contentful Paint), one of the key mobile performance metrics, is the time it takes to render the largest element on the first screen — usually the hero image at the top. Don't lazy-load the hero image; fetch it first with fetchpriority="high", and serve a file sized for the screen with srcset. The width and height attributes reserve space before the image arrives.
<img class="hero-image"
src="hero-800.jpg"
srcset="hero-480.jpg 480w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="100vw"
width="1600" height="900"
alt="(describe what the image shows)"
fetchpriority="high">
Instead of fixing the height in px for each screen, set a ratio with aspect-ratio and the image won't distort at any width. The aspect ratio calculator handles the ratio math.
.hero-image {
display: block;
width: 100%;
height: auto;
aspect-ratio: 4 / 3;
object-fit: cover;
}
@media (width >= 768px) {
.hero-image { aspect-ratio: 16 / 9; }
}
@media (width >= 1024px) {
.hero-image { aspect-ratio: 21 / 9; }
}
Lazy-load images below the fold with the browser's built-in loading="lazy". Setting opacity: 0 with JavaScript and fading images in is not recommended — if the script fails, the images never appear. For details on srcset, sizes and <picture>, see the responsive images section of responsive web design basics.
<img src="card.jpg" width="600" height="400" alt="(describe what the image shows)"
loading="lazy" decoding="async">
11. Performance checks
- Image formats: use AVIF or WebP, with JPEG as a fallback via
<picture>. - Critical CSS: inline only the CSS the first screen needs and load the rest later.
- INP: the responsiveness metric that replaced FID in March 2024. Break up long JavaScript tasks that delay the screen from updating after a tap.
- Scroll and touch events: pass the passive option, as in
addEventListener('touchstart', fn, { passive: true }), so scrolling isn't blocked. - Measurement: run PageSpeed Insights and Lighthouse with mobile settings, and reproduce slow conditions with DevTools network throttling (Slow 4G).
12. Choosing units and the converter
In mobile-first CSS, choosing a unit starts with deciding what should scale with the screen width. Font sizes and padding inside components get rem (PX to REM converter), layout values that should stretch with the screen get vw (PX to VW converter), and values that should transition smoothly between the two get clamp(). How to set font sizes with clamp() is covered in the fluid typography section of responsive web design basics.
13. Summary
Mobile first is a writing order: the narrow screen is the base, and you add only what's needed as it widens. Stick to that order and you write less undo code, and you take care of the screen mobile users see first right from the start. For the syntax of the tools used here — media queries, container queries, responsive images — see responsive web design basics.