CSS “margin: 0 auto” not centering

前端 未结 5 986
独厮守ぢ
独厮守ぢ 2021-01-12 04:40

Okay I understand that this topic has been covered. But I have looked at various solutions and have had little success with them.

I just have no clue why this

5条回答
  •  时光说笑
    2021-01-12 04:54

    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.

提交回复
热议问题