我正在处理一个使用CSS Grid和Flexbox组合的布局。网格正在设置结构(左边的英雄图像和右边的4行内容)。
在每个内容行中,我都有一个弹性框,用于处理缩略图图像的放置位置,在右边,左边三行文本-日期,标题和作者。
但是,当任何一行文本太长时,它们就会开始放大缩略图,如此处的第2行:
很显然,我希望缩略图保持固定不变,而文本在触摸之前就自动换行。我已经尝试了很多潜在策略-使用溢出,浮点数而不是flexbox,但是找不到窍门。有什么想法吗?
以下是代码,为简洁起见,省略了第3行和第4行,以及Codepen链接:
https://codepen.io/anon/pen/dBWyzb
.wrapper {
width: 1200px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(2, minmax(100px, auto));
grid-template-areas:
"hero row1"
"hero row2"
"hero row3"
"hero row4";
grid-gap: 10px;
}
.hero {grid-area: hero;}
.row1 {grid-area: row1;}
.row2 {grid-area: row2;}
.flex {
display: flex;
background-color: lightblue;
align-items: center;
}
.thumbnail {
width: 100px;
height: 100px;
margin-left: auto;
}
img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="wrapper">
<div class="grid">
<div class="hero">
<img src="https://dummyimage.com/600x400/000/fff"/>
</div>
<!-- Row 1 -->
<div class="row1">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>Title</h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div> <!-- end flex -->
</div> <!-- end row -->
<!-- Row 2 -->
<div class="row2">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div> <!-- end flex -->
</div> <!-- end row -->
</div>
</div>
答案 0 :(得分:1)
将flex: 1;
添加到.text
div中,以便flexbox可以管理它可以占用的空间:
.wrapper {
width: 1200px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(2, minmax(100px, auto));
grid-template-areas: "hero row1" "hero row2" "hero row3" "hero row4";
grid-gap: 10px;
}
.hero {
grid-area: hero;
}
.row1 {
grid-area: row1;
}
.row2 {
grid-area: row2;
}
.flex {
display: flex;
background-color: lightblue;
align-items: center;
}
.text {
flex: 1;
}
.thumbnail {
width: 100px;
height: 100px;
margin-left: auto;
}
img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="wrapper">
<div class="grid">
<div class="hero">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<!-- Row 1 -->
<div class="row1">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>Title</h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div>
<!-- end flex -->
</div>
<!-- end row -->
<!-- Row 2 -->
<div class="row2">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div>
<!-- end flex -->
</div>
<!-- end row -->
</div>
</div>
答案 1 :(得分:0)
弹性项目的默认设置为flex-shrink: 1
。这意味着可伸缩物品可以收缩以便保留在容器内。
对于您而言,只需禁用flex-shrink
。将此添加到您的代码中:
.thumbnail { flex-shrink: 0 }