The HTML mark Element for Search Highlights
Lesson 27 of 36 in the HTML5 learning path.
How do I mark words that matched a search?
The mark element flags text that is relevant right now, usually a search hit. Browsers paint it yellow unless you restyle it.
Mark only the matched words. Wrapping a whole paragraph tells assistive tech that everything was a hit.
Syntax & Parameters
<p>Use <mark>eager</mark> loading.</p>
Example
Interactive preview available<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Results</title>
<style>
body{font:1rem/1.5 sans-serif;margin:2rem} mark{background:#fde68a;color:#1c1917}
</style>
</head>
<body>
<h1>Results for eager</h1><p>Use <mark>eager</mark> loading before the loop.</p><p>Another hit: <mark>eager</mark> loading uses with().</p>
</body>
</html>
Expected Output
Two sentences with the word eager highlighted. Other words stay plain.
Line-by-Line Walkthrough
- The heading repeats the query.
- The first mark wraps only the matched word.
- The second paragraph marks the next hit.
- Background and text color are both set.
- Unmatched words stay outside mark.
- Escape the query before you wrap it.
- Do not mark words that were not searched.
- Keep one mark style for the whole site.
Common Pitfalls & Security
mark means a contextual match.
- Marking every favorite word removes the signal.
- Yellow on yellow fails contrast.
- A raw query inserted into HTML is XSS. Escape, then wrap.
- strong is importance. mark is a hit.
Best Practices & Modern Standards
Rebuild marks from the current query. Do not store highlight tags in the lesson body.
Escape first, highlight second, and echo the query in the heading too.
Real-World Use
Course search highlights the student query inside each blurb and restyles mark so the hit stays visible on a cream page.
Frequently Asked Questions
Can mark sit inside a link?
Yes. The anchor stays the link. mark only flags the matched letters.
Is a styled span the same?
It can look the same. It does not mean the text is a search hit.
Should a whole sentence be marked?
Only when the whole sentence matched.
Does mark replace em?
No. em is stress. mark is relevance in this context.