Okay, so I f**ked up. I forgot to write about <br>
and now the whole series is out of order. Having announced this failing on social media, I appreciate the comments I received like, âgive yourself a <br>
â. Very funny and clever. âExcellent humor,â as Bob Mortimer would say. Anyway, here it is, albeit late.
Some people get very uppity when you conflate the terms tag and element in HTML. Most of the time, a tag is only part of an element. An anchor comprises two tags, for example: <a>
(opening) and </a>
(closing). But some elements are self-closing or void, like <br/>
. In these cases, the tag alone is the element, because inserting text would not make sense. A <br/>
is literally a break from text, after all.
This is all made slightly more complex and off-putting by the fact that, in HTML, you donât actually have to self-close self-closing elements. You can leave the slash out like this: <br>
. In XHTMLâwhich is a syntactically stricter hybrid of HTML and XMLâremoving a slash from a <br/>
would be like removing the lead guitarist Slash from the band Guns ânâ Roses: a lot of dorks would complain loudly and incessantly. Fortunately, nobody really uses XHTML anymore, like nobody really listens to Guns ânâ Roses.
When would you use a <br>
element? Iâm glad I asked that question. Most of the time, line breaks already occur by:
- Letting the text wrap naturally, according to the available space, or
- Separating content between successive block elements, such as paragraphs.
Inserting a <br>
manually is usually unnecessary and always risky. Itâs liable to look out of place in all but one very specific context. Increase or decrease the space around the text just slightly and the text will reflow, causing the <br>
to break where breaking causes an unwanted, erm, breakage.
I very, very rarely code <br>
elements into my HTML, because I very, very rarely write poetry.
I learned all my HTML<br>
From a code camp held yearly in hell<br>
We started on `<s>`<br>
Then striked through the rest<br>
Now I have a career in DevRel
But that doesnât mean I havenât had to code around them. Which brings me to the subject of WYSIWYG editors.
Briefly, a WYSIWYG editor is a rich text editor that outputs HTML. It enables those who canât (or wonât) write HTML to generate content in the HTML format. I would estimate this is how most of the webâs content is created. The problem with WYSIWYG editors is they offer visual formatting options based on structural semantics. I should have said: WYSIWYG stands for âWhat You See Is What You Getâ, ironically.
For example, someone looking to make text go big will inevitably choose a âHeading 1â or âHeading 2â formatting option, regardless of whether a corresponding <h1>
or <h2>
heading element is suitable for the context or the nature of the content.
I recently worked on an algorithm that calculates heading levels and assigns them to design system templates. It was kind of neat, because it meant you could insert any component using headings anywhere in a consuming application and the heading level would be assigned automatically. Just one problem: this only works for components coming from the design system. Meanwhile, most of the actual content is spat out by a WYSIWYG editor.
Anyway, I digress. More to the point, WYSIWYG editors also tend to generate a lot of <br>
elements. Why? Well, basically, because their desktop publishing forebears donât have a concept of a <p>
. In desktop publishing, a paragraph is not a semantic entity; it is the appearance of a block of text separated by a single blank line.
In Microsoft Wordâ˘, if you hit ENTER you will start a new line. If you hit ENTER twice, then you create that blank line and commence a new paragraph. A web-based WYSIWYG editor worth its salt will behave differently. If you hit ENTER just onceâa hard returnâa new paragraph (<p>
) will be created. Creating a simple line break (<br>
) is the purview of a soft return: SHIFT + ENTER.
Okay, so fine. Itâs different; people will adapt. Except they donât. An editor who hits ENTER just once, only to see theyâve jumped two whole lines ahead, will be incensed. And I kid you not, they will backspace to where they started and use TWO SOFT RETURNS to arrive back in exactly the same place.
The same to them, anyway, and theyâre not stupidâthatâs how it always was, with typewriters and stuff. But to a front-end developer, itâs the difference between two nice paragraphs or some text with <br><br>
lurking inside it. To cut a long story short, this is some CSS code I wrote and deployed for one of the worldâs largest publishers. It hides all <br>
elements, reinstating a natural flow of text across all content.
br {
display: none;
}