Skip to content

Instantly share code, notes, and snippets.

@craig-m-unsw
Forked from JoeyBurzynski/55-bytes-of-css.md
Last active December 15, 2022 06:14
Show Gist options
  • Save craig-m-unsw/d5a2dff562826515bc2c35e0d10f3f96 to your computer and use it in GitHub Desktop.
Save craig-m-unsw/d5a2dff562826515bc2c35e0d10f3f96 to your computer and use it in GitHub Desktop.
58 bytes of css to look great nearly everywhere

58 bytes of CSS to look great nearly everywhere

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 1: following 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, 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).

100 Bytes of CSS to look great everywhere (enhanced version)

This should be simple drop-in css to look good on most displays:

html {
  max-width: 70ch;
  padding: 3em 1em;
  margin: auto;
  line-height: 1.75;
  font-size: 1.25em;
}

Let's break this down. I've adapted the original text with my own commentary.

max-width: 70ch

the "readable range" is usually 60-80 character widths, and CSS lets you express that directly with the ch unit.

padding: 3em 1em

If the display's width goes under the max-width set above, then this padding prevents edge-to-edge text on mobile. We use 3em to provide top/bottom whitespace.

margin: auto

This is really all that is needed to center the page - applied on html, because Dan's site doesnt have a semantic

tag and is more likely to exist in most sites. That the top tag centers itself relative to nothing is unintuitive, but thats how browsers do.

line-height: 1.75

Spacing between the lines to help increase visual clarity. Always leave line height unitless because reasons.

font-size: 1.5em

I've noticed that recent design trends and screen sizes have tended toward bigger font sizes. Or maybe I'm getting old. Prefer em or rem over px if you want to let users scale it.

You can use :root instead of <html> to guarantee that there is some selector present, but its a touch too fancy for me and uses an extra character :)

Optional 100 more bytes

h1,h2,h3,h4,h5,h6 {
  margin: 3em 0 1em;
}

p,ul,ol {
  margin-bottom: 2em;
  color: #1d1d1d;
  font-family: sans-serif;
}

References

<!DOCTYPE html>
<html>
<head>
<style>
html {
max-width: 70ch;
padding: 3em 1em;
margin: auto;
line-height: 1.75;
font-size: 1.25em;
}
h1,h2,h3,h4,h5,h6 {
margin: 3em 0 1em;
}
p,ul,ol {
margin-bottom: 2em;
color: #1d1d1d;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Test heading one</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vestibulum sed arcu non odio euismod lacinia at quis risus. Neque viverra justo nec ultrices dui sapien eget. Bibendum ut tristique et egestas quis ipsum suspendisse ultrices. Euismod lacinia at quis risus sed vulputate odio. Mi proin sed libero enim. At volutpat diam ut venenatis tellus in. Scelerisque eu ultrices vitae auctor eu augue ut lectus. Nisi scelerisque eu ultrices vitae. Praesent elementum facilisis leo vel. Mollis nunc sed id semper. Blandit cursus risus at ultrices mi tempus imperdiet nulla. Et tortor at risus viverra adipiscing at in tellus integer. Tristique sollicitudin nibh sit amet commodo nulla facilisi. Nisi est sit amet facilisis magna etiam tempor orci.</p>
<h2>Test heading two</h2>
<p>Scelerisque eu ultrices vitae auctor eu augue ut lectus arcu. Eget aliquet nibh praesent tristique magna sit amet. Nec dui nunc mattis enim ut tellus elementum sagittis vitae. Sit amet consectetur adipiscing elit pellentesque habitant morbi tristique. Ullamcorper malesuada proin libero nunc consequat interdum varius sit. Vitae congue eu consequat ac felis donec et odio pellentesque. Orci a scelerisque purus semper. Semper auctor neque vitae tempus quam. Egestas maecenas pharetra convallis posuere morbi leo urna. Vitae turpis massa sed elementum tempus egestas.</p>
<h2>foobar</h2>
<p>Luctus accumsan tortor posuere ac. Lobortis scelerisque fermentum dui faucibus in ornare quam viverra. Suspendisse potenti nullam ac tortor vitae purus faucibus ornare suspendisse. Ut porttitor leo a diam sollicitudin tempor id eu. Vitae ultricies leo integer malesuada nunc vel risus. Sagittis aliquam malesuada bibendum arcu vitae elementum. Urna porttitor rhoncus dolor purus non enim praesent. Nunc sed velit dignissim sodales ut eu. Enim ut tellus elementum sagittis vitae et. Elementum nisi quis eleifend quam adipiscing vitae proin sagittis. Felis eget velit aliquet sagittis id consectetur purus ut. Nulla pharetra diam sit amet nisl suscipit adipiscing. Est ultricies integer quis auctor. Sed faucibus turpis in eu mi bibendum neque egestas congue.</p>
</body>
</html>

web

start webserver:

python3 -m http.server --bind 127.0.0.1 7800

You can now:

curl localhost:7800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment