CSS margin: auto

Do you need to set the width of an element to successfully use

margin: auto;

If so, how come?

Yes, we need to set a width. It can be fixed (px) or proportional (%, em, or rem).

The default width for all block level elements is 100% so there is no margin space. auto will give equal margins to both left and right so long as the element is less than the width of the window.

div {
    width: 95%;
    margin: 0 auto;

That will leave 25px margin on each side if the window width is 1000px.