↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

5 ways to center a div vertically and horizontally using CSS

The article gathers several CSS techniques for centering a div on a page. It frames the task as a common one and points to different layout tools that can place an element in the middle both horizontally and vertically.

Reading notes
#

  • Flex can center the child by setting the parent to display: flex and using justify-content: center with align-items: center, with fixed width and height on the parent.
  • Grid can center content with display: grid and place-items: center on the parent.
  • Grid can also center through the child by setting display: grid on the parent and using align-self: center and justify-self: center on the child.
  • A positioning approach sets the child to position: absolute and moves it to 50% from the top and left of the parent.
  • A margin approach uses margin: 0 auto on the child, though the author notes that this can be inconsistent and may need extra properties.
  • Another positioning method sets the child to position: absolute, uses top, left, right, and bottom as 0, and adds margin: auto.
  • The note on inset says it is shorthand for top, left, right, and bottom, and mentions Firefox support.
  • A bonus method uses CSS table layouts by setting display: table on a wrapper and display: table-cell on the parent.
  • In the table approach, the child can be centered either with margin or with display: inline-block.