Menu

Our body text runs 98 characters per line, and on a phone 84 of our headings end on a word of their own
📰

Our body text runs 98 characters per line, and on a phone 84 of our headings end on a word of their own

DEV Community: css·Daniel Pertu·about 14 hours ago
#GPswPsM7
#devtocss#devto#dev#characters#line
Reading 0:00
15s threshold

Every measurement I have made of this site so far has been of things in the document: elements, bytes, colours, focus order. A line of text is not in the document. It is produced by the layout engine at a particular width with a particular font, and the only way to know how long your lines are is to ask the engine after it has decided.

You can. A Range over a single character has a rect, and characters on the same line share a top edge:

const range = document . createRange ( ) ; const lines = new Map ( ) ; for ( let i = 0 ; i < textNode . textContent . length ; i ++ ) { range . setStart ( textNode , i ) ; range . setEnd ( textNode , i + 1 ) ; const r = range . getBoundingClientRect ( ) ; const key = Math . round ( r . top ) ; // one bucket per line box lines . set ( key , ( lines . get ( key ) || 0 ) + 1 ) ; }

Enter fullscreen mode

Exit fullscreen mode

That gives the real line boxes, with their character counts and their pixel widths, after wrapping, at whatever viewport you emulate.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.