Mobile-First CSS: Why and How to Write It
Mobile-first means the base styles target small screens and larger screens add complexity. Learn the approach, the benefits, and the pitfalls.
By Velloreth
The approach
Mobile-first writes the base CSS for the smallest screen, then adds complexity with min-width media queries.
/* Base: mobile. No media query. */
.nav {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
/* Tablet and up: horizontal */
@media (min-width: 45rem) {
.nav {
flex-direction: row;
gap: 1.5rem;
}
}
The base styles are the mobile experience. Larger screens are an enhancement layered on top.
Why it is better
Simpler CSS
Each media query adds to the previous state. Nothing is undone.
/* Mobile-first: the cascade only adds */
.cards { grid-template-columns: 1fr; }
@media (min-width: 45rem) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 64rem) {
.cards { grid-template-columns: repeat(3, 1fr); }
}
/* Desktop-first: each query undoes the previous */
.cards { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 64rem) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 45rem) {
.cards { grid-template-columns: 1fr; }
}
Both work. The mobile-first version is easier to reason about, because reading top to bottom shows the layout growing. The desktop-first version requires holding the desktop state in mind and tracking what each query removes.
Better mobile performance
A mobile device evaluates the base styles and skips the min-width queries, because the conditions are false. With max-width, the device evaluates the desktop rules first and then overrides them.
The saving is small for a simple stylesheet and larger for a complex one. It also matters for CSS-in-JS and for stylesheets with many media queries.
Forced prioritisation
The base styles are the mobile experience, so you must decide what matters most. A desktop-first stylesheet lets you defer that decision, and the mobile experience becomes an afterthought.
This is the most valuable effect. It is a design discipline disguised as a CSS technique.
Fewer overrides
Desktop-first stylesheets accumulate unset, revert, and initial declarations as each query undoes the previous. Mobile-first rarely needs them.
A complete example
/* Base: mobile */
.page {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
padding: 1rem;
}
.sidebar {
order: 2; /* below the content on mobile */
}
.content {
order: 1;
}
.title {
font-size: 1.5rem;
}
/* 45rem and up: two columns */
@media (min-width: 45rem) {
.page {
grid-template-columns: 1fr 1fr;
gap: 1.5rem;
padding: 1.5rem;
}
.title {
font-size: 2rem;
}
}
/* 64rem and up: sidebar beside content */
@media (min-width: 64rem) {
.page {
grid-template-columns: 16rem 1fr;
gap: 2rem;
padding: 2rem;
}
.sidebar {
order: 0; /* back to the natural position */
}
.title {
font-size: 2.5rem;
}
}
Reading top to bottom shows the layout growing from one column to two to a sidebar layout. Each step is a small addition.
Content, not layout
The most important principle: the content is the same on every screen. Only the layout differs.
/* Wrong: hiding content on mobile */
@media (max-width: 45rem) {
.pricing-table { display: none; }
}
The mobile user needs the pricing information. Hiding it forces them to switch devices or leave.
/* Right: reflow it */
.pricing-table {
display: grid;
grid-template-columns: 1fr; /* stacked on mobile */
}
@media (min-width: 45rem) {
.pricing-table {
grid-template-columns: repeat(3, 1fr); /* side by side */
}
}
A table can become a stack of cards. A wide chart can scroll horizontally. A multi-column layout can become one column. The information stays.
Touch targets
Mobile-first means designing for touch, which has different constraints.
Minimum 44×44px for interactive elements, per the WCAG target size guidance.
.button {
min-height: 44px;
min-width: 44px;
padding: 0.75rem 1rem;
}
Spacing between targets. Adjacent links need enough gap that a thumb does not hit the wrong one.
No hover-only interactions. A hover menu does not work on touch. Provide a tap equivalent.
Avoid :hover for essential information. On touch devices, :hover may never fire, or may fire and stick.
Typography
Mobile-first typography starts readable and scales up.
body {
font-size: 1rem; /* 16px minimum, prevents iOS zoom on input focus */
line-height: 1.6;
}
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}
clamp() handles the scaling without breakpoints. The minimum is the mobile size, the maximum is the desktop size, and the middle term scales with the viewport.
The 16px minimum on inputs matters: iOS zooms the page when a focused input has a font size below 16px, which is disorienting.
Images
img {
max-width: 100%;
height: auto;
}
This is the baseline. For performance, use srcset and sizes so mobile downloads a smaller image.
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(min-width: 64rem) 50vw, 100vw"
alt="..."
/>
The browser picks the smallest image that fits, which matters most on mobile connections.
Pitfalls
Treating mobile as reduced. The content should be the same. Only the layout changes.
Hiding content. Usually a mistake. Reflow instead.
Hover-only interactions. Do not work on touch.
Small touch targets. Below 44px is hard to tap accurately.
Font size below 16px on inputs. Causes iOS zoom.
Assuming a fast connection. Mobile networks are slower and less reliable. Optimise images and avoid large JavaScript.
Testing only in an emulator. Emulators miss real rendering, touch behaviour, and performance.
Forgetting landscape. A phone in landscape is wider than in portrait, and the layout should still work.
Navigation on mobile
Navigation is where mobile-first design is most visible, and where desktop-first thinking causes the most trouble.
A horizontal nav bar with eight links does not fit on a 360px screen. The mobile-first approach starts with the mobile pattern and enhances it.
.nav {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.nav a {
padding: 0.75rem 1rem; /* 44px+ touch target */
border-radius: 6px;
}
@media (min-width: 48rem) {
.nav {
flex-direction: row;
gap: 1.5rem;
}
.nav a {
padding: 0.5rem 0;
}
}
The mobile version is a vertical list of full-width tap targets. The desktop version is a horizontal row. Both use the same markup, and the change is three declarations.
If you need a hamburger menu, the button should be a real button with aria-expanded, and the menu should be reachable by keyboard. A common mistake is building the toggle from a div with a click handler, which is invisible to screen readers and unreachable by keyboard.
Forms on mobile
Forms are the other place where mobile-first matters, because mobile keyboards and touch input change the constraints.
Use the right input type. It determines the keyboard shown.
<input type="email"> <!-- @ key on the keyboard -->
<input type="tel"> <!-- numeric keypad -->
<input type="number"> <!-- numeric with steppers -->
<input type="url"> <!-- / and .com keys -->
Keep font size at 16px or above. Below that, iOS zooms the page when the field is focused, which is disorienting and hard to undo.
Make fields full width on mobile. A two-column form on a 360px screen gives each field about 160px, which is too narrow for an email address.
.form {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 48rem) {
.form {
grid-template-columns: 1fr 1fr;
}
}
Use autocomplete attributes. They let password managers and browsers fill fields, which is far faster on a phone keyboard.
Practical guidance
- Write base styles for mobile, then add with
min-width. - Keep the content the same across screens; change only the layout.
- Use
clamp()for typography instead of breakpoints. - Make touch targets at least 44×44px.
- Keep input font size at 16px or above.
- Provide tap equivalents for hover interactions.
- Use
srcsetso mobile downloads smaller images. - Test on a real device, not only an emulator.
For the related guides, see responsive-design-breakpoints and css-custom-properties. For the broader context, css-flexbox-complete-guide and css-grid-complete-guide.
Quotable summary
Mobile-first writes the base CSS for the smallest screen and uses min-width media queries to add complexity as the viewport grows, so the default is mobile and larger screens are the enhancement. It produces simpler CSS because each query adds to the previous state rather than undoing it, and it improves mobile performance because small screens do not evaluate desktop rules. The most valuable effect is forced prioritisation, since the base styles are the mobile experience. The content should be the same on every screen and only the layout should differ, so hiding content on mobile is usually a mistake — reflow it instead. Design for touch with targets of at least 44×44px, no hover-only interactions, and input font sizes of 16px or above.