使两个响应图像具有相同的高度

编程入门 行业动态 更新时间:2024-10-22 18:32:24
本文介绍了使两个响应图像具有相同的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将图像width:100%(或50%)用于img,这会自动计算高度.有时候可以,但是我不可以.

I am using image width:100% (or 50%) for img, this calculates height automatically. This is ok sometimes, but not in my case.

我需要在同一高度的一行中显示两个图像,但是原始图像具有不同的高度(因此结果两个图像也具有不同的高度).

I need to display two images in a line with the same height, but original images has different height (so in the result two images also have different height).

<div class="col-md-7 horizontal-list-media"> <img src="moneyti.co/wp-content/uploads/2016/01/16-Zingis-RUS.png" style="width: 50%" class="img-responsive"> <img src="moneyti.co/wp-content/uploads/2016/01/16-kazino-RUS-360x240.png" style="width: 50%" class="img-responsive"> </div>

由于两个图像都具有不同的高度,因此生成的图像也具有不同的高度.我不希望这样.如何使两个图像具有相同的高度?请记住,当屏幕尺寸改变时,图像应该具有响应性,因此,我想我不能简单地同时添加两个图像的height属性.

As both images have different height, then the resulted images also has different height. I do not wish this. How to make both images the same height? Take in mind that images should be responsive when screen size changes, thus I cannot simply add height property of both images, I guess.

我也无法更改原始图像的高度.如果可能,我需要使用CSS进行制作-然后使用jquery.

I also cannot change height of original images. I need to make it with css, if not possible - then with jquery.

推荐答案

您可以这样做

var height = $('.image-container').height(); var width = $('.image-container').width(); if (height > width) { $('.image-container img').css({ width: "auto", height: "100%" }); } else { $('.image-container img').css({ width: "100%", height: "auto" }); }

.horizontal-list-media { overflow: hidden; } .image-container { overflow: hidden; width: 50%; /*define your height here*/ height: 100px; } .image-container img { top: 50%; left: 50%; }

<script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-7 horizontal-list-media"> <div class="image-container"> <img src="moneyti.co/wp-content/uploads/2016/01/16-Zingis-RUS.png" style="" class="img-responsive"> </div> <div class="image-container"> <img src="moneyti.co/wp-content/uploads/2016/01/16-kazino-RUS-360x240.png" class="img-responsive"> </div> </div>

更多推荐

使两个响应图像具有相同的高度

本文发布于:2023-10-08 01:44:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   高度   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!