.TH easy-fixes 7 .SH NAME easy-fixes \- Easy (Horizontal Scrollbar) Fixes for Your Blog CSS .SH AUTHOR Artyom Bologov .SH SYNOPSIS There are narrow screen CSS problems I often email people because of. These three (and a half) fixes should be enough for most. .SH TEXT .P 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!) .P 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. .P 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 four of my life-hacks for small viewport adaptation, matching the three most problematic HTML elements: .SH Preformatted (
) Block Scrolling
.P
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:
.P
.P
.in +4n
.EX
0001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110100001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111010000101100000011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110100001011000000111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111001110011100111010000010
.EE
.in
.P
— The name of my favorite Kpop singer (hint: she’s a certified freak, ten days a week 🎵)
.P
Yeah, Binary Lambda Calculus be like that sometimes.
Believe me, I can’t read it myself, especially with this scrollbar!
.B Fix it!
.EX
.P
The fix is easy:
.P
.in +4n
.EX
pre {
overflow-x: auto;
}
.EE
.in
.P
— Fix to overflowing pre-formatted blocks
.P
This \fBoverflow-x\fP value means:
.P
* if the code block overflows (horizontally, thus the \fBx\fP,) let’s add a scrollbar to it (and not the surrounding page!)
.P
* and if it doesn’t overflow, then just proceed as regular
.P
Check out the fixed code block above 👆
.EE
.SH Images (
) and Their Width
.P
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:
.B Fix it!
.EX
.P
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:
.P
.in +4n
.EX
img {
max-width: 100%;
height: auto;
}
.EE
.in
.P
— Fix to images occupying too much horizontal space
.P
This is usually enough as a baseline.
Add your fancy image placement on top in peace!
.P
Check out the fixed image above 👆
.EE
.P
Bonus point: this seems to be a problem for videos too.
So watch your \fBmax-width\fP there too!
.SH Tables (