Home >Web Front-end >CSS Tutorial >How to Center a Child Element Within a Parent `` Using CSS?
When using CSS to center an element with left: 50%;, the alignment is relative to the entire window. However, when centering an element within a parent
To achieve this, we can use a combination of text-align: center; and margin: auto; CSS properties.
Step 1: Align the Parent Set text-align: center; to the parent Step 2: Center the Specific Child Using Margin Once the parent For clarity, here's an example: The above is the detailed content of How to Center a Child Element Within a Parent `` Using CSS?. For more information, please follow other related articles on the PHP Chinese website!#parent {
text-align: center;
background-color: blue;
height: 400px;
width: 600px;
}
.child {
height: 100px;
width: 200px;
text-align: left;
}
.center {
margin: auto;
background-color: green;
}