Skip to content

Tailwind CSS - Text Underline Offset

The text-underline-offset utilities in Tailwind CSS allow you to control the vertical distance between text and its underline. This is crucial for creating aesthetically pleasing and readable text links, especially when using different underline thicknesses or styles.

While these examples use the Tailwind Play CDN for simplicity, this method is not recommended for production. A professional setup involves installing Tailwind CSS via a package manager like npm and integrating it into a build process.

Terminal window
# 1. Install Tailwind CSS
npm install -D tailwindcss
npx tailwindcss init
# 2. Configure your template paths in tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
# 3. Add Tailwind directives to your main CSS file
@tailwind base;
@tailwind components;
@tailwind utilities;
# 4. Start the build process
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

This modern setup enables features like purging unused styles for minimal file size, using a configuration file for customization, and applying vendor prefixes automatically.

Here is a list of the default text-underline-offset utility classes and their corresponding CSS properties.

ClassCSS Properties
underline-offset-autotext-underline-offset: auto;
underline-offset-0text-underline-offset: 0px;
underline-offset-1text-underline-offset: 1px;
underline-offset-2text-underline-offset: 2px;
underline-offset-4text-underline-offset: 4px;
underline-offset-8text-underline-offset: 8px;
  • underline-offset-auto: The browser determines the offset automatically. This is often the best choice for general readability.
  • underline-offset-0: Places the underline directly against the text’s baseline, which can sometimes interfere with descenders (like in ‘g’ or ‘p’).
  • underline-offset-[n]: Sets a fixed offset, moving the underline n pixels away from the baseline. This provides precise control over the underline’s position.

Let’s explore how these utilities work in a real-world context.

This example demonstrates the visual difference between the various offset utilities on underlined text.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Underline Offset</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8 font-sans">
<main class="max-w-2xl mx-auto bg-white p-6 rounded-lg shadow-md">
<h1 class="text-2xl font-bold mb-6 text-gray-800">Underline Offset Comparison</h1>
<p class="text-lg underline underline-offset-auto mb-4">Offset Auto: Gracefully adjusts.</p>
<p class="text-lg underline underline-offset-0 mb-4">Offset 0: Can clash with descenders.</p>
<p class="text-lg underline underline-offset-1 mb-4">Offset 1: A subtle separation.</p>
<p class="text-lg underline underline-offset-2 mb-4">Offset 2: A clean, modern gap.</p>
<p class="text-lg underline underline-offset-4 mb-4">Offset 4: A more pronounced space.</p>
<p class="text-lg underline underline-offset-8">Offset 8: A highly stylized distance.</p>
</main>
</body>
</html>

Example 2: Interactive and Combined Styling

Section titled “Example 2: Interactive and Combined Styling”

Underline utilities shine when combined with state variants like hover:, and other decoration utilities (decoration-color, decoration-style). This example shows a link that becomes more prominent on hover.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Underline Offset</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-900 text-white flex items-center justify-center min-h-screen font-sans">
<div class="text-center">
<h1 class="text-3xl font-bold mb-6">Modern Link Styling</h1>
<a
href="#"
class="text-2xl text-cyan-400
underline underline-offset-4 decoration-cyan-400/50 decoration-2
transition-all duration-300
hover:underline-offset-8 hover:decoration-cyan-400"
>
Hover over this link to see the effect
</a>
</div>
</body>
</html>
  • Combine Utilities: For the best results, combine underline-offset with decoration-thickness (decoration-2, decoration-4) and decoration-color (decoration-blue-500) for complete control over your underlines.
  • Arbitrary Values: If the default values aren’t enough, you can use arbitrary values like underline-offset-[3px] for pixel-perfect precision.
  • Readability: Be mindful of accessibility. While large offsets can be stylish, ensure the underline remains clearly associated with the text.
  • State Variants: Use hover:, focus:, and other state variants to provide clear visual feedback to users interacting with links.