Tillitsdone
down Scroll to discover
Mastering CSS Vertical-Align for Web Development
Learn about the CSS vertical-align property, its use cases, and available options for aligning elements vertically.

Enhance your web design skills with this comprehensive guide.
thumbnail

Vertical Alignment in CSS

The vertical-align property in CSS controls the vertical positioning of elements within a line box. This includes aligning images with text and ensuring elements are properly positioned relative to each other. By using different vertical-align values, you can achieve various alignment effects, enhancing the overall appearance and readability of your web content.

Syntax

vertical-align: value;

Values

  • Keyword Values:
    • baseline
    • sub
    • super
    • text-top
    • text-bottom
    • middle
    • top
    • bottom
  • Length Values:
    • 10em
    • 4px
  • Percentage Values:
    • 20%
  • Global Values:
    • inherit
    • initial
    • revert
    • revert-layer
    • unset

Examples

To align an element to the baseline of its parent:

vertical-align: baseline;

To align an element to the top of its parent’s font:

vertical-align: text-top;

To raise an element by 10 pixels above the baseline:

vertical-align: 10px;

To position an element 20% above the baseline:

vertical-align: 20%;

Values for Inline Elements

Parent-Relative Values

  • baseline: Aligns the baseline of the element with the baseline of its parent.
  • sub: Aligns the baseline of the element with the subscript-baseline of its parent.
  • super: Aligns the baseline of the element with the superscript-baseline of its parent.
  • text-top: Aligns the top of the element with the top of the parent element’s font.
  • text-bottom: Aligns the bottom of the element with the bottom of the parent element’s font.
  • middle: Aligns the middle of the element with the baseline plus half the x-height of the parent.
  • <length>: Aligns the baseline of the element to the given length above the baseline of its parent.
  • <percentage>: Aligns the baseline of the element to the given percentage above the baseline of its parent, with the value being a percentage of the line-height property.

Line-Relative Values

  • top: Aligns the top of the element and its descendants with the top of the entire line.
  • bottom: Aligns the bottom of the element and its descendants with the bottom of the entire line.

Values for Table Cells

  • top: Aligns the content to the top of the table cell.
  • bottom: Aligns the content to the bottom of the table cell.
  • middle: Aligns the content to the middle of the table cell.
  • baseline: Aligns the content to the baseline of the table cell.
  • <length>: Aligns the content to the given length above the baseline of the table cell.
  • <percentage>: Aligns the content to the given percentage above the baseline of the table cell, with the value being a percentage of the line-height property.

Examples of vertical-align in Table Cells

To align the content to the top of a table cell:

td {
vertical-align: top;
}

To align the content to the middle of a table cell:

td {
vertical-align: middle;
}

To align the content to the bottom of a table cell:

td {
vertical-align: bottom;
}

Example Usage

To create a table where cells are aligned differently:

HTML:

<table>
<tr>
<td class="baseline">Baseline</td>
<td class="top">Top</td>
<td class="middle">Middle</td>
<td class="bottom">Bottom</td>
</tr>
</table>

CSS:

table {
width: 100%;
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: 10px;
}
.baseline {
vertical-align: baseline;
}
.top {
vertical-align: top;
}
.middle {
vertical-align: middle;
}
.bottom {
vertical-align: bottom;
}

Basic Example

HTML:

<div>
An <img src="frame_image.svg" alt="link" width="32" height="32" /> image with a default alignment.
</div>
<div>
An <img class="top" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-top alignment.
</div>
<div>
An <img class="bottom" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-bottom alignment.
</div>
<div>
An <img class="middle" src="frame_image.svg" alt="link" width="32" height="32" /> image with a middle alignment.
</div>

CSS:

img.top {
vertical-align: text-top;
}
img.bottom {
vertical-align: text-bottom;
}
img.middle {
vertical-align: middle;
}

Example: Vertical Alignment in a Line Box

HTML:

<p>
top: <img style="vertical-align: top" src="star.png" alt="star"/>
middle: <img style="vertical-align: middle" src="star.png" alt="star"/>
bottom: <img style="vertical-align: bottom" src="star.png" alt="star"/>
super: <img style="vertical-align: super" src="star.png" alt="star"/>
sub: <img style="vertical-align: sub" src="star.png" alt="star"/>
</p>
<p>
text-top: <img style="vertical-align: text-top" src="star.png" alt="star"/>
text-bottom: <img style="vertical-align: text-bottom" src="star.png" alt="star"/>
0.2em: <img style="vertical-align: 0.2em" src="star.png" alt="star"/>
-1em: <img style="vertical-align: -1em" src="star.png" alt="star"/>
20%: <img style="vertical-align: 20%" src="star.png" alt="star"/>
-100%: <img style="vertical-align: -100%" src="star.png" alt="star"/>
</p>

CSS:

img {
margin-right: 0.5em;
}
p {
height: 3em;
padding: 0 0.5em;
font-family: monospace;
text-decoration: underline overline;
margin-left: auto;
margin-right: auto;
width: 80%;
}

Example: Vertical Alignment in a Table Cell

HTML:

<table>
<tr class="bottom">
<td class="baseline">baseline</td>
<td class="top">top</td>
<td class="middle">middle</td>
<td>bottom</td>
<td>Row's style</td>
<td>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse pretium felis eu sem mattis vulputate.
</td>
</tr>
</table>

CSS:

.baseline {
vertical-align: baseline;
}
.top {
vertical-align: top;
}
.middle {
vertical-align: middle;
}
.bottom {
vertical-align: bottom;
}

Browser Compatibility

The vertical-align property is widely supported across all major web browsers, ensuring that you can reliably use it to control the vertical alignment of elements in your web designs.

Browser Compatibility Table

BrowserVersionRelease Date
Chrome1.0December 2008
Edge12.0July 2015
Firefox1.0November 2004
Internet Explorer4.0September 1997
Safari1.0June 2003
Opera4.0June 2000

Practical Tips

  • Testing: Always test your web designs across multiple browsers to ensure consistent appearance and functionality.
  • Polyfills and Fallbacks: For older browsers that may have limited support for certain CSS properties, consider using polyfills or fallbacks to maintain compatibility.
  • Documentation: Refer to the official CSS specifications and browser documentation for the most up-to-date information on compatibility and any known issues.

See Also

For further exploration and understanding of related CSS properties and concepts, you may find the following resources helpful:

  • [Typical Use Cases of Flexbox]WebsiteUrl: Learn about the various applications of Flexbox, including how to center items within a container.
  • [line-height]WebsiteUrl: Understand how the line-height property affects the vertical spacing of text and how it interacts with vertical-align.
  • [text-align]WebsiteUrl: Explore the text-align property for horizontal text alignment, which complements vertical-align for comprehensive text layout control.
  • [margin]WebsiteUrl: Learn about the margin property and how it can be used to create space around elements, enhancing layout and readability.
  • [Understanding vertical-align]WebsiteUrl: Delve into an in-depth explanation of the vertical-align property, including its nuances and practical examples.
  • [Vertical-Align: All You Need To Know]WebsiteUrl: A comprehensive guide on the vertical-align property, covering its various uses and best practices.
  • [HTML Reference Guide]WebsiteUrl: Access a comprehensive reference for HTML elements, which can help you understand how to structure your web content effectively.
Get in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.