【问题标题】:Responsive images side by side with same height different width具有相同高度不同宽度的响应图像并排
【发布时间】:2016-05-13 20:57:00
【问题描述】:

我的问题在图片中:

桌面分辨率示例:

在服务器中,我想使用默认尺寸的图片 (800x450px)。因此,我需要使用 css 调整图像大小和裁剪图像以适应容器 div's width 并保持行中所有图像的相同高度。然后在窗口调整大小图像必须保持纵横比。

HTML代码示例:

<div class="wrap">
<div class="container col-7">
    <img src="http://lorempixel.com/800/450/sports/1/" alt="">
    <h2>Title1</h2>
</div>
<div class="container col-5">
    <img src="http://lorempixel.com/800/450/sports/2/" alt="">
    <h2>Title2</h2>
</div>
<div class="container col-5">
    <img src="http://lorempixel.com/800/450/sports/3/" alt="">
    <h2>Title3</h2>
</div>
<div class="container col-4">
    <img src="http://lorempixel.com/800/450/sports/4/" alt="">
    <h2>Title4</h2>
</div>
<div class="container col-3">
    <img src="http://lorempixel.com/800/450/sports/5/" alt="">
    <h2>Title5</h2>
</div>

css 示例:

.wrap { 
    display: flex; 
    flex-direction: row; 
    flex-wrap: wrap;
}
.container { 
    display: flex; 
    flex-direction: column;
}
.container img { 
    width: 100%; 
    display: block; 
}

【问题讨论】:

  • 你在使用引导程序吗?
  • 没有。但是如果我需要,那么使用它是没有问题的
  • 好的,col- 类的 CSS 在哪里?我不确定您是否误用了引导类。
  • .col-12 { 宽度:100%; } .col-7 { 宽度:58.333333333%;} .col-5 { 宽度:41.666666667%;} .col-4 { 宽度:33.333333333%;} .col-3 { 宽度:25%;}

标签: javascript jquery html css image


【解决方案1】:

如果可能的话,一种简单的方法是将图像显示为 div 背景。然后您可以使用 css3 背景属性,例如

CSS:

div {
      background-image:url('image-url-here');
      background-repeat:no-repeat;
      background-size:cover;
      background-position: center; 
  }

例如,您的图像 div 可能类似于

HTML:

<div class="container col-7">
    <div id="id1" class="myImage"></div>
    <h2>Title1</h2>
</div>

CSS:

   .myImage {
              background-repeat:no-repeat;
              background-size:cover;
              background-position: center; 
              width: 100%;
              height: 450px;
            }

   #id1     {
              background-image:url('http://lorempixel.com/800/450/sports/1/');
            }

【讨论】:

  • 图像高度也必须响应并保持原始照片的纵横比。
  • 您可以将 .myImage 高度设置为响应式。 background-size 属性保持图像纵横比不变。
  • 以及如何做到这一点?
  • 您可以使用 vh 值,例如 height: 60vh; ,等于浏览器窗口高度的 60/100。使用 Bootstrap 和固定高度值会更容易,但 vh 应该也可以。
  • 感谢您的建议! a 使用了大众价值观。不是 vh
猜你喜欢
  • 1970-01-01
  • 2015-09-19
  • 2014-02-09
  • 2018-12-29
  • 2021-08-25
  • 1970-01-01
  • 2014-12-13
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多