【问题标题】:Make images that have different heights scale within a container使具有不同高度的图像在容器内缩放
【发布时间】:2016-04-04 03:34:17
【问题描述】:

我有三个图像放在一个容器中。其中两张图片的高度相同,而另一张略高。当用户调整屏幕大小时,如何让所有三个图像在缩小时保持当前比例?

HTML

<div class='wrapper-inner panel centered green'>
<section class='content'>
  <article class='full'>
    <h2>Title of section four.</h2>
    <div class="phone-container">

      <img src='http://placehold.it/276x490' class="phone-1 desktop" style="border: 1px solid blue;" />
      <img src='http://placehold.it/320x516' class="phone-2" style="border: 1px solid red;" />
      <img src='http://placehold.it/276x490'class="phone-3 desktop" style="border: 1px solid blue;" />

    </div>
  </article>
</section>

CSS

.desktop {
  display: none;
}

@media only screen and (min-width: 768px) {
  .phone-container {
    margin: 0 auto;
    position: relative;
  }
  .phone-container img {
    display: inline-block;
    margin-bottom: -1px;
    margin-top: 0 !important;
    position: relative;
    max-width: 320px !important;
  }
  .phone-container .phone-1 {
    width: 276px;
    -webkit-transform: translate(50px, 0);
    transform: translate(50px, 0);
    z-index: 2;
    position: relative;
  }
  .phone-container .phone-3 {
    width: 276px;
    -webkit-transform: translate(-50px, 0);
    transform: translate(-50px, 0);
    z-index: 2;
    position: relative;
  }
  .phone-container .phone-2 {
    width: 320px;
    z-index: 3;
    position: relative;
  }
  .centered {
    text-align: center;
  }
}

从我的jsFiddle 中,您可以看到我的图像彼此内联显示,两个外部图像位于中间图像之下。这是我想要在浏览器窗口变小直到移动查询启动时保持的定位。

【问题讨论】:

    标签: html css image


    【解决方案1】:

    也许使用百分比宽度可能是一个解决方案?

    Like this

    @media only screen and (max-width: 968px) {
      .phone-container {
        margin: 0 auto;
        position: relative;
      }
      .phone-container img {
        display: inline-block;
        margin-bottom: -1px;
        margin-top: 0 !important;
        position: relative;
        max-width: 30% !important;
      }
      .phone-container .phone-1 {
        width: 20%;
        -webkit-transform: translate(50px, 0);
        transform: translate(50px, 0);
        z-index: 2;
        position: relative;
      }
      .phone-container .phone-3 {
        width: 20%;
        -webkit-transform: translate(-50px, 0);
        transform: translate(-50px, 0);
        z-index: 2;
        position: relative;
      }
      .phone-container .phone-2 {
        width: 25%;
        z-index: 3;
        position: relative;
      }
      .centered {
        text-align: center;
      }
    }
    

    【讨论】:

    • 感谢您的意见。正如您在示例中调整屏幕大小一样,图像仍然会中断对齐。
    • 我可以更改媒体查询以使其更好地工作。像这样:jsfiddle.net/3140e2g4/3
    • 谢谢!虽然,我不希望它依赖媒体查询,而是基于百分比。原因是我的设计在 768 像素处中断,然后这就是摆脱两个图像的原因。
    • 是的!你具体调整了什么?
    • 因此,当我对图像应用最大宽度百分比时,它们似乎会继续中断。将 30% 应用于最大宽度可以解决问题,但会打乱图像的初始大小。做 40% 得到正确的大小,但不会缩放。
    猜你喜欢
    • 2011-08-15
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2016-11-12
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多