Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use flex to realize horizontal and Vertical centering in CSS

Shulou Source: shulou.com Published: 2022-06-01 05:56:15 10月15日 Update

This article mainly explains "how to use flex to achieve horizontal vertical center in CSS". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use flex to achieve horizontal vertical center in CSS".

Basic structural style:

.box {

Width: 400px

Height: 400px

Background-color: red

}

.inner {

Width: 100px

Height: 100px

Background-color: blue

}

1. Using flex layout

Start the flex layout with the parent element and set justify-content: center; align-items: center;.

Add a style:

.box {

Display: flex

Justify-content: center

Align-items: center

}

two。 Using flex+margin

The parent element sets the display: flex;, child element sets margin: auto;.

Add a style:

.box {display: flex;}

.inner {margin: auto;}

Thank you for your reading, the above is the content of "how to use flex to achieve horizontal vertical center in CSS". After the study of this article, I believe you have a deeper understanding of how to use flex to achieve horizontal vertical center in CSS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Level element style learning content layout that is ideas situations articles more knowledge knowledge points articles structure follow problems practice push research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Apple Shulou Tech Info Huawei