CSS TEXT ALIGNMENT /// VERTICAL ALIGN /// LINE HEIGHT /// JUSTIFY /// CSS TEXT ALIGNMENT /// VERTICAL ALIGN /// LINE HEIGHT /// JUSTIFY ///

CSS Text Alignment

Take control of your typography. Learn how to perfectly position text horizontally, conquer the tricky vertical-align, and establish solid vertical rhythm.

typography.css
1 / 13
12345
📝

Tutor:Text alignment is crucial for readability and layout structure. CSS gives us immense control over how text flows and sits within its container.


Alignment Matrix

UNLOCK NODES BY MASTERING SPACING.

Horizontal Alignment

The text-align property is used to set the horizontal alignment of a text.

Alignment Assessment

Does `text-align: center` on a block element center the block itself or its inline content?


Community Holo-Net

Showcase Your Layouts

ACTIVE

Created a beautifully aligned typography system? Share your codepens and get feedback!

CSS Text Alignment: Controlling Typography

Frontend Instructor in Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

Mastering text alignment goes beyond just centering a paragraph. It involves understanding horizontal flow, vertical rhythm, line heights, and how inline elements interact with their containers.

Horizontal Alignment: text-align

The text-align property is your primary tool for horizontal alignment. It applies to block-level elements but affects the inline content inside them.

  • left: Aligns text to the left margin (default in LTR languages).
  • right: Aligns text to the right margin.
  • center: Centers the text horizontally within the block.
  • justify: Stretches the lines so that both left and right edges are aligned.

The Mystery of vertical-align

Many developers try to use vertical-align: middle on a div and wonder why it doesn't work. The key is that vertical-align only applies to inline, inline-block, or table-cell elements.

It is used to align an inline element relative to the line-height of its parent or sibling elements. For example, aligning a small icon perfectly with adjacent text. If you want to vertically center a block element, you should look into Flexbox (align-items: center) instead!

Vertical Rhythm: line-height

The line-height property sets the distance between lines of text. A crucial best practice is to use unitless values (like 1.5 instead of 24px).

Using a unitless value ensures that child elements recalculate their line-height based on their own font-size, preventing overlapping text issues down the DOM tree.

View Classic Centering Trick+

The Line-Height Trick: Before Flexbox existed, a common way to vertically center a single line of text within a button or div was to set the line-height equal to the element's height.

.btn { height: 40px; line-height: 40px; }

Frequently Asked Questions

Why isn't vertical-align working on my div?

vertical-align is mathematically designed to work only within an inline formatting context (inline or inline-block elements) or in table cells (table-cell). It has no effect on block-level elements like a standard <div>.

How can I center vertically if vertical-align doesn't work for divs?

Nowadays, the best way to vertically center content within a block container is to use Flexbox or CSS Grid.

display: flex;
align-items: center; /* Vertical centering */
justify-content: center; /* Horizontal centering */
Why is unitless line-height recommended?

If you use line-height: 1.5em; or line-height: 24px;, that fixed value is inherited by child elements. If a child has a very large font-size, the line-height won't grow proportionally and the text will overlap. By using a unitless value (1.5), the browser recalculates the line-height based on the font-size of each individual child.

Alignment Glossary

text-align
Describes how inline content like text is aligned in its parent block element.
snippet.css
vertical-align
Sets the vertical alignment of an inline, inline-block or table-cell element.
snippet.css
line-height
Sets the height of a line box. Commonly used to set distance between lines of text.
snippet.css
text-justify
Sets the justification method used when text-align is set to justify.
snippet.css
text-indent
Specifies the indentation of the first line in a text-block.
snippet.css
direction
Sets the text direction (ltr for English, rtl for Arabic/Hebrew).
snippet.css