So it often happens that I find some blog post. A fun one. A freaky one. A profound one. A critical (in the sense of Critical Science, not everyday criticism!)
And then I see my arch nemesis: The Horizontal Scrollbar. Creeping in when no one sees, on smallest screen widths. (I have an iPhone SE and a small 13-inch laptop with x2 UI scaling.) Making the website interaction much less pretty than it might’ve been.
So I took up a torch and decided to write this up for y’all. None of us can’t know everything about making blog posts more accessible to small viewports. So let’s share the knowledge. Here are three (and a half) of my life-hacks for small viewport adaptation, matching the three most problematic HTML elements:
We programmers list a lot of code snippets in our blog posts. And, living in a post-VT100 era, we don’t usually care about line length. Which is totally fine. But it often results in long code blocks overflowing the viewport and causing horizontal scrollbars:
0001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110100001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111010000101100000011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110100001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111010000010
Yeah, Binary Lambda Calculus be like that sometimes.
Believe me, I can’t read it myself, especially with this scrollbar!
The fix is easy:
This
Check out the fixed code block above 👆
pre {
overflow-x: auto;
}
if the code block overflows (horizontally, thus the
I was unable to contact htmx website maintainers, but oh are they guilty of that. The images on htmx blog are not properly sized and “escape” the viewport regularly. Something like:
HTML images are scaled to their original width and height by default. So huge images are likely to overflow and cause dreadful scrollbars. This is solvable by capping image width to the size of its container:
img {
max-width: 100%;
height: auto;
}
This is usually enough as a baseline. Add your fancy image placement on top in peace!
Check out the fixed image above 👆
Ever visited the Space Jam website? Neither did I. But we still live with its legacy. No, not the space operas or whatever. Tables as markup.
Tables were abused for markup on the early Internet.
And we still have to deal with the suspicion that table layouting is Turing-complete.
Or at least pray the next table doesn’t destroy the page.
There’s no single-property fix to tables stretching too far.
But one solution (that always works in webdev, right?) is to wrap the table into a
And apply
I mean, this
Check out the fixed table above 👆
<div class=scrollable>
<table>
...
</table>
</div>
.scrollable {
overflow-x: auto;
}
Bonus post-publication section! In case you care about accessibility and do large device-adapted fonts.
font-size: large; font-size: max(1em, 16px, 1rem);
You might inadvertently destroy the viewport with your vocabulary. Well, if yours is fancy and includes German compound words. (Neunmilliardeneinhundertzweiundneunzigmillionensechshunderteinunddreißigtausendsiebenhundertsiebzigfache 👻)
The problem with long no-delimiter words: they are not broken by default in HTML.
So using longer words can overflow the viewport and cause the miserable horizontal scrollbar.
The solution is more involved than a CSS property, unfortunately: use
Another option is using a
I mean, there’s a CSS solution too, but it’s not pretty to language semantics:
p {
overflow-wrap: anywhere;
}
These are three (+ a half) problems I’m hoping you viscerally felt with me just now. And the solutions I personally use and suggest to fellow bloggers. Hopefully we’ll come out of it with less tolerance to horizontal scrollbars. Test your website on smaller screens and tread carefully 🩷