CSS “margin: 0 auto” not centering

☆樱花仙子☆ 提交于 2019-12-01 04:48:11

It is working.

The problem is you're centering a div, which is a block-level element by default, and which therefore occupies 100% width of its parent (body, in this case). So there's no space to move horizontally, hence no space to center.

For an illustration see this revised demo, which has an added border around .container.

If you reduce the width of .container you'll see the centering work. Here's a second revised demo with width: 50% applied to .container.

It actually works, but without specifying the width it takes full 100%. Try something like:

   .container {
    margin: 0 auto;
    width:50%;
    }

Hope this may help

You should specify the width of the div for margin:auto to work. try to use width in percentage to make your div responsive as well.

You should set a width on .container to let the margin: 0 auto; work. See the updated JSfiddle.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!