HTML & CSS · Junior · Question 31 of 75

Which approach correctly centers a block horizontally and vertically inside its parent?

Explanation

Flexbox with justify-content and align-items: center is the most common approach (Grid alternative: display: grid; place-items: center). text-align centers only inline content, and vertical-align doesn't work for blocks. margin: auto on a regular block centers only horizontally (and only with a set width); vertically it works inside flex/grid.

Further reading

Problem with a question

HTML & CSS · Junior · question 31

Test yourself

Take the HTML & CSS · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz