Four ways to center in CSS
The centering recipes worth knowing, from the margin trick to place-content.
.wrapper {
max-width: 68rem;
margin-inline: auto;
padding-inline: 1rem;
}
.hero {
display: grid;
place-content: center;
min-height: 60vh;
}
.modal {
position: fixed;
inset-block-start: 50%;
inset-inline-start: 50%;
transform: translate(-50%, -50%);
}
How it works
margin-inline: autowith a width centers a block.- Grid with
place-content: centercenters in both axes. - The transform recipe still wins for an absolute box.
Keywords and builtins used here
displaygridheromodalpositionremtransformtranslatevhwrapper
The run, in numbers
- Lines
- 18
- Characters to type
- 252
- Tokens
- 64
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 45 seconds.
Step 6 of 7 in Positioning, step 22 of 24 in Flexbox & grid.