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
Release: 2024-12-11 01:38:10
Original
488 people have browsed it

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template