CSS
π― CSS vertical-align Property (Vertical Alignment in CSS)
The vertical-align property defines how an inline element aligns vertically relative to surrounding text or elements.
It is commonly used with images, inline elements, table cells, superscripts, and subscripts.
π‘ Common Usage Areas
- Images inside text
- Inline elements (span, img, button)
- Table cells
- Superscripts and subscripts
π§© Simple Definition
vertical-align = controls vertical alignment of inline elements.
π‘ Why Is It Useful?
By default, inline elements align according to the text baseline.
Sometimes this creates awkward positioning, especially for:
- Images next to text
- Icons inside buttons
- Mathematical formulas
- Table content alignment
The vertical-align property helps fix these alignment problems.
π§ Basic Syntax
vertical-align: value;
π§© Example 1 β Vertically Center an Image
<p>
Text with an image:
<img
src="image.png"
style="vertical-align: middle;"
alt="example"
>
aligned like this.
</p>
π§ Explanation
vertical-align: middle;
π The image becomes vertically centered relative to the surrounding text.
This creates a cleaner inline appearance.
π§© Example 2 β Align to the Top
<p>
Text with an image:
<img
src="image.png"
style="vertical-align: middle;"
alt="example"
>
aligned like this.
</p>
π§ Explanation
vertical-align: top;
π The image aligns with the top of the text line.
π§© Example 3 β Subscript and Superscript
H<sub style="vertical-align: sub;">2</sub>O
x<sup style="vertical-align: super;">2</sup>
π§ Explanation
sub creates a subscript effect.
Example: HβO
super creates a superscript effect.
Example: xΒ²
These are commonly used in mathematics, chemistry, and scientific notation.
βοΈ Supported Values
| Value | Description |
|---|---|
| baseline | Aligns to the text baseline (default) |
| top | Aligns to the top |
| middle | Vertically centers the element |
| bottom | Aligns to the bottom |
| text-top | Aligns with top of text |
| text-bottom | Aligns with bottom of text |
| sub | Creates subscript alignment |
| super | Creates superscript alignment |
| length | Moves element using fixed units |
| % | Moves element using percentage |
| initial | Resets to default value |
| inherit | Inherits from parent element |
πΉ Common Values
baseline (Default)
vertical-align: baseline;
π Aligns the element with the normal text baseline.
middle
vertical-align: middle;
π Centers the element vertically relative to nearby text.
top
vertical-align: top;
π Aligns the element to the top of the line.
bottom
vertical-align: bottom;
π Aligns the element to the bottom of the line.
Using Length Values
vertical-align: 10px;
π Moves the element upward or downward using a fixed amount.
Using Percentage Values
vertical-align: 20%;
π Adjusts vertical positioning using percentage values.
β οΈ Important Note
The vertical-align property mainly works with:
- inline elements
- inline-block elements
- table cells
β It does not work properly on normal block elements like:
For block-level vertical alignment, developers usually use:
- Flexbox
- CSS Grid
- Positioning techniques
π¨ Common Use Cases
The vertical-align property is commonly used for:
- Aligning icons with text
- Positioning inline images
- Creating superscripts/subscripts
- Table-cell alignment
- Fixing icon/button alignment
π§© Practical Example
.icon {
vertical-align: middle;
}
π Commonly used to align icons neatly beside text.
βοΈ Difference Between text-top and top
| Value | Behavior |
|---|---|
| top | Aligns relative to line box |
| text-top | Aligns relative to text top |
-
π§ In Summary
- vertical-align controls vertical alignment of inline elements.
- Most common values:
- middle β centers vertically
- top β aligns to top
- bottom β aligns to bottom
- sub β creates subscript
- super β creates superscript
