Text Direction, Decoration Styles and Selection
What Is It?
Text presentation includes direction-aware alignment, decoration line styles, last-line alignment, and selection highlighting.
Properties, Tags and Attributes Explained
text-align: start and end follow inline direction; left, right, center, and justify control inline content alignment.text-align-last: Controls alignment of the last line or a line before a forced break.text-decoration-line: Chooses underline, overline, line-through, or none.text-decoration-style: Chooses solid, double, dotted, dashed, or wavy decoration lines.direction: Sets CSS text direction, but use the HTML dir attribute to express content direction.unicode-bidi: Controls bidirectional embedding or isolation; HTML bdi and dir are usually better for content semantics.::selection: Styles selected text with a supported subset of properties.
Each example is a complete HTML page. The CSS is inside the head's style element. Use Try in new tab to edit the CSS or HTML, run your changes, and reset the example.
Example 1: Choose the last-line alignment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Direction, Decoration Styles and Selection</title>
<style>
p { max-width: 32ch; text-align: justify; text-align-last: center; line-height: 1.6; }
</style>
</head>
<body>
<p>Short CSS examples make learning easier because you can change one value and see a clear result immediately.</p>
</body>
</html>What you see: The paragraph is justified while its final line is centered. Use last-line alignment for deliberate editorial layouts.
Example 2: Style a decoration line
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Direction, Decoration Styles and Selection</title>
<style>
.note { text-decoration-line: underline; text-decoration-style: wavy; text-decoration-color: #7c3aed; text-underline-offset: .3em; }
</style>
</head>
<body>
<p class="note">A decorative underline</p>
</body>
</html>What you see: The underline uses a purple wavy line. Keep essential error or status meaning in text rather than relying on decoration alone.
Example 3: Select text
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Direction, Decoration Styles and Selection</title>
<style>
p::selection { background-color: #fde68a; color: #172033; }
</style>
</head>
<body>
<p>Select a few words in this paragraph to see the highlight.</p>
</body>
</html>What you see: Selected text receives the requested highlight where supported. Use strong contrast for both selected text and background.
Example 4: Isolate a direction fragment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Direction, Decoration Styles and Selection</title>
<style>
.token { direction: rtl; unicode-bidi: isolate; }
</style>
</head>
<body>
<p>A direction-isolated token: <span class="token">ABC 123</span> in a sentence.</p>
</body>
</html>What you see: The span creates a separate bidirectional context. Prefer HTML dir and bdi when the fragment is actual content requiring isolation.
When and Where to Use It
Use direction-aware alignment for translated content, explicit decoration properties for link or heading styles, and selection colors as an optional readability enhancement.
Common Mistake or Useful Tip
text-align does not position the block box itself. Avoid overriding content direction just to move an element visually.
Try It Yourself
Change the last-line alignment from center to start and compare it in an rtl paragraph.