Sentence Length Colorization

Avatar of Chris Coyier
Chris Coyier on

I think I had the same wooahhhhh coool reaction as so many others did when this tweet was going around:

Certainly, good writing is more than just varied sentence length, but this is a fantastic visualization that makes an excellent point. It wouldn’t hurt to be able to see this kind of thing in our own writing, in an on-demand fashion while editing.

After tweeting that I wasn’t quite sure how best to go about it, a bunch of folks chimed in with their takes on how they would do it.

Dave took a crack at it within a single tweet. His idea is essentially:

  1. Loop through all paragraphs.
  2. Make an array of sentences by splitting the whole string on periods.
  3. Wrap each sentence in a span with a data attribute of how many words in that sentence.

The data attributes could then be used in CSS selectors to colorize. The idea didn’t quite work as written, but I was able to extrapolate that idea into a working concept, if slightly more verbose:

See the Pen Sentence Colorizer by Chris Coyier (@chriscoyier) on CodePen.

Note that this demo:

  • Doesn’t take into account almost any edge case. Note the hyphenated word fail there. Things like “Mrs. Robinson” would be an obvious edge case this doesn’t deal with.
  • Isn’t particularly efficient.

Before we get too far here, It was pointed out several times to me that the UI shown in that tweet looks an awful lot like Hemmingway App.

Indeed it does. I wonder if it’s some kind of hidden feature or something? I wasn’t able to find any sentence length colorizer feature poking around in there a bit. It highlights other things in useful ways though.

And speaking of highlighting sentences for useful alterior purposes, Tone Analyzer is an experiment to do that:

There is also an Angular JS lib that can be used to colorize arbitrary lengths of text:

Pim Derks created an actual bookmarklet to do the job! Very cool.

I’ll post here for posterity:


I was able to get it to work:

I also un-URL Encoded it, and dropped it here in a fork of my Pen so you can take a look at the code more easily (has some minor bugs like doubling up periods at the end):

See the Pen Sentence Colorizer by Chris Coyier (@chriscoyier) on CodePen.

Brandon Brule took a crack at it:

See the Pen Highlight sentence length by Brandon Brule (@brandonbrule) on CodePen.

I do like applying either styling or range-specific classes in JavaScript. The [data-wc] approach was hard because you have to be very explicit. You can’t really do [data-wc>10].

Antoinette Janus has a nicely-done take as well:

See the Pen Text Highlighter by Antoinette Janus (@acjdesigns) on CodePen.

Jonathan Williamson created a demo with a textarea and the colorization is separate and updated as-you-type:

See the Pen Gary Provost by Jonathan Williamson (@jon-w1) on CodePen.

The original tweet was all about the beauty of varied sentence length. Beauty in how the writing feels and reads. It wasn’t really about the colors or visualization of it, that just served to explain the message. But the colors were kinda beautiful too.

In another bit of prior art here, Sanne Peters did some visualized poems that maps words to colors so you can see them as well as read them:

Also remember we’re no stranger to Boomarklets That Help With Text™. A few years ago we tackled one that helped show you where an ideal line length for readability should land:

See the Pen Bookmarklet to make the text between 45 and 75 characters turn red. by Chris Coyier (@chriscoyier) on CodePen.

And that is what an active tweet thread can bring!