【发布时间】:2016-02-26 04:10:39
【问题描述】:
如何调整图像大小以适应 display:flex 子 div?请参考这个plunk:http://plnkr.co/edit/zqdpqnV7QkKrx7lm1Tsi?p=preview
我要缩放的图像是这样的:http://images.freeimages.com/images/previews/8fe/sky-above-the-black-mountains-1402912.jpg
我在 Stackoverflow 上读到,这可以使用 max-height 和 max-width 属性来完成,但我无法达到预期的结果。
我也读过这篇文章,但没有帮助:http://www.w3schools.com/css/css_rwd_images.asp
CSS:
.card {
position: relative;
z-index: 2;
box-shadow: 0 0.1rem 0.2rem #aaa;
background: #fff;
border-radius: 0.2rem;
margin: 0.5rem 0 1rem 0;
height: 10rem;
display: flex;
flex-direction: column;
}
.card.sm {
height: 10rem;
}
.card.sm .card-action {
padding: 0.5rem;
}
.card.md {
height: 20rem;
}
.card.md .card-action {
padding: 1rem;
}
.card.lg {
height: 30rem;
}
.card.lg .card-action {
padding: 1.5rem;
}
.card .card-content {
padding: 1rem;
flex: 8;
overflow:auto;
overflow-y: scroll;
}
.card .card-content .card-title {
font-weight: bold;
font-size: 1.1rem;
}
.card .card-action {
flex: 2;
max-height: 5%;
padding: 1rem;
border-top: 0.1rem solid rgba(160, 160, 160, 0.2);
text-transform: uppercase;
}
.card .card-image {
position: relative;
flex: 24;
overflow:hidden;
}
.card .card-image img {
border-radius: 2px 2px 0 0;
position: relative;
left: 0;
right: 0;
top: 0;
bottom: 0;
max-width: 100%;
max-height: 100%;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div style="width:30rem">
<div class="card md">
<div class="card-image">
<img src="http://images.freeimages.com/images/previews/8fe/sky-above-the-black-mountains-1402912.jpg"/>
</div>
<div class="card-content">
<span class="card-title">Title</span>
<p>Some content</p>
</div>
<div class="card-action">
<a href="#">ACTION 1</a>
<a href="#">ACTION 2</a>
</div>
<div class="card-action">
<a href="#">ACTION 3</a>
<a href="#">ACTION 4</a>
</div>
</div>
</div>
<div style="width:30rem">
<div class="card md">
<div class="card-content">
<span class="card-title">Title</span>
<p>Some content</p>
</div>
<div class="card-action">
<a href="#">ACTION 1</a>
<a href="#">ACTION 2</a>
</div>
<div class="card-action">
<a href="#">ACTION 3</a>
<a href="#">ACTION 4</a>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
从视觉上看,您到底想完成什么?
-
我在卡片图像 div 中显示的图像应根据 div 的大小进行缩放。如果您现在查看 plunk,图像会被裁剪掉。我希望整个图像以卡片图像 div 的大小显示
-
嗨,盖比。我没有在您上面粘贴的 plunk 中看到完整的图像。图片由于某种原因被裁剪
-
在下面查看我更新的 plunk,您应该会看到预期的完整图像。