【问题标题】:Make two column responsive images of same size制作相同大小的两列响应式图像
【发布时间】:2016-08-12 09:27:15
【问题描述】:

我有不同尺寸的图片,但想让它们在页面上的宽度和高度看起来相等。我做了什么:

HTML

<div class="col-lg-6">
  <img src = "assets/img/101.jpg" alt="Null" />
</div>

CSS

#images img {
  width: 100%;
  padding-bottom: 4em;
  position: center;
}

但它们看起来像这样:

他们有不同的高度。 如何使其相等? 可能我需要在不改变宽度的情况下按高度和中心裁剪它们。有可能吗?

来自 cmets 的解释

我建议最好裁剪它们,而不是调整大小。居中并裁剪为较小的尺寸。

【问题讨论】:

  • 如果图像有不同的高度。除非您设置它们的高度,否则它们在网络中的高度不会相同。
  • 您是否意识到如果在不保持纵横比的情况下调整图像大小,它们会看起来很糟糕?只是说...
  • 是的,我意识到了这一点。这就是为什么我建议裁剪它们而不是调整大小可能会很好。居中并裁剪为较小的尺寸。

标签: html css twitter-bootstrap image-size


【解决方案1】:

要更好地控制您的图片,最好切换到背景图片。然后你可以使用background-size: cover来填写div。

HTML

<div class="col-lg-6">
  <div class="image-holder" style="background-image: url('assets/img/101.jpg');">
    &nbsp;
  </div>
</div>
<div class="col-lg-6">
  <div class="image-holder" style="background-image: url('assets/img/101.jpg');">
    &nbsp;
  </div>
</div>

CSS

.image-holder {
  background-size: cover;
  background-position: center center;
  height: 400px;
  width: 100%;
}

如果您的图片只是静态资产,最好在 css 中包含图片引用。

【讨论】:

    【解决方案2】:

    您不能同时为图像设置高度和宽度 - 它会将它们拉伸。已根据要求将其与col-lg-6 一起制作为 2 列 flexbox。

    请看下面的演示:

    .box {
      border: 1px solid #ddd;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      flex: 1 50%;
      min-width: 200px; 
    }
    .container {
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
    }
    
    .col-lg-6.box {
       padding: 15px;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css" />
    
    <div class="container">
      <div class=" col-lg-6 box">
        <img src="http://placehold.it/150x100" alt="Null" />
      </div>
      <div class=" col-lg-6 box">
        <img src="http://placehold.it/75x75" alt="Null" />
      </div>
      <div class=" col-lg-6 box">
        <img src="http://placehold.it/150x50" alt="Null" />
      </div>
      <div class=" col-lg-6 box">
        <img src="http://placehold.it/150x150" alt="Null" />
      </div>
    </div>

    【讨论】:

    • 好的,我试过了,它按预期工作,谢谢。但我也在尝试使用 col-lg-6 将它们的照片对齐在两列中,但由于某种原因它不能一起工作。
    • @TheKitMurkit 使它与col-lg-6 一起工作。请检查并让我知道您的反馈。谢谢
    猜你喜欢
    • 2019-08-05
    • 2019-02-12
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2018-04-22
    相关资源
    最近更新 更多