58 bytes of css to look great nearly everywhere
apr 6, 2019
when making this website, i wanted a simple, reasonable way to make it
look good on most displays. not counting any minimization techniques,
the following 58 bytes worked well for me:
main {
max-width: 38rem;
padding: 2rem;
margin: auto;
}
let's break this down.
max-width: 38rem
it appears that the default font size for most browsers is 16px, so
38rem is 608px. supporting 600px displays at a minimum seems
reasonable.
padding: 2rem
if the display's width goes under 38rem, then this padding keeps things
looking pretty good until around 256px. while this may seem optional,
it actually hits two birds with one stone - the padding also provides
sorely-needed top and bottom whitespace.
margin: auto
this is really all that is needed to center the page, because main is a
block element under semantic html5.
a key insight: it took me a surprising number of iterations to arrive
at this point. perhaps that speaks to the fact that i know nothing
about "modern" web development, or, as i'm more inclined to believe,
just how hard it is to keep it simple in a world of complication.
__________________________________________________________________
update: following [1]some discussion, i've since changed the padding to
1.5rem for a happier compromise between mobile and desktop displays.
update 2: the ch unit was brought to my attention [2]here, and i quite
like it! i've since changed to 70ch/2ch, which looks nearly the same
with 2 less bytes, except that the padding is a little bit smaller (a
good thing for mobile).
References
1.
https://news.ycombinator.com/item?id=19607169
2.
https://www.reddit.com/r/css/comments/bb73cw/58_bytes_of_css_to_look_great_nearly_everywhere/ekj8yhm/