我已经寻找了一段时间,但找不到合适的答案。
我有一些卡片,里面有两个图像。
卡中的图像的纵横比略有不同-留下了一些多余的空白空间和不均匀的水平间距。本质上,我该如何摆脱呢? :)
这里有一个codepen,可以帮助您更好地进行解释。
解决这个问题的最好方法是为所有内容和某种缩放方法声明高度吗?
main {
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: auto;
grid-gap: 0.5em;
}
.card {
display: grid;
place-items: center;
box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 5px;
}
.card-image-profile {
display: block;
width: 100%;
height: auto;
}
.card-image-department {
padding: 0.25rem;
max-width: 70%
}
@media only screen and (min-width: 400px) {
main {
grid-template-columns: 1fr 1fr;
}
}
@media only screen and (min-width: 500px) {
main {
grid-template-columns: 1fr 1fr 1fr;
}
}
@media only screen and (min-width: 700px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
.card-image-department {
max-width: 100%
}
}
@media only screen and (min-width: 900px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
}
}
@media only screen and (min-width: 1100px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
}
}
<main>
<div class="card">
<img src="http://via.placeholder.com/350x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/359x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/345x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/357x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x62" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/350x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
</main>
两张卡之间的间距不一致-由于图像尺寸略有不同。
答案 0 :(得分:0)
此代码可能会解决您的问题,但如果没有让我知道。
main {
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: auto;
grid-gap: 0.5em;
}
.card {
display: grid;
place-items: center;
box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 5px;
}
.card-image-profile {
display: block;
width: 100%;
height: 109px;
}
.card-image-department {
padding: 0.25rem;
//max-width: 70%;
max-width: 100%;
display: block;
width: 100%;
}
@media only screen and (min-width: 400px) {
main {
grid-template-columns: 1fr 1fr;
}
}
@media only screen and (min-width: 500px) {
main {
grid-template-columns: 1fr 1fr 1fr;
}
}
@media only screen and (min-width: 700px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
.card-image-department {
//max-width: 100%;
}
}
@media only screen and (min-width: 900px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
}
}
@media only screen and (min-width: 1100px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
}
}
<main>
<div class="card">
<img src="http://via.placeholder.com/350x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/359x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/345x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/357x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x62" class="card-image-department">
</div>
<div class="card">
<img src="http://via.placeholder.com/350x150" class="card-image-profile">
<img src="http://via.placeholder.com/200x60" class="card-image-department">
</div>
</main>
我刚刚更改了3个课程: 第一个:
.card-image-department {
padding: 0.25rem;
//max-width: 70%;
max-width: 100%;
display: block;
width: 100%;
}
第二个是:
.card-image-profile{
display: block;
width: 100%;
height: 109px;
}
最后一个只是注释@Media类中的代码:
@media only screen and (min-width: 700px) {
main {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
.card-image-department {
//max-width: 100%;
}
}
答案 1 :(得分:0)
您只需要将卡片图像部门的宽度和高度设置为100%。您已将最大宽度设置为70%,应将其删除。
.card-image-department {
padding: 0.25rem;
width: 100%;
height: 100%;
}
答案 2 :(得分:0)
如果要删除图像之间的间隔,则应删除.card-image-department类中的padding属性。对于水平空间,它应该已经与以下位置项居中:在卡类中,我看不出会有什么不均匀。