Highlighting Relevant Text With HTML mark
The HTML <mark> element highlights text because it is relevant to the current context. A search-results page is a good example: the matching words matter because of what the person searched for.
<p>
Results for <mark>semantic HTML</mark>
</p>
Browsers normally give marked text a yellow background. That default is useful, but you can change it with ordinary CSS:
mark {
background: navy;
color: white;
}
Use <mark> for contextual relevance, not just because you want yellow text. If the text is important regardless of context, <strong> may be the better element. The visual result can look similar; the meaning is not the same.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Choosing Between srcset and picture
Choose srcset when the image stays the same and picture when the crop, composition, or format needs to change.
- Finding Hidden Content With hidden="until-found"
Let Find in Page and fragment navigation reveal collapsed content with hidden=until-found, while keeping a usable fallback.
- HTML Features That Can Save You Some JavaScript
Use native disclosures, dialogs, popovers, form attributes, and responsive images before rebuilding their basic behaviour.