Mobile-First CSS: Is It Time for a Rethink?
alistapart.com
alistapart.com
width: min(calc(100wv - 2rem), 80rem)
Instead of resizing text for different breakpoints, use font-size: clamp(2rem, 4vw + 1rem, 3rem);
etc. No media queries needed. I also find it easier to reason about since you don’t have different rules applying in different circumstances.never let the font size be smaller than 2rem
never let the font size be bigger than 3rem
the middle is then your fluid calculation of what the size should be between those two sizes.
since min, max, and clamp can be used in every property that can use calc it follows that lots of media query code can be replaced.
Finally when using media queries alone you will always run into some situation where something does not look that good if you are resizing your window and you are still in breakpoint 1 but you have not hit breakpoint 2 yet, you can of course fix that by adding a breakpoint in between these two, but the same problem presents itself multiple times.
Media queries alone will never give a 100% fluid experience.
Doesn't look like they're supported by Chrome yet, but they're there in Firefox and Safari.
There's also `scrollbar-gutter: stable` to always reserve space for the scrollbar. It's not supported in Safari, but macOS uses overlay scrollbars by default anyway so it doesn't really matter.
body {
--scrollbar-width: 0px;
--320-to-1440: ( 100vw - ( 320px + var( --scrollbar-width ) ) ) / 1120;
}
h1 {
font-size: clamp( 20px, calc( 20px + var( --320-to-1440 ) * 10 ), 30px );
}
While accounting for the scrollbar width via JavaScript. document.body.style.setProperty(
'--scrollbar-width',
`${ window.innerWidth - document.documentElement.clientWidth }px`
);
This will be easier when user-defined functions arrive.Trying to impart this way of thinking was one of my biggest failures managing a product engineering team. I’m curious if others have successfully gotten teams of designers and engineers to stop worrying about mobile/tablet/desktop first and instead think in terms of an infinite number of viewport configurations?
To be more specific about my problems: in my experience with designers is that they want fixed viewport sizes so they can come up with “pixel perfect” designs. The tools they used tended to have fixed canvas sizes and dimensions. For “responsive first” designs, you need something that’s constantly changing the viewport size.
Engineers struggled because they wanted specs to implement against that were also pixel perfect. From a tooling perspective, I noticed most use Chrome and NOT the responsive view. I don’t know how anybody can build a responsive web application without being exclusively in responsive view and constantly resizing the viewport.
If I had to generalize the struggle between design & engineering, they felt they couldn’t meet the requirements given to them in a reasonable amount of time unless “mobile” and “desktop” were scoped separately.
Sadly, no, despite my best efforts across multiple teams I've led in the last few years.
If someone manages to crack this, I'll hail them as a hero.
This is pretty much the "original sin" of the web. Every anti-accessible inflexible technology we've had to struggle with for 20+ years has been driven by this. Flash sites. Text "baked" into images. User-agent bans.
If I had to guess, they think the amount of people who would actually use something that falls outside of the standard desktop/mobile/tablet presets is very low and not worth the amount of extra effort it would take to implement.
In other words, this is one of those things the boss says they want but realistically it's way down the list of real world priorities and if nobody does it, nobody gets in trouble for not doing it, so it wasn't actually important in the first place. Whereas if something's wrong on the desktop or mobile designs, they'll hear about it and hear about it quickly, so they know that IS important.
The reason it was "way down the list of real world priorities" is because it was perceived as an impossible requirement to meet in terms of effort. My hopes is that it would be met as an innocuous side effect of making the thing work in a mobile and desktop viewport, but for the reasons I speculated above, it wasn't, and hence got dropped down on the list to the point where it wasn't worth the battle needed to ship a truly responsive design.
I can't just blame individuals though -- its easy to say, "oh, well they just weren't smart or motivated enough", but that's not the case. In the end I'm left thinking that the tooling, documentation, and ecosystem surrounding it simply didn't support making this "an easy requirement".
Your story isnt important enough to deal with God awful UX.
javascript:let i, elements = document.querySelectorAll('body *'); for (i = 0; i < elements.length; i++) { if(getComputedStyle(elements[i]).position === 'fixed' || getComputedStyle(elements[i]).position === 'sticky'){ elements[i].parentNode.removeChild(elements[i]); } }
That zaps fixed and sticky elements, so I can actually see the content. (I like the Vivaldi browser because I can assign keys to many things.)
Lots of “gotcha” questions being asked that are symptoms of less-than-thorough responsive and multi-device development, I have to say…
Resolution can be targeted with other media queries, which should be useful for other high-resolution devices. And separate from this you should target touchscreens with a `pointer: coarse` query.
This is exactly why you should not think in terms of mobile, tablet, desktop, but instead in terms of features of the output media.
I've tried using tailwind and semantic ui very briefly, but wasn't aware they provided any responsive-oriented classes.
eg. you can give an element a class of "sm:p-2 lg:p-4 xl:p-8" to give different padding sizes.
the docs are here: https://tailwindcss.com/docs/responsive-design
As a dev, I've never embraced the idea. It's over-compensating for the days when mobile was an afterthought. If all you've done is make desktop the afterthought, you haven't solved anything.
The sites I've worked on - big, small, popular, unpopular, all have significant numbers of important desktop and laptop visitors (important as in low bounce rate, high engagement). I spend time building interesting things for their screens because I want their return visits.
I prefer "accessible first" or "mobile-friendly". Not that I use catch phrases. Nothing needs to be "first". You don't need an ideology, you need results.
> "no one wants to spend their time retrofitting a desktop-centric site to work on mobile devices!"
That sounds like a problem from 15 years ago. Today it's reasonable to develop the desktop layout and mobile response at the same time. We have better tools such as flexbox to get mobile layouts working out of the gate.
All too often I come across sites that were designed on desktop and mobile was an afterthought, and it quite clearly shows
Before making big generalized claims, you need to define "mobile device". If you're including tablets and iPads, a lot of those devices have large screens which are closer to desktop or laptop anyway.
Your "mobile first" decision to banish the top navigation to a burger menu, for example, might actually leave enough empty space which would have fitted the navigation you just sent to burger land.
"[Device]-first" isn't a thing that comes to the rescue of anyone. If anything, starting with the big-screen design, allows all possible features to be clearly realized, sorted, and refined. After which, optimization and reduction can happen for whatever small-screen channels are needed. Chipping away at the block of marble is a reasonable and sensible approach.
Starting with a full set of features and presentation is more beneficial for understanding your product goals. Mobile-first inhibits the potential and limits exploration, by enforcing a restraint at the outset.
Imagine if the classic painters used the equivalent of "mobile first", let's call it "window first", in anticipation of satisfying the 50% of people who view the artwork from outside the gallery, peering through a window. Or take it further and try "postage stamp first" because more people will buy postage stamps than visit the gallery. Each to their own as always!
Starting with a big screen design might allow “all possible features to be clearly realized, sorted, and refined” but all too often it leads to unoptimized mobile experiences because compromises are made to make the desktop experience adapt to mobile
And when 75% of the audience aren’t going to get that full experience I argue that designing a great experience for the 25% to the compromise of the 75% is a flawed approach
Laptops are not unpopular devices. People use them at work, at home, to shop, browse, everything. People love their laptops. At work, people love dual monitors etc. That won't change because humans need sufficient screen real estate to get stuff done and display documents etc.
As I said before, quality of users is also important. You should analyze your traffic to see bounce rate and spending for mobile phone vs everything else.
You seem to think that a mobile layout will be "compromised" if the design starts with the full website layout. This is old thinking. If the brief says "we want a strong website on all devices" then your designer and devs will make sure the site is great on all layouts. Otherwise they're not doing their job.
There's no universal law that says unless you design for mobile first, it will suffer. Your idea is left over from years ago when mobile phone usage was low, and mobile browsers were poor. Remember when there was a delay between pressing a button and the action, because iOS was "waiting" to see if was a double-tap or not. The compromise was built-in.
Add to that the crippled cache and memory abilities of phones, and the preference of mobile manufacturers to push people to native app stores, nobody should be surprised that mobile browsing was/is compromised by its very nature.
There’s some variation depending on the demographics e.g. sites with a large audience amongst the older population tend to have more tablet, and laptop visitors.
You can check the mobile vs desktop split for most sites using the Chrome UX Report
You say my logic is flawed but too many sites are designed with large screens in mind and then retrofitted to mobile with poor outcomes for the user experience
With that in mind, the same is true now as it has been for almost 20 years. The layout should be made of 320px columns growable to 400px, while components occupy one or more columns. If everything else is based on that foundation, it will be rare to run into special cases. Whereas the current breakpoints based around 768 and 1024 are actively working against you by creating common dead zones that you need to adjust for.
You can see the 1025-1079 dead zone in action by visiting most websites in 4K side-by-side, causing a horizontal scroll as the content overflows. That includes major sites from Google, Meta and others who really ought to know better. Since almost all of my browsing is done side-by-side, I've resolved to reducing the default browser zoom to 90%, which eliminates the horizontal overflow on most of these sites.
It's just as frustrating to use a site on a desktop that insists on a mobile layout until I make the window uncomfortably wide. (I assume this is tablet-focused design run amok.)
So, for a while now I've found it fruitful to regularly resize the browser between min and max width as I work (and sometimes vertically).
Rather than worrying about someone's device-size chart, I tend to build in more breakpoints focused on transitioning as the dimensions approach points where the content feels too crowded or too sparse, or where the UI/X affordances are creaking.
(Though this entails more of the regression testing that the source cites as a con.)
I'm fairly convinced at this point that with enough developers, the best approach is adaptive if you plan on building a web app with considerable complexity. Completely isolated development, fully custom design and code for mobile.
With an adaptive site you have the choice to include or exclude elements which may have a major performance impact on the app, including for instance: entire view/component trees, media queries, heavy JS like graphs or ads. You also get to make web-unfriendly decisions like using a custom renderer if you think it will produce a higher quality experience, like Flipboard's React Canvas from several years ago: https://engineering.flipboard.com/2015/02/mobile-web
Overall it's still very context dependent, but I would recommend for larger teams to not completely discount adaptive mobile sites if they're looking to build the highest quality mobile experiences.
>> I would recommend for larger teams to not completely discount adaptive mobile sites
Quality developers know when to use what. It's the legacy code that bites us.
Plus, the desktop design tends to be the more complex one, so the HTML will be laid out to accomodate grids/flexbox elements, etc. where necessary. for the mobile view, you usually don't have to change the HTML as much.
I might add, that there were tables and WAP before and finally the iPhone on the demand side gave rise to the mobile revolution. Or put another way: before iPhone there wasn't really a reason to design for mobile, only desktop.
Also in the beginning dev and designers focussed on landing pages and websites, not complex SaaS applications, to be mobile first.
For example, in the beginning it was coined "responsive web design": https://alistapart.com/article/responsive-web-design/
I always rejected responsive web design, because it was in my point of view - lacking browser support of flex/grid! - an overly theoretical approach without any practical merits. Data on projects I worked showed, that a hybrid approach with some responsive sugar for edge cases did the deal way better (hybrid approach).
I remember, when Smashing Magazine went on the hype train and redesigned their website to be responsive, the goal was that it would react to every pixel change in the browser with a consistent layout - a task so tough and so irrelevant, even the designer afterwards conceded. (diminishing returns)
Even today, I prefer to think in views instead of "mobile first". It is not about adding or substracting features depending on the screen size, it is more about fulfilling tasks given a certain screen resolution. Some SaaS apps do not make sense on a phone. That's why I rejected the term "mobile first" in the first place.
Nevertheless, I recognize all the hard work by all devs and designers they pure in developing apps for different screen (re)solutions. No matter what you call the paradigm, it is tough and challenging. Keeping an eye on the user and the data, helped me stay sane.
Maybe the example at the end wasn't clear enough that you should still have a base CSS that encompasses the common parts?
In my experience, browser side javascript is always the biggest render blocking issue with all the on-the-fly dom manipulation it might entail. Viewport specific css usually adds some kilobytes, a few hundred at most but the browser tends to be relatively swift about those as long as you don't use a lot of stuff like transitions or wildcard selectors on a broad spectrum.
webapps suck on mobile when the builders don’t use them on mobile.
One one hand, I think this makes total sense from a technical perspective. Don't load code you don't need, right? That's what we're doing with JS in SPA frameworks like Next.js, NuxtJS, SvelteKit, etc.
The problem with splitting code like this is it isn't aligned with the developer's thought process. It's a step back from where we've been headed over the past decade of web development. Current major JS frameworks like React and Vue are basically JS in HTML and newer CSS frameworks like Tailwind are basically CSS in HTML. The reason these frameworks have gained so much traction is because they don't break the developer's concentration.
The whole "separation of concerns" mantra from a decade ago was misguided. Web developers don't naturally think about code in terms of "let's do the HTML, then the CSS, then the JS". They think, "let's make a button that does something when I click it". And with the current frameworks, that button is often a single (albeit long) line of code. Semantics, layout, animation, interaction—everything. A decade ago, that button was spread across three files in three separate locations.
The problem with the approach outlined in the article is developers don't think, "let's do mobile, then tablet, then desktop". They think, "this box is always blue and it lives in a column that's a third the width of the container on larger screens".
The reason the code splitting works in the JS frameworks I mentioned earlier is because it's completely automatic. Unless this code splitting requires zero effort on behalf of the developer, it's never going to catch on. The DX just isn't there, even if it is certainly more performant.
Not all of it, sure, but more than our profession likes to make it out to be.
I’ve challenged this on HN before, but I’ll take a different approach. Can you describe a scenario where these fundamentally can’t be achieved with the same markup? I’ve never encountered one, in over a decade of developing responsive apps and sites.
They maybe can be done with the same markup, but it'd be much more work and some duplication, too. And of course it's absolutely unacceptable to redesign your UIs to make it easier to reuse markup — remember, developer experience doesn't matter if the end result is shit.