Javatpoint Logo
Javatpoint Logo

Vertical Align in CSS

In CSS, the vertical-align property controls the vertical alignment of inline-level elements or table cells within their containing element. It applies to elements that are part of a line of text or are displayed as inline-block or table-cell.

The "vertical-align" property is commonly used for inline elements like images, text, or inline-block elements within a line of text. It does not apply directly to block-level elements; however, you can use techniques like flexbox or positioning to align them vertically.

Syntax:

Here's the basic syntax for the vertical-align property:

The "value" can be one of the following options:

  1. Baseline: Aligns the element's baseline with the baseline of its parent element. This is the default value for most elements.
  2. Sub: Aligns the element's baseline with the subscript baseline of the parent element's font.
  3. Super: Aligns the element's baseline with the superscript baseline of the parent element's font.
  4. Top: Align the top of the element with the top of the tallest element on the line within the line box.
  5. Text-top: Aligns the top of the element with the top of the parent element's font.
  6. Middle: Vertically centers the element relative to the parent element.
  7. Bottom: Align the bottom of the element with the bottom of the lowest element on the line within the line box.
  8. Text-bottom: Align the bottom of the element with the bottom of the parent element's font.
  9. Percentage: The element is aligned vertically at a specified percentage of the line height. For example, vertical-align: 50% will center the element vertically within its parent element.

NOTE: Remember that "vertical-align" has its specific behavior depending on the element type and the context in which it is used, so its effects might not always be straightforward. It is particularly useful for aligning inline elements with text or other inline elements.

Examples

Here are some more details and examples related to the "vertical-align" property in CSS:

1. Baseline Alignment:

  • The vertical-align baseline value aligns the baseline of the element with the baseline of its parent element. This is the default behavior for most inline-level elements.

2. Subscript and Superscript:

  • The vertical-align: Sub value aligns the element's baseline with the subscript baseline of the parent element's font, making it appear as a subscript. On the other hand,
  • vertical-align: Super aligns the element's baseline with the superscript baseline of the parent element's font.

3. Top and Bottom Alignment:

  • The vertical-align: Top value aligns the top of the element with the top of the tallest element on the line within the line box. Similarly,
  • vertical-align: The bottom aligns the bottom of the element with the bottom of the lowest element on the line within the line box.

4. Middle Alignment:

  • The vertical-align: Middle value vertically centers the element relative to the parent element. This is often used to center icons or images within text.

5. Text-top and Text-bottom Alignment:

  • The vertical-align: Text-top value aligns the top of the element with the top of the parent element's font and
  • vertical-align: Text-bottom aligns the bottom of the element with the bottom of the parent element's font.

6. Percentage Alignment:

Using a percentage value with vertical alignment allows you to align the element vertically at a specific line height percentage. For example, vertical-align: 50% will center the element at half the line height.

7. Vertical Centering of Block-Level Elements:

To vertically center a block-level element inside its parent, you can use the flexbox or grid layout.

8. Vertical Centering with Unknown Element Height:

If you don't know the height of the element you want to center vertically, you can use a combination of position and transform:

9. Vertical Centering with Multi-Line Text:

To vertically center multi-line text within a container, you can use a combination of flexbox and a pseudo-element:

10. Vertical Centering of Images in a Container with Different Aspect Ratios:

If you have images of various aspect ratios that you want to center within a container, you can use a combination of flexbox and object-fit:

HTML:

CSS:

11. Combining vertical align with line height:

You can combine the vertical-align property with the line-height property to achieve more precise vertical alignment, especially with larger font sizes.

12. Using the display Property for Alignment:

While vertical alignment primarily works with inline-level elements, you can alter the display property to achieve vertical alignment for block-level elements within specific contexts.

13. Vertical Alignment in Tables:

The vertical-align property is often used in table cells (<td>) to control the alignment of content within cells.

14. Inline-Block Elements Alignment:

You can use vertical alignment to align inline-block elements within a line of text, for example, icons alongside text.

These are just a few examples of handling vertical alignment in different scenarios. Depending on your specific layout and requirements, you might need to adapt or combine these techniques to achieve the desired results. CSS provides various tools to handle vertical alignment effectively in various contexts.

Remember that while the vertical-align property has uses, more comprehensive solutions exist for all alignment scenarios, especially block-level elements. For more complex layouts and alignment requirements, exploring modern CSS layout techniques like Flexbox, CSS Grid, or even CSS position values (such as absolute and relative) is recommended to achieve the desired results more effectively and predictably.

Remember that "vertical-align" only affects inline-level elements or table cells. Use techniques like flexbox, grid layout, or positioning to align block-level elements vertically.

Few More Examples

Test it Now

Output

How to vertically align text with CSS

Now, there is another example in which we are aligning the text with image.

Example

Test it Now

Output

How to vertically align text with CSS

Advantages of Vertical Align in CSS

  1. Easy to Use for Inline Elements: The vertical-align property is straightforward for aligning elements, such as images, icons, or text, within a line of text or other elements.
  2. Wide Browser Support: The vertical-align property has good browser support and is widely supported across different browsers.
  3. Multiple Alignment Options: It provides various alignment options, such as baseline, middle, top, bottom, text-top, text-bottom, subscript, and superscript, giving developers flexibility in aligning elements based on their requirements.
  4. Responsive Alignment: It can be used in responsive design to adapt the vertical alignment based on the container's size or available space.
  5. Simplicity for Inline Elements: For aligning small elements like icons or images within a line of text, the vertical-align property provides a relatively simple solution without requiring complex layout techniques.
  6. Fine-tuning: The property allows for fine-tuning the vertical position of elements, which can be useful for achieving specific design goals.
  7. Consistency with Table Cells: In the context of tables, the vertical-align property controls content alignment within table cells. This can help maintain consistency across table-based layouts.
  8. Combining with Text: It effectively aligns elements with text content, such as aligning icons or inline labels with adjacent text.
  9. Maintaining Aspect Ratios: When aligning images or icons within a line of text, vertical alignment can help maintain the aspect ratio of these elements, especially when combined with appropriate font sizes and line heights.
  10. Quick Alignment Fixes: When you need quick fixes for vertical alignment issues, especially in scenarios with mixed content, vertical alignment can provide a quick solution without requiring extensive layout restructuring.
  11. CSS Email Styling: In HTML emails, where complex layouts need to be better supported, using vertical alignment can be helpful for basic vertical alignment of elements without relying on external stylesheets or complex techniques.
  12. Compatible with display: inline-block: The vertical-align property is compatible with inline-block elements, allowing for easy vertical alignment of such elements within a line.
  13. Maintaining Consistency: For elements that are part of tabular data or need to align with similar elements across different rows or columns, vertical alignment can help maintain visual consistency.
  14. Browser Compatibility: Unlike some newer CSS techniques, vertical-align has been part of CSS for a long time and enjoys good cross-browser compatibility.

Disadvantages of Vertical Align in CSS

  1. Limited to Inline Elements: The most significant limitation of the vertical-align property is that it only works for inline-level elements or table cells. It does not directly apply to block-level elements. This can make vertical alignment more challenging for larger elements or complex layouts.
  2. Inconsistent Behavior: Vertical alignment can be tricky and inconsistent, especially with different font sizes, line heights, and nested elements. The same vertical alignment value may produce different results based on the context.
  3. Browser Quirks: Some older browsers might have inconsistent interpretations or quirks with the vertical-align property, which can lead to unexpected results. However, this issue has improved with the advancement of modern browsers.
  4. Limited Control over Spacing: The vertical-align property primarily deals with aligning elements vertically, but it only offers a little control over the spacing between elements. Adjusting the spacing often requires additional CSS or HTML modifications.
  5. Flexbox and Grid as Alternatives: For more complex layout requirements and vertical alignment of block-level elements, developers often rely on Flexbox or CSS Grid, which provide more robust and predictable solutions.
  6. Not Suitable for Full-Centering: While vertical alignment is useful for vertically aligning inline elements, it's suitable for full-centering (horizontally and vertically) block-level elements with additional CSS techniques.
  7. Misleading Name: The name "vertical-align" can be misleading because it doesn't align the element vertically in the way developers often expect. Instead, it controls the alignment of the element's content within its line box.
  8. Complexity with Different Fonts: The behavior of vertical alignment can be unpredictable when dealing with elements of different font sizes and line heights. This can make consistent vertical alignment challenging.
  9. Limited for Complex Layouts: It's unsuitable for complex layouts or scenarios where you must vertically align larger, block-level elements within a parent container.
  10. Cross-Browser Compatibility: While modern browsers have improved support for vertical alignment, older browsers might still exhibit inconsistencies or unexpected behavior.
  11. Alternative Techniques: Modern CSS layout techniques like Flexbox and CSS Grid offer more powerful and predictable ways to handle complex layout requirements, including vertical alignment of both inline and block-level elements.
  12. Accessibility Considerations: Using vertical alignment for layout might not be the most accessible approach, as it could interfere with screen readers and other assistive technologies. Semantic HTML and proper CSS techniques are often better choices for accessibility.
  13. Debugging Challenges: Debugging unexpected behavior or alignment issues related to vertical alignment can sometimes be tricky, especially when dealing with nested elements and varying font sizes.
  14. Evolution of Web Layout: As the web development landscape evolves, new layout techniques like CSS Grid Layout and Flexbox provide more modern and comprehensive solutions for layout challenges, potentially making vertical alignment less relevant for many scenarios.

Overall, while the vertical-align property is handy for aligning inline elements or table cells within a line of text, developers often need other CSS techniques for more advanced layout and positioning requirements, especially when dealing with block-level elements or complex layouts. CSS Flexbox and CSS Grid are powerful alternatives for broader alignment and positioning control.

Conclusion

The vertical-align property is useful for aligning inline elements within text or table cells. However, it has limitations and can be challenging to use effectively for complex layouts or block-level elements. Developers should consider modern CSS layout techniques that provide more control and flexibility over alignment and positioning.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA