Home > OS >  Should we necessarily specify the width of an element to use margin auto?
Should we necessarily specify the width of an element to use margin auto?

Time:09-02

When i use just margin: auto for an element, it doesn't get centered, but when i add for the width a value it just get in the middle.

CodePudding user response:

You should add a width because by default the block-level elements such as a div, p, list, etc take up the full width of its parent element, and no space is left to adjust the element horizontally. So, the very first thing you need to check is whether you have set the width of the element for margin: auto to work.

CodePudding user response:

Adding a width to an element defines how much space (the margin) will be applied to the element. Different widths will have different margin: auto values.

  • Related