Home >Web Front-end >CSS Tutorial >How to Vertically Center a Div Within Its Parent Using CSS?

How to Vertically Center a Div Within Its Parent Using CSS?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-11 01:38:10470browse

How to Vertically Center a Div Within Its Parent Using CSS?

Vertically Centering a

Within Its Parent Element Using CSS

In this query, the user seeks assistance in vertically aligning the "Username" and "Form" divs within the "Login" parent div. They have attempted to use the "vertical-align: middle;" property without success.

SOLUTION

The optimal solution for vertically centering divs in contemporary browsers is Flexbox:

#Login {
    display: flex;
    align-items: center;
}

In cases where Flexbox support is lacking (e.g., IE 9 and below), a fallback option via older methods is required.

ADDITIONAL RESOURCES

  • [Browser Support for Flexbox](https://caniuse.com/?search=flexbox)
  • [A Guide to Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/)
  • [Using CSS Flexible Boxes](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/)

The above is the detailed content of How to Vertically Center a Div Within Its Parent Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn