【问题标题】:How to center Element which have display property inline block i use margin left and right auto but it is not work?如何将具有显示属性内联块的元素居中我使用左右边距自动但它不起作用?
【发布时间】:2022-07-19 19:53:56
【问题描述】:

我在 display : inline-block 中使用 margin: 0px auto;,但我的元素无法居中,但当我使用 display : block 时,它会居中。

.container {
  background-color: beige;
  border: 1px solid red;
  width: 250px;
  height: 200px;
  display: inline-block;
  margin: 0px auto;
}
<div class="container">
  This is Container
</div>

【问题讨论】:

    标签: css margin center


    【解决方案1】:

    您可以使用grid

    body {
      height: 100%;
      display: grid;
      place-items: center;
    }
    

    或flexbox

    body {
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 2018-06-09
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2019-09-29
      • 2013-09-02
      • 2017-05-17
      相关资源
      最近更新 更多