我有一个简单的div标记:
<div class="gallery__card">
<img class="gallery__card__img" src="1.jpg">
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
</div>但是我希望gallery__card和gallery__card__img一样宽,这样图像下面的文本就不会占据整个页面的宽度。

我想避免设置固定宽度,因为这应该是响应式的。
有没有办法在没有JS和固定宽度的情况下实现这一点?
jsfiddle:http://jsfiddle.net/6h19oa1q/
发布于 2015-11-11 00:08:23
发布于 2015-11-11 00:18:54
您可以使用JS (我的解决方案是使用jQuery),在每次迭代中,您都会使用选择器img查看子元素,然后设置父元素的宽度
$('.gallery__card').each(function() {
var newWidth = $(this).children('img').width();
$(this).width(newWidth);
});发布于 2015-11-11 00:19:02
试试这个:jsFiddle
将此样式添加到您拥有的所有图像
img {
width: 100%;
max-width: 100%;
height: auto;
display: block;
}并从.gallery__card__img中删除所有的高度限制
https://stackoverflow.com/questions/33634234
复制相似问题