Master HTML From Scratch

Clear, interactive, and structured HTML lessons designed for beginners.

Underline and Highlight

Learn how to underline text and highlight relevant content using HTML and CSS.

Introduction

HTML and CSS provide different ways to visually distinguish text. The <u> element can represent text with an unarticulated annotation, while the <mark> element highlights text because it is relevant to the current context.

Common Elements:
<u> for underline-style annotation
<mark> for highlighted text

The <u> Element

The <u> element represents text with a non-textual annotation. Browsers commonly render it with an underline.

Syntax

<u>Underlined Text</u>

Example

<p>
    Learn <u>HTML</u> step-by-step.
</p>

Output

Learn HTML step-by-step.

Example Uses of <u>

Annotation
<p>
    This word has an
    <u>annotation</u>.
</p>
Misspelled Word
<p>
    Check the
    <u>speling</u>
    in this sentence.
</p>

The <mark> Element

The <mark> element represents text that is highlighted because it is relevant to the user's current context.

Syntax

<mark>Highlighted Text</mark>

Example

<p>
    Learn <mark>HTML</mark> first.
</p>

Output

Learn HTML first.

Common Uses of <mark>

Search Result
<p>
    Search result:
    <mark>HTML</mark>
</p>
Important Keyword
<p>
    Learn
    <mark>semantic HTML</mark>.
</p>
Matching Text
<p>
    Found:
    <mark>JavaScript</mark>
</p>
Study Material
<p>
    Focus on
    <mark>HTML Tags</mark>
</p>

<u> vs <mark>

Feature <u> <mark>
Main Purpose Represents text with an annotation. Highlights relevant text.
Common Browser Appearance Underlined text. Highlighted text.
Typical Example Annotated or specially marked text. Search matches or relevant text.

Underline Using CSS

When the goal is purely visual styling, CSS is usually more appropriate than using HTML semantics.

<p class="underlined-text">
    HTML Course
</p>

<style>

    .underlined-text {
        text-decoration: underline;
    }

</style>

HTML Course

Different Underline Styles with CSS

Solid underline:

HTML Course

Dashed underline:

HTML Course

Dotted underline:

HTML Course

Custom Highlighting with CSS

CSS can also be used when you need a custom visual highlighting style.

<span class="custom-highlight">
    Important Text
</span>

<style>

    .custom-highlight {
        background-color: yellow;
        padding: 2px 5px;
    }

</style>

Important Text

Combining Underline and Highlight

<p>
    <u>
        <mark>Important HTML Topic</mark>
    </u>
</p>

Important HTML Topic

Semantic Meaning

<u>

Represents text with an annotation or non-textual distinction.

<mark>

Indicates text that is relevant or highlighted in the current context.

Accessibility Considerations

Text styling should not be the only way important information is communicated.

Important: Do not rely only on underline or highlight color to communicate critical information. The content should remain understandable through its text and structure.

Real-World Example

<h2>
    HTML Course
</h2>

<p>
    Focus on the
    <mark>HTML Tags</mark>
    chapter first.
</p>

<p>
    Review the
    <u>important note</u>
    before continuing.
</p>

HTML Course

Focus on the HTML Tags chapter first.

Review the important note before continuing.

Common Mistakes

  • Using <u> only because you want visual decoration.
  • Using highlighting everywhere without a meaningful reason.
  • Using color alone to communicate important information.
  • Forgetting that CSS can control visual underline and highlight styles.

Practice Exercise

  1. Underline a word using <u>.
  2. Highlight an important keyword using <mark>.
  3. Create a custom underline using CSS.
  4. Create a custom highlight using CSS.
  5. Compare semantic HTML with purely visual CSS styling.

Summary

The <u> element represents text with a non-textual annotation and is commonly displayed with an underline. The <mark> element highlights text that is relevant to the current context. CSS should be used for purely visual underline or highlighting requirements.