【问题标题】:How to put the multiple images on the center of multiple backgrounds [duplicate]如何将多个图像放在多个背景的中心[重复]
【发布时间】:2018-04-01 23:52:36
【问题描述】:

如何在背景上垂直和水平居中图像?

我有这个代码:

.lineargradientgreen {
  background-image: linear-gradient(green, white);
  border-width: 2pt 2pt 2pt 2pt;
  height: 500px;
  width: 500px;
  float: left;
  text-align: center;
}

.lineargradientblue {
  background-image: linear-gradient(to top left, white, blue);
  border-width: 2pt 2pt 2pt 2pt;
  height: 500px;
  width: 500px;
  float: left;
  text-align: center;
}
<div class="lineargradientgreen">
  <img src="https://www.listefit.com/wp-content/uploads/2017/12/tiny-png-google-g%C3%B6rsel-optimizasyonu.jpg" alt="Hot Air Balloon" width="200px" height="200px" />
</div>

<div class="lineargradientblue">
  <img src="https://www.samcodes.co.uk/project/geometrize-haxe-web/assets/images/xseagull.jpg.pagespeed.ic.iK66EGA15-.jpg" alt="Hot Air Balloon" width="200px" height="200px" />
</div>

最后,我得到的图像只在两个背景上水平居中,我怎样才能让它们垂直居中?

【问题讨论】:

    标签: css html


    【解决方案1】:

    这很简单,只需替换:

    text-align: center;
    

    与:

    box-sizing: border-box;
    padding: 150px;
    

    给它一个填充是盒子大小500px和图像大小200px之间差异的一半:

    500 - 200 = 300 / 2 = 150。

    .lineargradientgreen {
      background-image: linear-gradient(green, white);
      border-width: 2pt 2pt 2pt 2pt;
      height: 500px;
      width: 500px;
      float: left;
      box-sizing: border-box;
      padding: 150px;
    }
    
    .lineargradientblue {
      background-image: linear-gradient(to top left, white, blue);
      border-width: 2pt 2pt 2pt 2pt;
      height: 500px;
      width: 500px;
      float: left;
      box-sizing: border-box;
      padding: 150px;
    }
    <div class="lineargradientgreen">
      <img src="https://placehold.it/200x200" alt="Hot Air Balloon" />
    </div>
    
    <div class="lineargradientblue">
      <img src="https://placehold.it/200x200" alt="Hot Air Balloon" />
    </div>

    【讨论】:

      【解决方案2】:

      使用Flexbox

      对于水平中心 -> justify-content:center 在父级

      对于垂直中心 -> align-self:center 在孩子身上

      .lineargradientgreen {
        display: flex;
        justify-content: center;
        background-image: linear-gradient(green, white);
        border-width: 2pt 2pt 2pt 2pt;
        height: 500px;
        width: 500px;
        float: left;
        text-align: center;
      }
      
      .lineargradientblue {
        display: flex;
        justify-content: center;
        background-image: linear-gradient(to top left, white, blue);
        border-width: 2pt 2pt 2pt 2pt;
        height: 500px;
        width: 500px;
        float: left;
        text-align: center;
      }
      
      div img {
        width: 300px;
        align-self: center;
      }
      <div class="lineargradientgreen">
        <img src="https://www.samcodes.co.uk/project/geometrize-haxe-web/assets/images/xseagull.jpg.pagespeed.ic.iK66EGA15-.jpg" alt="Hot Air Balloon" />
      </div>
      
      <div class="lineargradientblue">
        <img src="https://www.listefit.com/wp-content/uploads/2017/12/tiny-png-google-g%C3%B6rsel-optimizasyonu.jpg" alt="Hot Air Balloon" />
      </div>

      Flexbox 支持的浏览器:当您在 Google 上搜索时

      【讨论】:

      • 酷,非常感谢!!
      • 如果回答有帮助,请将其标记为正确。
      • 使用 flexbox 是一个很好的现代解决方案,但如果您想支持旧版浏览器,则不能使用它。我认为对于问题中的简单案例,这是一种矫枉过正,因为它可以在没有 flexbox 的情况下轻松完成,并且您无需担心浏览器支持。
      • 如果 OP 对旧版浏览器相当精确,那他肯定不能用 Flex。但由于没有指定,flexbox 非常方便。您的解决方案也很棒@RacilHilan :)
      • 当您想检查对某些 CSS、JavaScript 或 HTML 功能的支持时,请查看更好的网站,例如 caniuse 或 MDN ;)。 w3school 也不错,虽然很多开发者不喜欢它,因为它过去不准确,虽然现在好多了,但他们仍然持反对意见。
      【解决方案3】:


      有几种方法可以做到这一点,比如使用它 flexbox、display、position 等。centering css complete guide

      例如使用位置:(所有旧浏览器都支持,例如 IE :D)

      .lineargradientgreen {
        background-image: linear-gradient(green, white);
        border-width: 2pt 2pt 2pt 2pt;
        height: 500px;
        width: 500px;
        float: left;
        text-align: center;
      }
      
      .lineargradientblue {
        background-image: linear-gradient(to top left, white, blue);
        border-width: 2pt 2pt 2pt 2pt;
        height: 500px;
        width: 500px;
        float: left;
        text-align: center;
      }
      
      .myclass{
      	position: relative;
      }
      .myclass > img{
      	display: block;
      	position: absolute;
      	top: 0;
      	bottom: 0;
      	left: 0;
      	right: 0;
      	margin: auto;
      }
      <div class="myclass lineargradientgreen">
        <img src="https://www.listefit.com/wp-content/uploads/2017/12/tiny-png-google-g%C3%B6rsel-optimizasyonu.jpg" alt="Hot Air Balloon" width="200px" height="200px" />
      </div>
      
      <div class="myclass lineargradientblue">
        <img src="https://www.samcodes.co.uk/project/geometrize-haxe-web/assets/images/xseagull.jpg.pagespeed.ic.iK66EGA15-.jpg" alt="Hot Air Balloon" width="200px" height="200px" />
      </div>

      【讨论】:

      • 没有必要说“愚蠢”。这是一个专门用于编程问答的网站,所以请保持专业。
      • @RacilHilan 好的,已编辑:d
      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2014-04-18
      • 2019-08-12
      • 1970-01-01
      • 2012-07-22
      相关资源
      最近更新 更多