How to center things - display:block/inline-block

后端 未结 3 1631
误落风尘
误落风尘 2020-12-29 03:31

When centering things in html and css, I find two approaches - either applying on the element :

display:block;
margin:0 auto;

or using:

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 04:12

    Block elements should always be centered using

    .block {
        margin-left: auto;
        margin-right: auto;
        width: 600px;
    }
    

    as stated by the w3c: http://www.w3.org/Style/Examples/007/center.en.html#block

    text-align: center;
    

    is well-named: use it to center texts.

    Update

    You can also use display: flex now:

    .parent {
        display: flex;
        justify-content: center;
    }
    .block {
        width: 200px;
    }
    

提交回复
热议问题