【问题标题】:Why margin:0 auto can't center image in div levelly?为什么 margin:0 auto 不能在 div 中水平居中图像?
【发布时间】:2017-11-29 23:28:34
【问题描述】:

图片元素可以用margin:0px 50px 0px 50px;水平居中。

.wrapper {
  width: 175px;
  height: 100px;
  border: 1px solid red;
}

img {
  margin: 0px 50px 0px 50px;
}
<div class="wrapper">
  <img src="http://i.imgur.com/9OIT14w.jpg" alt="">
</div>

在这种情况下,margin:0 auto; == margin:0px 50px 0px 50px;.
所以等于把margin:0px 50px 0px 50px;写成margin:0 auto;
为什么不能以margin:0 auto; 为中心?

.wrapper {
  width: 175px;
  height: 100px;
  border: 1px solid red;
}

img {
  margin: 0 auto;
}
<div class="wrapper">
  <img src="http://i.imgur.com/9OIT14w.jpg" alt="">
</div>

【问题讨论】:

  • 它适用于前一种情况,因为您有固定边距,并且您可以将固定边距应用于内联元素。在第二个示例中,您需要添加 display: block - 这是将元素与 margin: auto 居中所必需的。

标签: html css


【解决方案1】:

你想念display:block

.wrapper {
  width: 175px;
  height: 100px;
  border: 1px solid red;
}

.img1 {
  margin: 0 auto;
  display: block
}
<div class="wrapper">
  <img class="img1" src="http://i.imgur.com/9OIT14w.jpg" alt="">
</div>

【讨论】:

    【解决方案2】:

    我确信有更好/更有效的方法来做到这一点。但是……

    这是我用来在div 中垂直和水平居中图像的方法,同时维护 div 的固定尺寸。图片永远不会裁剪/拉伸

    body {
      text-align: center;
      margin: 0 auto;
    }
    
    .my-image-parent {
      margin:1em auto;
      width: 350px;
      max-width:100%;
      height: 200px;
      line-height: 200px; /* should match your div height */
      text-align: center;
      font-size: 0;
      background: #131418;
    }
    
    
    /*fluff */
    .bg1 {background: url(https://unsplash.it/799/799);}
    .bg2 {background: url(https://unsplash.it/800/400);}
    .bg3 {background: url(https://unsplash.it/400/800);}
    /* end fluff */
    
    
    .my-image {
      width: auto;
      height: 100%;
      vertical-align: middle;
      background-size: contain;
      background-position: center;
      background-repeat: no-repeat;
    }
    <h4>Works for square, landsacape and portrait images.</h4>
    
    <div class="my-image-parent">
      <div class="my-image bg1"></div>
    </div>
    <br>
    <div class="my-image-parent">
      <div class="my-image bg2"></div>
    </div>
    <br>
    <div class="my-image-parent">
      <div class="my-image bg3"></div>
    </div>

    【讨论】:

      【解决方案3】:

      由于img 标签是inline-block 元素,margin: 0 auto; 将不起作用。它的显示属性必须设置为display: block;

      .wrapper {
        width: 175px;
        height: 100px;
        border: 1px solid red;
      }
      
      img {
        margin: 0 auto;
        display: block;
      }
      <div class="wrapper">
        <img src="http://i.imgur.com/9OIT14w.jpg" alt="">
      </div>

      您还可以为外包装添加text-align: center; 以使图像居中。

      .wrapper {
        width: 175px;
        height: 100px;
        border: 1px solid red;
        text-align: center;
      }
      <div class="wrapper">
        <img src="http://i.imgur.com/9OIT14w.jpg" alt="">
      </div>

      编辑:

      inline 和 inline-block 元素没有 width 属性,因此无法计算“auto”。

      参考:Why centering with margin 0 auto works with display:block but does not work with display:inline-block ?

      【讨论】:

        猜你喜欢
        • 2016-04-05
        • 2010-11-01
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 2018-06-08
        • 1970-01-01
        • 2015-07-09
        • 2012-01-27
        相关资源
        最近更新 更多