0
0
HtmlHow-ToBeginner · 3 min read

How to Underline Text in HTML: Simple Syntax and Examples

To underline text in HTML, use the <u> tag around the text you want underlined. Alternatively, you can use CSS with text-decoration: underline; on any element to achieve the same effect.
📐

Syntax

The simplest way to underline text is by wrapping it with the <u> tag. This tag tells the browser to display the text with a line underneath.

Alternatively, you can use CSS by applying text-decoration: underline; to any HTML element, which gives more control and flexibility.

html
<u>Underlined text</u>

<span style="text-decoration: underline;">Underlined text with CSS</span>
Output
Underlined text Underlined text with CSS
💻

Example

This example shows how to underline text using both the <u> tag and CSS styling.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Underline Text Example</title>
  <style>
    .underline-css {
      text-decoration: underline;
      color: blue;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <p>This is <u>underlined text using the &lt;u&gt; tag</u>.</p>
  <p>This is <span class="underline-css">underlined text using CSS</span>.</p>
</body>
</html>
Output
This is underlined text using the <u> tag. This is underlined text using CSS.
⚠️

Common Pitfalls

One common mistake is using the <u> tag for styling only, which is not recommended for accessibility because it can confuse users who expect underlined text to be a link.

Another pitfall is forgetting to use CSS when you want more styling control, like changing underline color or style.

html
<!-- Wrong: Using <u> for decoration only -->
<p><u>This looks like a link but is not clickable.</u></p>

<!-- Right: Use CSS with clear context -->
<p style="text-decoration: underline; color: red;">This text is underlined with CSS and styled clearly.</p>
Output
This looks like a link but is not clickable. This text is underlined with CSS and styled clearly.
📊

Quick Reference

  • <u>: Simple underline tag, use for semantic meaning or legacy support.
  • CSS text-decoration: underline;: Preferred for styling and more control.
  • Use CSS when you want to change underline color, thickness, or style.
  • Avoid underlining text that is not a link to prevent confusion.

Key Takeaways

Use the tag to underline text simply and semantically.
Prefer CSS text-decoration: underline for styling flexibility.
Avoid underlining non-link text to keep user experience clear.
CSS allows customizing underline color and style beyond basic underlining.
Always test your underlined text for accessibility and clarity.