CSS vertical-align Property
The CSS vertical-align property controls the vertical alignment of inline-level elements and the contents of table cells.
It can align text, images, icons, and other inline-level elements relative to the surrounding line box. In tables, it controls how cell content is positioned vertically when the cell is taller than its content.
Important: The vertical-align property does not vertically center ordinary block elements or control alignment along the cross axis of Flexbox and Grid layouts.
vertical-align Syntax
The vertical-align property accepts alignment keywords, length values, and percentages.
Basic Syntax
.example {
vertical-align: baseline;
}
Keyword Values
.example {
vertical-align: middle;
}
.example {
vertical-align: top;
}
.example {
vertical-align: bottom;
}
Text-Relative Values
.example {
vertical-align: sub;
}
.example {
vertical-align: super;
}
.example {
vertical-align: text-top;
}
.example {
vertical-align: text-bottom;
}
Length Values
.example {
vertical-align: 5px;
}
.example {
vertical-align: -3px;
}
.example {
vertical-align: 0.5em;
}
Percentage Values
.example {
vertical-align: 25%;
}
.example {
vertical-align: -50%;
}
CSS-Wide Keywords
.example {
vertical-align: initial;
}
.example {
vertical-align: inherit;
}
.example {
vertical-align: unset;
}
.example {
vertical-align: revert;
}
.example {
vertical-align: revert-layer;
}
Invalid Values
.example {
vertical-align: center;
}
.example {
vertical-align: left;
}
.example {
vertical-align: right;
}
The values center, left, and right are not valid vertical-align values.
Property Values
| Value | Description |
|---|---|
baseline |
Aligns the element's baseline with the parent's baseline. This is the initial value. |
sub |
Positions an inline element at the parent's subscript baseline. |
super |
Positions an inline element at the parent's superscript baseline. |
text-top |
Aligns the element's top with the top of the parent's font. |
text-bottom |
Aligns the element's bottom with the bottom of the parent's font. |
middle |
Aligns the element's middle with the parent's baseline plus half its x-height. |
top |
Aligns the element's top with the top of the line box, or the top of a table cell's content area. |
bottom |
Aligns the element's bottom with the bottom of the line box, or the bottom of a table cell's content area. |
<length> |
Raises or lowers an inline element relative to its baseline position. Positive values raise it; negative values lower it. |
<percentage> |
Raises or lowers an inline element by a percentage of its own line height. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | baseline |
| Inherited | No |
| Applies to | Inline-level elements and table-cell elements |
| Computed value | As specified, with lengths made absolute |
| Animation type | By computed value type for lengths and percentages; discrete for keywords |
| Percentages | Relative to the element's own line height |
| Specification | CSS Inline Layout Module Level 3 and CSS 2.2 |
Note: Some values have different meanings for inline-level elements and table cells. The top, middle, and bottom values are particularly useful for table-cell alignment.
How It Works
Text and inline-level elements are arranged into line boxes. Each line has a baseline used to position characters and other inline content.
The vertical-align property changes how an inline-level element is positioned relative to that baseline or the surrounding line box.
Step 1: Create Inline Content
<p>Normal text <span class="raised">raised text</span> continues here.</p>
Step 2: Apply Vertical Alignment
.raised {
vertical-align: 5px;
}
The span moves upward relative to its normal baseline position.
Step 3: Lower the Element
.raised {
vertical-align: -5px;
}
The span moves downward instead.
Step 4: Use a Keyword
.raised {
vertical-align: middle;
}
The element's vertical midpoint aligns with a position slightly above the parent's baseline.
Important Distinction
Unlike a transform, changing vertical-align can affect the height of the line box because the browser must accommodate the aligned element.
The baseline Value
The baseline value is the default alignment for inline-level elements.
Syntax
.example {
vertical-align: baseline;
}
HTML Example
<p>This is <span class="baseline-text">baseline-aligned text</span> in a sentence.</p>
CSS Example
.baseline-text {
vertical-align: baseline;
}
How It Behaves
The element's baseline aligns with the parent's baseline. For replaced elements such as images, the bottom margin edge is generally used when a normal text baseline is unavailable.
When to Use It
Use baseline for normal inline text alignment or to restore the default behavior after another alignment rule has been applied.
The sub and super Values
The sub and super values position inline elements at subscript and superscript baseline positions.
Subscript Syntax
.subscript {
vertical-align: sub;
}
Superscript Syntax
.superscript {
vertical-align: super;
}
HTML Example
<p>H<span class="subscript">2</span>O</p>
<p>x<span class="superscript">2</span></p>
CSS Example
.subscript {
font-size: 0.75em;
vertical-align: sub;
}
.superscript {
font-size: 0.75em;
vertical-align: super;
}
How It Behaves
The sub value lowers the element to a subscript position, while super raises it to a superscript position.
Font Size
These values do not automatically reduce font size. Use font-size when smaller characters are desired.
Semantic HTML
For meaningful subscripts and superscripts, prefer the HTML <sub> and <sup> elements.
<p>H<sub>2</sub>O</p>
<p>x<sup>2</sup></p>
The text-top and text-bottom Values
The text-top and text-bottom values align an inline element with the top or bottom of the parent's font.
text-top Syntax
.example {
vertical-align: text-top;
}
text-bottom Syntax
.example {
vertical-align: text-bottom;
}
HTML Example
<p>Text <span class="top-aligned">TOP</span> and <span class="bottom-aligned">BOTTOM</span> alignment.</p>
CSS Example
.top-aligned {
font-size: 12px;
vertical-align: text-top;
}
.bottom-aligned {
font-size: 12px;
vertical-align: text-bottom;
}
How It Behaves
The element is positioned relative to the parent's font metrics rather than the complete height of the line box.
When to Use Them
These values are useful when small inline elements need to align with the upper or lower edges of surrounding text.
The middle Value
The middle value aligns the vertical midpoint of an inline-level element with the parent's baseline plus half its x-height.
Syntax
.example {
vertical-align: middle;
}
HTML Example
<p><img class="inline-icon" src="icon.svg" alt=""> Text beside an icon.</p>
CSS Example
.inline-icon {
width: 20px;
height: 20px;
vertical-align: middle;
}
How It Behaves
The image is positioned near the visual middle of the surrounding text.
Important Distinction
For inline elements, middle does not mean exact centering within the parent element's height.
Table Cells
When applied to a table cell, vertical-align: middle centers its content vertically within the available cell height.
When to Use It
Use it for small icons beside text and for vertically centering content inside table cells.
The top and bottom Values
The top and bottom values align inline-level elements with the corresponding edges of the line box.
top Syntax
.example {
vertical-align: top;
}
bottom Syntax
.example {
vertical-align: bottom;
}
HTML Example
<p>Normal text <span class="align-top">Top aligned</span> and <span class="align-bottom">Bottom aligned</span>.</p>
CSS Example
.align-top {
font-size: 12px;
vertical-align: top;
}
.align-bottom {
font-size: 12px;
vertical-align: bottom;
}
Line Box Alignment
For inline-level elements, the values align with the top or bottom of the line box, which may be taller than the surrounding text.
Table Cell Alignment
td {
vertical-align: top;
}
For table cells, top positions content toward the top of the cell, while bottom positions it toward the bottom.
When to Use Them
These values are useful for aligning inline elements of different heights and for controlling content placement inside table cells.
Length Values
A length value raises or lowers an inline-level element relative to its normal baseline position.
Positive Length
.example {
vertical-align: 5px;
}
A positive value raises the element.
Negative Length
.example {
vertical-align: -5px;
}
A negative value lowers the element.
Using em Units
.example {
vertical-align: 0.5em;
}
The em unit is relative to the element's font size.
Using rem Units
.example {
vertical-align: 0.25rem;
}
The rem unit is relative to the root element's font size.
HTML Example
<p>Normal text <span class="raised">raised</span> and <span class="lowered">lowered</span>.</p>
CSS Example
.raised {
vertical-align: 6px;
}
.lowered {
vertical-align: -6px;
}
Important Consideration
Length values are useful for precise adjustments, but large offsets can increase line box height or produce awkward spacing between lines.
Percentage Values
Percentage values raise or lower an inline-level element relative to its normal baseline position.
The percentage is calculated from the element's own line-height, not the height of its containing block.
Positive Percentage
.example {
vertical-align: 25%;
}
The element is raised by 25% of its own line height.
Negative Percentage
.example {
vertical-align: -25%;
}
The element is lowered by 25% of its own line height.
HTML Example
<p>Normal text <span class="percentage-align">adjusted text</span> continues here.</p>
CSS Example
.percentage-align {
line-height: 24px;
vertical-align: 25%;
}
Calculation
With a line height of 24px, vertical-align: 25% raises the element by 6px.
Important Consideration
Percentage alignment depends on line height. Changing line-height can change the resulting vertical offset.
Inline Elements
The vertical-align property commonly affects inline elements such as <span>, <strong>, and <em>.
HTML Example
<p>This is <span class="highlight">aligned text</span> inside a paragraph.</p>
CSS Example
.highlight {
font-size: 12px;
vertical-align: super;
}
How It Behaves
The inline span moves relative to the surrounding text baseline.
Inline Formatting Context
Inline elements participate in line layout. Their vertical positions are calculated as part of the line box.
Line Height
Aligned inline elements can influence the height of the line box, particularly when they are raised, lowered, or larger than surrounding text.
When to Use It
Use vertical-align for small text adjustments, inline symbols, and elements that need to align with nearby characters.
Inline-Block Elements
Elements using display: inline-block participate in inline formatting and can be vertically aligned relative to neighboring inline-level elements.
HTML Example
<div class="inline-block-container">
<span class="box-one">First</span>
<span class="box-two">Second</span>
</div>
CSS Example
.box-one,
.box-two {
display: inline-block;
width: 80px;
padding: 10px;
background-color: #e5eef7;
}
.box-one {
height: 40px;
vertical-align: top;
}
.box-two {
height: 70px;
vertical-align: top;
}
How It Behaves
Both boxes align along their top edges within the same line box.
Baseline Behavior
An inline-block normally uses the baseline of its last in-flow line box. If it has no suitable in-flow line box, or its overflow is not visible, its bottom margin edge is generally used instead.
Using middle
.box-one,
.box-two {
vertical-align: middle;
}
The boxes align their vertical midpoints relative to the parent text baseline and x-height.
Important Consideration
For modern component layouts, Flexbox or Grid often provides more predictable alignment than inline-block elements.
Table Cells
The vertical-align property controls the vertical positioning of content inside table cells.
Unlike inline alignment, table-cell alignment uses the available height of the cell.
Top Alignment
td {
vertical-align: top;
}
Middle Alignment
td {
vertical-align: middle;
}
Bottom Alignment
td {
vertical-align: bottom;
}
HTML Example
<table>
<tr>
<td class="top-cell">Top</td>
<td class="middle-cell">Middle</td>
<td class="bottom-cell">Bottom</td>
</tr>
</table>
CSS Example
table {
border-collapse: collapse;
}
td {
width: 100px;
height: 100px;
padding: 10px;
border: 1px solid #999;
}
.top-cell {
vertical-align: top;
}
.middle-cell {
vertical-align: middle;
}
.bottom-cell {
vertical-align: bottom;
}
How It Behaves
The first cell places its content near the top, the second centers it vertically, and the third places it near the bottom.
Table Cell Limitations
For table cells, the most useful values are baseline, top, middle, and bottom. Length and percentage offsets are not used for ordinary table-cell vertical alignment.
When to Use It
Use vertical-align to align content in data tables where rows contain cells of different content heights.
Images and Icons
Inline images and icons are commonly aligned with surrounding text using vertical-align.
Default Image Alignment
<p><img src="icon.svg" alt=""> Label text</p>
An inline image normally aligns its bottom edge with the text baseline. This can leave a small gap below the image for text descenders.
Middle Alignment
img.inline-icon {
vertical-align: middle;
}
Top Alignment
img.inline-icon {
vertical-align: top;
}
Bottom Alignment
img.inline-icon {
vertical-align: bottom;
}
HTML Example
<p><img class="inline-icon" src="icon.svg" alt=""> Account Settings</p>
CSS Example
.inline-icon {
width: 18px;
height: 18px;
vertical-align: middle;
}
Removing the Baseline Gap
.standalone-image {
display: block;
}
When an image does not need to remain inline with text, using display: block removes the baseline spacing associated with inline image layout.
Important Consideration
For icons inside buttons and other interactive components, Flexbox with align-items: center can provide more predictable alignment.
Related CSS Properties
line-height
.example {
line-height: 1.5;
}
Controls the height of line boxes and influences vertical spacing around inline content.
text-align
.example {
text-align: center;
}
Controls horizontal alignment of inline content within a block container.
align-items
.example {
display: flex;
align-items: center;
}
Aligns Flexbox items along the cross axis. It is commonly used for vertical centering in a horizontal flex container.
align-self
.example {
align-self: center;
}
Overrides cross-axis alignment for an individual flex or grid item.
place-items
.example {
display: grid;
place-items: center;
}
Provides a shorthand for aligning items along both axes in a grid container.
position
.example {
position: relative;
top: 3px;
}
Can visually offset an element without changing its original allocated space in normal flow.
transform
.example {
transform: translateY(3px);
}
Visually moves an element without changing the space reserved for it in the layout.
Choosing the Correct Property
Use vertical-align for inline-level alignment and table cells. Use Flexbox or Grid alignment properties for modern layout containers.
Inheritance
The vertical-align property is not inherited. Child elements do not automatically receive the parent's declared value.
Parent Element
.parent {
vertical-align: middle;
}
Child Element
.parent .child {
vertical-align: top;
}
The child uses its own declared alignment.
Explicit Inheritance
.parent {
vertical-align: middle;
}
.parent .child {
vertical-align: inherit;
}
The child explicitly inherits the parent's computed value.
Restore the Initial Value
.example {
vertical-align: initial;
}
This restores the initial value of baseline.
Using unset
.example {
vertical-align: unset;
}
Because the property is not inherited, unset behaves like initial.
Important Consideration
Applying vertical-align to a block-level parent does not vertically align all of its child content. The property must apply to an element that participates in a supported alignment context.
Practical Examples
Align an Icon With Text
.icon {
width: 18px;
height: 18px;
vertical-align: middle;
}
Raise a Text Symbol
.symbol {
vertical-align: 4px;
}
Lower a Text Symbol
.symbol {
vertical-align: -4px;
}
Create a Superscript Effect
.superscript {
font-size: 0.75em;
vertical-align: super;
}
Create a Subscript Effect
.subscript {
font-size: 0.75em;
vertical-align: sub;
}
Align Inline-Block Elements at the Top
.inline-card {
display: inline-block;
vertical-align: top;
}
Center Table Cell Content
td {
vertical-align: middle;
}
Top-Align Table Cell Content
td {
vertical-align: top;
}
Bottom-Align Table Cell Content
td {
vertical-align: bottom;
}
Align a Small Label With Text
.small-label {
display: inline-block;
font-size: 0.75em;
vertical-align: middle;
}
These examples demonstrate the most common uses of vertical-align in text formatting and table layouts.
vertical-align Example
This example compares several vertical-align values applied to inline text and demonstrates vertical alignment inside table cells.
HTML Example
<div class="alignment-demo">
<div class="alignment-row">
<strong>baseline</strong>
<p>Normal text <span class="align-baseline">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>super</strong>
<p>Normal text <span class="align-super">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>sub</strong>
<p>Normal text <span class="align-sub">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>middle</strong>
<p>Normal text <span class="align-middle">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>10px</strong>
<p>Normal text <span class="align-raised">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>-10px</strong>
<p>Normal text <span class="align-lowered">Example</span> continues.</p>
</div>
<div class="alignment-row">
<strong>Table Cell Alignment</strong>
<table>
<tr>
<td class="cell-top">Top</td>
<td class="cell-middle">Middle</td>
<td class="cell-bottom">Bottom</td>
</tr>
</table>
</div>
</div>
CSS Example
.alignment-demo {
display: grid;
gap: 12px;
}
.alignment-row {
padding: 12px;
border: 1px solid #d1d5db;
border-radius: 4px;
}
.alignment-row strong {
display: block;
margin-bottom: 8px;
}
.alignment-row p {
margin: 0;
line-height: 2;
}
.alignment-row span {
color: #0066cc;
font-weight: bold;
font-size: 12px;
}
.align-baseline {
vertical-align: baseline;
}
.align-super {
vertical-align: super;
}
.align-sub {
vertical-align: sub;
}
.align-middle {
vertical-align: middle;
}
.align-raised {
vertical-align: 10px;
}
.align-lowered {
vertical-align: -10px;
}
.alignment-row table {
width: 100%;
border-collapse: collapse;
}
.alignment-row td {
width: 33.33%;
height: 80px;
padding: 8px;
border: 1px solid #999;
text-align: center;
}
.cell-top {
vertical-align: top;
}
.cell-middle {
vertical-align: middle;
}
.cell-bottom {
vertical-align: bottom;
}
Normal text Example continues.
Normal text Example continues.
Normal text Example continues.
Normal text Example continues.
Normal text Example continues.
Normal text Example continues.
| Top | Middle | Bottom |
Understanding the Results
The first six examples show how vertical-align changes the position of inline text relative to the surrounding baseline.
baseline
The smaller text aligns with the surrounding text baseline.
super
The smaller text moves to a superscript position.
sub
The smaller text moves to a subscript position.
middle
The smaller text aligns its midpoint relative to the parent's baseline and x-height.
Positive Length
The 10px value raises the smaller text.
Negative Length
The -10px value lowers the smaller text.
Table Cell Alignment
The table demonstrates how top, middle, and bottom position content vertically within cells of equal height.
Experiment With the Values
Change one of the inline alignment values and compare the result.
.align-raised {
vertical-align: 20px;
}
The text moves farther upward, and the line box may grow to accommodate the new position.
Accessibility Considerations
Vertical alignment affects the visual position of content but does not change its semantic meaning or reading order.
Use Semantic HTML
For subscripts and superscripts with mathematical or scientific meaning, use the HTML <sub> and <sup> elements rather than relying only on visual positioning.
Maintain Readable Text
Avoid reducing font sizes excessively when creating superscript or subscript effects.
Avoid Overlapping Content
Large positive or negative alignment offsets can make text difficult to read or cause visual collisions.
Preserve Line Spacing
Check that raised or lowered elements do not interfere with neighboring lines of text.
Align Icons Consistently
When placing icons beside text, ensure the icon remains visually associated with its label.
Use Accessible Table Markup
For data tables, use appropriate table headers and associations. Vertical alignment does not replace semantic table structure.
Test Text Zoom
Verify that alignment remains usable when text size increases or the page is zoomed.
Avoid Fixed Offsets When Possible
Keyword values often adapt better to changes in font size than fixed pixel offsets.
Common Mistakes
Using center Instead of middle
.example {
vertical-align: center;
}
The center value is invalid. Use middle where appropriate.
Trying to Center a Block Element
.container {
height: 200px;
vertical-align: middle;
}
This does not vertically center an ordinary block element's content.
Using vertical-align on Flex Items
.container {
display: flex;
}
.item {
vertical-align: middle;
}
Use Flexbox alignment properties instead.
Using vertical-align on Grid Items
Use align-items, align-self, or place-items for Grid alignment.
Assuming middle Means Exact Centering
For inline elements, middle is calculated from font metrics rather than the parent's full height.
Confusing Percentage Alignment With Parent Height
Percentage values are relative to the element's own line height, not the containing block's height.
Using Large Pixel Offsets
Large offsets can increase line box height or create awkward spacing.
Forgetting Image Baseline Spacing
Inline images may leave space below their bottom edge because of baseline alignment.
Expecting sub and super to Change Font Size
These values change vertical positioning but do not automatically reduce font size.
Applying Length Values to Table Cells
Use supported table-cell alignment keywords instead of length or percentage offsets.
Confusing vertical-align With text-align
The text-align property controls horizontal inline content alignment. The vertical-align property controls supported vertical alignment behavior.
Important Considerations
Keep the following points in mind when using vertical-align:
- The property controls vertical alignment of inline-level elements and table-cell content.
- Its initial value is
baseline. - The property is not inherited.
- It does not vertically center ordinary block elements.
- It does not control Flexbox cross-axis alignment.
- It does not control Grid item alignment.
- The
baselinevalue aligns element baselines. - The
subvalue creates a subscript position. - The
supervalue creates a superscript position. - The
subandsupervalues do not automatically change font size. - The
text-topvalue aligns with the top of the parent's font. - The
text-bottomvalue aligns with the bottom of the parent's font. - The
middlevalue uses baseline and x-height calculations for inline elements. - The
topandbottomvalues align inline elements with line box edges. - Positive length values raise inline elements.
- Negative length values lower inline elements.
- Percentage values are based on the element's own line height.
- Aligned inline elements can affect line box height.
- Inline-block elements support vertical alignment.
- Inline images normally participate in baseline alignment.
- Table cells support top, middle, bottom, and baseline alignment.
- Length and percentage values are not used for ordinary table-cell alignment.
- Use semantic HTML for meaningful subscripts and superscripts.
- Use Flexbox or Grid for modern component alignment.
- Avoid excessive offsets that reduce readability.
- Test alignment with different fonts and font sizes.
- Check alignment when users zoom or enlarge text.
Browser Support
The CSS vertical-align property is widely supported across modern browsers and has been available for many years.
Its commonly used values, including baseline, middle, top, bottom, sub, super, lengths, and percentages, are broadly supported.
For current compatibility information, visit Can I Use? and search for vertical-align.
Standard Declaration
.example {
vertical-align: middle;
}
Table Cell Alignment
td {
vertical-align: top;
}
Feature Detection
@supports (vertical-align: middle) {
.example {
vertical-align: middle;
}
}
Vendor Prefixes
The standard vertical-align property does not require vendor prefixes in modern browsers.
Testing Recommendations
Test inline alignment with different font sizes, images, and line heights. For tables, verify that content is positioned correctly when cell heights vary.
Specifications
The vertical-align property is defined in CSS Inline Layout Module Level 3, with established behavior also described in CSS 2.2.
CSS Inline Layout Module Level 3 - Vertical Alignment
CSS 2.2 - The vertical-align Property
These specifications describe vertical alignment behavior for inline-level elements and table cells.
For additional documentation and examples, consult MDN Web Docs.
