Desktop-First Development
By the end of this lesson you will be able to read and write a desktop-first stylesheet with max-width media queries, decide when desktop first is a sensible choice, and convert an old desktop-first stylesheet into mobile-first CSS one step at a time.
You should already know the mobile-first approach from Mobile-First Development. Desktop first is its mirror image. You will meet it often in real jobs, because a lot of existing websites were written this way.
What desktop first looks like
In desktop-first CSS, the base styles (the ones outside any media query) describe the wide layout for a laptop or desktop screen. Then max-width media queries change that layout for smaller screens. @media (max-width: 40rem) means "apply these rules when the viewport is at most 40rem wide". That is 640px with the default text size.
/* Base: the desktop layout */
.layout { display: grid; grid-template-columns: 16rem 1fr; }
.sidebar { border-right: 1px solid #ddd; }
/* Smaller screens: undo the sidebar layout */
@media (max-width: 40rem) {
.layout { grid-template-columns: 1fr; }
.sidebar { border-right: 0; border-bottom: 1px solid #ddd; }
}Look at the words inside the query: "undo", "reset", "set back to 0". That is the pattern of desktop first. The small-screen rules mostly take things away.
A good comparison is a big wedding thali. Desktop first serves the full 20-dish thali to everyone and then removes dishes for the guest who wanted a light meal. Mobile first serves dal, rice and roti, then adds dishes for guests who want more. Both feed everyone, but one involves a lot more taking away.
How max-width queries stack
With max-width, the smaller the number, the fewer screens it matches. A 25rem phone matches both max-width: 40rem and max-width: 30rem. To let the smaller query win, write the queries from largest to smallest.
| Viewport width | Rules that apply |
|---|---|
| Wider than 40rem | Base (desktop) styles only |
| 30rem to 40rem | Base + 40rem query |
| 30rem and below | Base + 40rem query + 30rem query |
Step 1: a desktop-first admin screen
Here is a small admin screen for a GST billing app used by a shop in Jaipur. It is written desktop first, with a corner badge that shows which rules are active.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Desktop-first admin</title>
<style>
/* Base: desktop layout */
body { margin: 0; font-family: system-ui, sans-serif; }
.layout { display: grid; grid-template-columns: 12rem 1fr; min-height: 100vh; }
.side { background: #1e3a8a; color: #fff; padding: 1rem; }
.side a { display: block; color: #fff; padding: 6px 0; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; padding: 1rem; }
.stat { padding: 1rem; background: #eff6ff; border-radius: 8px; }
body::after { content: "Desktop (base)"; position: fixed; bottom: 8px; right: 8px;
background: #111; color: #fff; padding: 4px 10px; border-radius: 999px; font-size: 0.8rem; }
/* Up to 40rem: remove the sidebar column */
@media (max-width: 40rem) {
.layout { grid-template-columns: 1fr; min-height: 0; }
.side { display: flex; gap: 1rem; flex-wrap: wrap; }
.side a { padding: 0; }
.stats { grid-template-columns: 1fr 1fr; }
body::after { content: "max-width 40rem"; }
}
/* Up to 30rem: one column of stats */
@media (max-width: 30rem) {
.stats { grid-template-columns: 1fr; }
body::after { content: "max-width 30rem"; }
}
</style>
</head>
<body>
<div class="layout">
<nav class="side"><a href="#">Invoices</a><a href="#">Customers</a><a href="#">GST returns</a></nav>
<main class="stats">
<div class="stat">Today's sales<br><strong>₹48,300</strong></div>
<div class="stat">Invoices<br><strong>37</strong></div>
<div class="stat">GST collected<br><strong>₹7,370</strong></div>
</main>
</div>
</body>
</html>Make the browser window wider and narrower; the result pane follows it. Wide: a blue sidebar and three stat boxes in a row. At 40rem and below the sidebar becomes a row of links on top and the stats form two columns. At 30rem and below the stats stack. Notice how many lines inside the queries only reset values: min-height: 0, padding: 0, a new display.
When desktop first is reasonable
Mobile first is the better default for new public websites. Desktop first still makes sense in a few cases:
- Retrofitting a legacy site. A college results portal built in 2012 for desktops may have thousands of lines of CSS. Rewriting everything is risky. Adding a few
max-widthqueries to make it usable on phones is a fast, safe first step. - Internal admin tools. An inventory dashboard used on office computers all day can be designed for wide screens first. It should still work on a phone for the manager checking stock from home, but the main users sit at desks.
- Small fixes inside a mobile-first sheet. Something that exists only on phones, such as a sticky "Call now" bar, can sit in one
max-widthquery. That is not desktop first; it is a targeted exception.
The costs of desktop first
- Phones do extra work. A phone applies the desktop rules and then the overrides on top. On a budget phone that is wasted effort.
- Overrides pile up. Every desktop feature needs a matching reset. Over the years, the small-screen queries grow long and fragile.
- Boundary overlap.
max-width: 40remmatches widths up to and including 40rem, andmin-width: 40remmatches 40rem and above. If one sheet mixes both, at exactly 40rem both sets apply. Old code worked around this with odd values such asmax-width: 39.99rem. The range syntax(width < 40rem)says "strictly less than" and solves it cleanly. Media Queries covers the syntax.
Step 2: convert to mobile first
Converting is mechanical if you go step by step. Use the admin screen above.
- List each max-width query and what it changes. The 40rem query changes the layout, the sidebar and the stats. The 30rem query changes only the stats.
- Move the smallest-screen values into the base. One column layout, links in a wrapping row, one column of stats.
- Move the desktop values into min-width queries. The two-column stats appear above 30rem, the sidebar layout above 40rem.
- Delete resets that are no longer needed.
min-height: 0andpadding: 0existed only to undo desktop values. In the base, those desktop values were never set. - Order the queries from smallest to largest.
- Test at each width and in between. Drag slowly across each breakpoint and check that nothing jumps or overlaps.
A practical tip: the new min-width value usually equals the old max-width value. Strictly, the old 40rem query also covered exactly 40rem, but a single pixel rarely matters. If it does, keep the old boundary with the range syntax, such as (width > 40rem).
Here is the converted result. It looks the same at every width, but the base is shorter and nothing is undone.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Converted to mobile first</title>
<style>
/* Base: phone layout */
body { margin: 0; font-family: system-ui, sans-serif; }
.side { background: #1e3a8a; color: #fff; padding: 1rem; display: flex; gap: 1rem; flex-wrap: wrap; }
.side a { color: #fff; }
.stats { display: grid; gap: 1rem; padding: 1rem; }
.stat { padding: 1rem; background: #eff6ff; border-radius: 8px; }
body::after { content: "Base (phone)"; position: fixed; bottom: 8px; right: 8px;
background: #111; color: #fff; padding: 4px 10px; border-radius: 999px; font-size: 0.8rem; }
@media (min-width: 30rem) {
.stats { grid-template-columns: 1fr 1fr; }
body::after { content: "min-width 30rem"; }
}
@media (min-width: 40rem) {
.layout { display: grid; grid-template-columns: 12rem 1fr; min-height: 100vh; }
.side { display: block; }
.side a { display: block; padding: 6px 0; }
.stats { grid-template-columns: repeat(3, 1fr); }
body::after { content: "min-width 40rem"; }
}
</style>
</head>
<body>
<div class="layout">
<nav class="side"><a href="#">Invoices</a><a href="#">Customers</a><a href="#">GST returns</a></nav>
<main class="stats">
<div class="stat">Today's sales<br><strong>₹48,300</strong></div>
<div class="stat">Invoices<br><strong>37</strong></div>
<div class="stat">GST collected<br><strong>₹7,370</strong></div>
</main>
</div>
</body>
</html>Resize the window again and compare with the first example. The look matches, but the phone now reads only the short base rules.
Common mistakes
- max-width queries in the wrong order. In desktop first, the largest
max-widthcomes first. - Mixing min-width and max-width with the same number. Both match at that exact width, so rules collide. Use the range syntax or pick one direction.
- Hiding features instead of adapting them.
display: noneon the sidebar removes the navigation for phone users. Turn it into a row or a menu instead. - Converting without testing in between. A layout can look right at 320px and at 1280px but break at 700px.
Interview questions
1. What is the difference between mobile-first and desktop-first CSS?
Mobile first puts small-screen styles in the base and adds layout with min-width queries. Desktop first puts the wide layout in the base and adapts it for small screens with max-width queries. Mobile first usually adds; desktop first usually undoes.
2. When would you choose desktop first today?
When making an existing desktop-only site usable on phones without a risky rewrite, or for an internal tool whose users mainly work on desktops. For a new public site, mobile first is normally the better choice.
3. In what order should max-width queries be written, and why?
From the largest value to the smallest. A small screen matches all of them, and when matching rules have equal specificity the later one wins, so the smallest query must come last.
4. What problem can happen when min-width and max-width use the same value?
Both queries match at exactly that width because both conditions include the boundary. Rules from both blocks apply and can conflict. The range syntax, for example (width < 40rem) with (width >= 40rem), makes the ranges meet without overlapping.
5. How would you convert a desktop-first stylesheet to mobile first?
Move the smallest-screen values into the base, move the desktop values into min-width queries, delete resets that only undid desktop styles, order the queries from small to large, and test across every width. Doing it one component at a time keeps the change safe.
Key points
- Desktop first puts the wide layout in the base and adapts it with
max-widthqueries. - Write
max-widthqueries from largest to smallest. - It is reasonable for retrofitting legacy desktop sites and for desktop-heavy admin tools.
- Its costs are extra work for phones, long lists of resets and boundary overlaps.
- To convert: smallest values into the base, desktop values into
min-widthqueries, delete resets, reorder, test. - An occasional
max-widthquery for a phone-only feature is fine inside a mobile-first sheet.
Exercise
Convert this desktop-first CSS for a Lucknow coaching centre's course list to mobile first. Keep the same look at every width and remove any declaration that only undoes another one.
.courses { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; }
.course { padding: 1.5rem; text-align: center; }
@media (max-width: 50rem) {
.courses { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 32rem) {
.courses { grid-template-columns: 1fr; }
.course { padding: 1rem; text-align: left; }
}Show solution
.courses { display: grid; gap: 1rem; }
.course { padding: 1rem; text-align: left; }
@media (min-width: 32rem) {
.courses { grid-template-columns: 1fr 1fr; }
.course { padding: 1.5rem; text-align: center; }
}
@media (min-width: 50rem) {
.courses { grid-template-columns: repeat(4, 1fr); }
}The phone values (one column, small padding, left text) are now the base, so the explicit 1fr reset is gone. The bigger layouts are added from small to large. Only the exact boundary widths of 32rem and 50rem behave slightly differently; use (width > 32rem) and (width > 50rem) if you must match them exactly.
Quick Quiz
1. Which query type does desktop-first CSS mainly use?
Desktop first starts wide and adapts downwards with max-width (or width <=) queries.
2. In a desktop-first stylesheet, which query should come last?
Small screens match every max-width query, so the smallest one must come last to win.
3. Which is a reasonable use of desktop first?
Retrofitting legacy desktop code is where desktop first still makes sense.
4. At a viewport of exactly 40rem, which of these match: (max-width: 40rem) and (min-width: 40rem)?
Both conditions include the boundary value, which is why mixing them causes overlap.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
