HTML text-level semantics

Before HTML5, text-level semantic elements were referred to in the specifications as inline elements. Therefore, if you are familiar with that description, be aware that we are talking about the same thing here.

Let’s take a look at the most common and useful text-level elements.

 

The <span> element

The span element is the text-level equivalent of a div. It is unopinionated and is the perfect element to reach for when you merely want to wrap text in an element for styling purposes.

 

The <b> element

Historically, visuals were defined in the markup and the <b> element meant “make this bold”. The b element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood, such as key words in a document abstract, product names in a review, actionable words in interactive text-driven software, or an article lede.

Although no specific meaning is now attached to it, as it’s text level, it’s not intended to be used to surround large groups of markup. Use a div for that.

You should also be aware that because it was historically used to bold text, you’ll typically have to reset the font-weight in CSS if you want content within a <b> tag to not appear bold.

For example:

b {

font-weight: normal;

}

The <strong> element

If you do want to emphasize something for strength, urgency, or importance, <strong> is the element for you. Here is how the specification defines these use cases:

Importance: The strong element can be used in a heading, caption, or paragraph to distinguish the part that really matters from other parts that might be more detailed, more jovial, or merely boilerplate.

Seriousness: The strong element can be used to mark up a warning or caution notice.

Urgency: The strong element can be used to denote contents that the user needs to see sooner than other parts of the document.”

 

Learn and Earn More-   Want to be a Developer? What Front-End Developers Should Know

The <em> element

I’ll admit, in the past, I’ve often used <em> merely as a styling hook to provide italic text when I wanted it. I need to mend my ways as the HTML specification tells us:

The em element represents stress emphasis of its contents.

Therefore, unless you actually want the enclosed contents to be emphasized, consider using a <b> tag or, where relevant, an <i> or span tag instead.

The <i> element

The HTML5 specification describes the <i> as:

A span of text in an alternate voice or mood, or otherwise offset from the normal prose in a manner indicating a different quality of text.

Suffice it to say, it’s not to be used to merely italicize something. For example, we could use it to mark up the odd name in this line of text:

<p>However, discussion on the hgroup element is now frustraneous as it’s now gone the way of the <i>Raphus cucullatus</i>.</p>

 

Or, perhaps if you were marking up a button in a food ordering web application, you might do this:

&lt;button type="button"&gt;

French Fries &lt;i&gt;No Salt Added&lt;/i&gt;

&lt;/button&gt;

&amp;nbsp;

 

Putting HTML elements to use

It’s time to practice using some of the elements we have just looked at. If we compare the following markup to the original markup you can see where the new elements we’ve looked at have been employed:

&lt;article&gt;

&lt;header class="Header"&gt;

&lt;a href="/" class="LogoWrapper"

&gt;&lt;img src="img/SOC-Logo.png" alt="Scone O'Clock logo"

/&gt;&lt;/a&gt;

&lt;h1 class="Strap"&gt;Scones: the most resplendent of snacks&lt;/h1&gt;

&lt;/header&gt;

&lt;section class="IntroWrapper"&gt;

&lt;p class="IntroText"&gt;

Occasionally maligned and misunderstood; the scone is a quintessentially British classic.

&lt;/p&gt;

&lt;figure class="MoneyShot"&gt;

&lt;img class="MoneyShotImg" src="img/scones.jpg" alt="Incredible scones" /&gt;

&lt;figcaption class="ImageCaption"&gt;

Incredible scones, picture from Wikipedia

&lt;/figcaption&gt;

&lt;/figure&gt;

&lt;/section&gt;

&lt;p&gt;Recipe and serving suggestions follow.&lt;/p&gt;

&lt;section class="Ingredients"&gt;

&lt;h3 class="SubHeader"&gt;Ingredients&lt;/h3&gt;

&lt;/section&gt;

&lt;section class="HowToMake"&gt;

&lt;h3 class="SubHeader"&gt;Method&lt;/h3&gt;

&lt;/section&gt;

&lt;footer&gt;

Made for the book,

&lt;a href="http://rwd.education"

&gt;'Responsive web design with HTML5 and CSS'&lt;/a

&gt;

by

&lt;address&gt;&lt;a href="http://benfrain"&gt;Ben Frain&lt;/a&gt;&lt;/address&gt;

&lt;/footer&gt;

&lt;/article&gt;

At this point I’d also like to offer some pragmatic advice; it isn’t the end of the world if you don’t always pick the correct element for every single given situation.

Learn and Earn More-   Fluid layout, Flexbox, and responsive images in css 4

For example, whether or not I used a <section> or <div> in the earlier example is of little real consequence. If we use an <em> when we should actually be using an <i>, I certainly don’t feel it’s a crime against humanity; the folks at the W3C won’t hunt you down and tar and feather you for making the wrong choice. Just apply a little common sense. That said, if you can use elements like the <header> and <footer> when relevant, there are inherent accessibility benefits in doing so. I certainly think you’re better than using nothing but div elements in your markup!

 



WhatsApp chat