Fixing Mobile Page Layouts
Some webpages are too damn hard to read on my phone, like this one:
The font is tiny, the page is wide, and the text is wrapping at a weird place. I must read these on my phone like a PDF: scrolling in and panning along each line.01
This post is my guide to fixing these issues. Itās aimed at folks like me who arenāt quite web developers, but blog or run small sites that could use a little fine-tuning.
Contents
-
How to Fix Common Problems ā Four tricks I use to fix pages.
-
Walkthroughs ā Tutorials of repairing pages.
Disclaimer: Iām not a web developer. I have no idea what Iām doing, really.
How to Fix Common Problems
1. Set the Viewport
Does the page look like a desktop site zoomed way out? You might need to tell web browsers to render the web page at the actual deviceās width.02 Add this HTML tag:
<meta
name="viewport"
content="width=device-width, initial-scale=1"
>
In action, this makes the following change:
Now the text renders into a more reasonable area. This makes the font look bigger and the lines wrap much earlier. But the page still has a problem thatās making the text only take up half the page. Something is too wide.
2. Scroll Over-Wide Elements
Over-wide elements are easy to miss even from your phone, because your phone might mask them with a horizontal scroll bar. If you accidentally scroll right, though, youāll fly off the content.
Once you notice this happens, you can pinch to zoom out and survey the mess. Some element is probably too wide. Find the culprit.
Ah ha. In this case, itās text in <pre>
blocks. We can add some simple CSS to limit their width.
pre {
max-width: 100%;
overflow-x: scroll;
}
This solves it. Hereās the before and after.
No more scroll bar, and the width is correct. This section is a bit weird because of overly wide indentation. Hereās the same result on body text.
What About Bigger Screens?
This article is only about making pages look better on mobile. I try not to break anything for larger screens, but naively applying some of these changes will make desktop layouts more cramped.
Center: For larger screens, we can keep the same strategy as with mobile displays and clip everything to the body region, but wide elements will still need to scroll.
Right: If we instead let wide elements spill outside the body area when there is room on larger displays, they wonāt need to be scrolled.
When thereās more screen space, it probably makes sense to let larger elements take up more room rather than clipping them and making them scroll horizontally. Consider housing wide content in containers that can break out from the body text.
3. Typographic Cleanup
There are a few bits of low hanging fruit that will make a page go from functional to comfortably readable. (Donāt worry, Iām not going to make the text annoyingly light gray or anything.)
-
Margins. Content should have a bit of breathing room, but not waste real estate.
-
Font size. Text be readable without squinting, but not so big to cause short line lengths.
-
Line height. Picking around
1.5
is a standard typography trick for body text and helps with readability.
We can apply all these changes to our running example.
4. Test on Your Actual Phone
This last trick is the most important and also the easiest to skip. Believe me, I forget to do it, too. But thereās no getting around it.03
Yes, test it using your browserās pretend Iām a phone mode first. But to this day, Iāve never made a page where I didnāt find something to change when I actually looked at it on my phone. Itās easier to recognize bad proportions on the physical device you glue to your poor eyeballs all day.
Furthermore, sometimes the browser simulators are just plain wrong. In fact, our running example is exactly one such case. I describe this more in the walkthrough below.
Walkthroughs
Click on any picture to jump to the walkthrough for fixing the mobile layout on that page.
Disclaimer: Iām nitpicking microscopic stylistic issues on these ages for the sake of illustration. Their actual content is largely fascinating, technical, and well-written. I humbly offer these example fixes because I like what the authors are doing and would like to enjoy it even more on my phone.
Git Best Practices
This is the site that I used as the running example for the first section. This page is called Commit Often, Perfect Later, Publish Once: Git Best Practices. The page looks like this on my phone:
Now, I canāt be too hard on the designer.04 First, the page is from 2012āancient Internet history.
Second, itās actually kind of hard to tell this was going to happen. Normally, I would guess that they simply didnāt test it using developer tools. But if you ask Chrome and Safari what the page should look like on an iPhone X, they both say, ājust fine:ā
This is annoying, because it means we have to debug on the phone itself. No problem, just download the page, whip up a python -m http.server
, find the laptopās local IP address with ifconfig | grep 192
, and point the olā telephone to 192.168.0.xxx:8000
. Weāre in.
Fixing it
These fixes are exactly the ones I describe in detail in the first section, so Iāll run through them quickly.
First, we adjust the viewport (fix #1).
<meta
name="viewport"
content="width=device-width, initial-scale=1"
>
Then, we stop <pre>
-formatted blocks from stretching out the page width (fix #2).
pre {
max-width: 100%;
overflow-x: scroll;
}
For the final tweaks (fix #3), we apply:
-
Narrower margins. We donāt need that much of a buffer on our phones. Iāll take it from
3em
ā1em
.05 -
Bigger font size. Itās just in the squint-y range for me.
16px
ā18px
. -
Taller line height. The usual
normal
ā1.5
.
Apply them all, hereās the before/after.
Hooray, super readable!
Extra credit: typography
If we wanted to, one last thing Iād tweak is the indentation, which is so comically large as to end up taking up nearly half the screen width (really only glaring on an actual phone, AKA (fix #4):
ā¦ but eh, Iād say weāve done enough for one evening. Letās call it an exercise for the reader.
Poissonās Equation
This page is called Poissonās Equation is the Most Powerful Tool not yet in your Toolbox. The pageās viewport seems good, but you can accidentally scroll to the right, revealing a bigger page and likely overly wide elements (fix #2).
If we scroll down, we can see the worst offenders are, again, over-wide <pre>
regions for code. They are already the correct width, but when their content overflows, it spills out.
We can fix these,
pre {
overflow-x: scroll;
}
ā¦ but rendered math is still an issue. Like the <pre>
chunks, theyāre also set to a good width, but spill out.
The math chunks are all in <div>
s with the CSS class math
, so we can just add another selector to our rule:
pre, .math {
overflow-x: scroll;
}
And with that, weāve tamed the over-wide elements and the page displays nicely.
Extra credit: code and scrollbars
If I were to add finishing touches, the two things Iād tweak would be the code and math.
The font size for code blocks is bigger than it needs to be (fix #3) and lacks syntax highlighting. If we make those two changes and then reduce the indentation from four to two spaces, we can even fit all the code without any scrollingāwe just need to wrap two of the comments.
Aside: This was another place where it was instrumental to check on my physical phone (fix #4). Only there did I notice the code font was so large, and only there could I verify that it was still readable after reducing the font size.
The second aspect Iād improve is the math displayāor more generally, the remaining elements that are have their own horizontal scroll bars because of their width.
When possible, itās nice to have your content fit inside the display so people donāt have to scroll horizontally to see it. But itās not always worth the effort to make that happen. I think typeset math is one of those occasions. Itās enough work just to make LaTeX display your math nicely, let alone making a version that fits on portrait-sized mobile screen.
But when we do have content that needs scrolling, I think itās good to show people that scrolling is available. In other words, to show them when an element has the scrolling affordance. Showing content thatās cut off is a hint, but this isnāt reliable unless calculated explicitly, as the content might be cut off in a way that doesnāt reveal thereās more to see.
As I mentioned above, Appleās browser seems to go out of its way to hide scroll bars. Even when they appear because you accidentally found something scrollable, they vanish a bit later. Can we make sure that our extra wide content has scroll bars them?
In short, I couldnāt robustly figure this out. Using the ::-webkit-scrollbar
CSS selector, I could get really big chonky ones to always appear on Chrome, but not using Safari on my phone. Mozilla says this feature isnāt standardized yet, but that it should work on Safari and all iOS browsers š¤·āāļø
F#
This page is called F# Is The Best Coding Language Today. Once again, a good viewport, but an over-wide <pre>
code block. Jeez, what is it with these code blocks?
Two oddities with this one:
-
Not only is the code overly wide, but the left part is also cut off and canāt be revealed. Not sure how that one happened.
-
The page becomes a broken, un-styled mess when I download it to make tweaks. Iām guessing itās blocking some CSS from being loaded.
Because of 2., I only made minimal tweaks in the browserās developer tools to fix things (fix #2).
/* Attached these to the <figure> element,
the literal root of figurative evil. */
max-width: 100%;
/* Once again, you'd want to tweak font
sizes for desktop vs mobile. */
font-size: 14px;
This fixes the page and makes the code readable. Hooray!
Extra credit
Nothing else major here, but I would wrap the comments in the code earlier so we could get rid of horizontal scrolling entirely. The URL, which would be hard to wrap, could simply be moved outside of the code.
Hydroponic
This page is called Automated Hydroponic System Build. In a crazy turn of events, šØ itās not code in a <pre>
block this time! šØ This page actually contains pretty huge chunks of code, but they all obey the body width. Whatās the issue, then?
An overly long URL, yeeted into the comments by none other than the author himself. Bummer.
In fact, thereās a different comment from another person that includes a URL that would also stretch the page (fix #2). This means that any commenter can break the mobile page layout š±
I checked out the source, and saw each comment has class="comment-body"
. So if we simply add the CSS:
.comment-body {
overflow-wrap: break-word
}
ā¦ then everything is groovy.
Diffusion Models
This page is called What are Diffusion Models? This time, itās pesky math again, stretching a good viewport too wide.
We can tell MathJax to stay in its lane (fix #2) by adding the CSS:
.MathJax_Display {
overflow-x: scroll;
}
ā¦ and the page is back in its box.
Footnotes
In hindsight, Safariās Reader View almost completely solves this (for web pages, not PDFs). So I should just use that more. But it does mess up content on some pages, which you wonāt know is happening while youāre reading it, and, you know, *waves hands* fixing stuff is fun etc. ā©ļø
āActualā deviceās width is already a simplification, because screens have pretend pixels (e.g., āoh hey, Iām 375 x 812ā) and actual pixels (e.g., āshh Iām actually 1125 x 2436 lolā). As usual, to read way more than you wanted to about this check out Mozillaās docs on the viewport meta tag. ā©ļø
I was in vague denial that everyone who visited my website would be on a desktop. Then I added some basic analytics. Nope, mostly phones. ā©ļø
I was writing āauthorā until I realized, oh yeah, this is a design issue, and, like it or not, self-publishing on the web a designer of us all makes. ā©ļø
Keep in mind for a real project weād probably have the measurements change based on the screen size using CSS media queries. ā©ļø