Home > Article > Web Front-end > How to center child elements in css3
Method: 1. Use the "parent element {display:flex}" statement to set the parent element to a flexible layout; 2. Use the "parent element {align-items:center}" statement to vertically center the child element; 3. , use the "parent element {justify-content:center}" statement to center the child element horizontally.

The operating environment of this tutorial: Windows 10 system, CSS3&&HTML5 version, Dell G3 computer.
How to set the child element to be centered in css3
We can set the parent element to a flexible layout through the display:flex style, which is used for the parent element. Elements provide maximum flexibility.
align-items property defines the alignment of flex items in the direction of the cross axis (vertical axis) of the current row of the flex container.
justify-content is used to set or retrieve the alignment of the flexible box element in the main axis (horizontal axis) direction.
The example is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>盒子完美居中</title>
<style>
.con {
width: 300px;
height: 300px;
border: 1px solid red;
margin: 100px auto;
display: flex;
justify-content: center; /*水平方向的居中*/
align-items: center; /*垂直方向的居中*/
}
.box {
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="con">
<div class="box"></div>
</div>
</body>
</html>Output result:

(Learning video sharing: css video tutorial)
The above is the detailed content of How to center child elements in css3. For more information, please follow other related articles on the PHP Chinese website!