gpt4 book ai didi

css - 使具有不同宽高比的响应图像具有相同的高度

转载 作者:技术小花猫 更新时间:2023-10-29 11:25:01 25 4
gpt4 key购买 nike

我正在尝试找出一种方法,使响应式图像行具有相同的高度,而不管每个图像的纵横比或容器的宽度如何。实际图像文件的高度相同,但宽度不同。问题在于,当容器变小时,在某些宽度下舍入误差会导致图像在高度上有一个或两个不同的像素。这是一把 fiddle http://jsfiddle.net/chris_tsongas/vj2rfath/ ,调整结果 Pane 的大小,您会看到在某些宽度处图像的高度不再相同。

我正在为 CMS 中的内容设置样式,因此对标记的控制很少,并且只能使用 css 而不是 js。我尝试将图像高度设置为 100%,但这只会使图像成为其原始大小的 100%,而不是容器高度的 100%。这是上面链接的 fiddle 中的 html 和 css:

<div class="table">
<div class="row">
<div class="cell">
<img src="http://placehold.it/600x400" />
</div>
<div class="cell">
<img src="http://placehold.it/300x400" />
</div>
</div>
</div>

.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
}
img {
max-width: 100%;
}

最佳答案

为了让不同宽高比的图片达到相同的高度,可以通过NPM使用开源库bootstrap-spacer

npm install uniformimages

或者你可以访问github页面:

https://github.com/chigozieorunta/uniformimages

这是一个使用“unim”类的示例(为此您需要 jQuery):

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="uniformimages.css" rel="stylesheet" type="text/css"/>
<script src="uniformimages.js" type="text/javascript"></script>
</head>

<body>
<section>
<div class="container-fluid">
<div class="row">
<div class="col-sm-4">
<a href="product1.html">
<img src="image1.jpg" class="unim"/>
</a>
</div>
<div class="col-sm-4">
<a href="product2.html">
<img src="image2.jpg" class="unim"/>
</a>
</div>
<div class="col-sm-4">
<a href="product3.html">
<img src="image3.jpg" class="unim"/>
</a>
</div>
</div>
</div>
</section>
</body>

关于css - 使具有不同宽高比的响应图像具有相同的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com