制作适合所有内容的卡片
Make a card that fits all content
我想在我的新闻网站(学校项目)的移动版上制作 8 张卡片。我希望所有卡片的比例都相同,但是,我在电脑上保存的卡片图像在尺寸上有所不同(有些更高)。
有没有办法让图片、标题和文本都适合卡片,而无需在将每张图片添加到项目之前调整其大小?
I wish for all cards to look like this
How the cards look like when the image size differ
代码:
img {
max-width: 100%;
max-height: 100%;
display: block;
}
.cards {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 5px;
}
.card{
display: flex;
flex-direction: column;
padding: 5px;
margin: 20px;
margin-bottom: 5px;
height: 280px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
只需为图片指定一个您希望图片达到的最高高度值即可。您必须将宽度设置为自动以避免拉伸图片。
img {
height: 300px; /* play around with this number until you get the height you need */
width: auto;
max-width: 100%;
}
通常我把图片作为背景,然后居中放置。您也可以将所有背景属性放在一起,但是如果您使用 php,我建议将 style 标签放入 background-image 和 class 属性 其余所有。
css的解释:
溢出:隐藏; -> 这会隐藏图像的额外边界
background-size: cover; -> 这使得图像 stretch/resize 尽可能多地与容器大小相关(事实上我使用了一个图像600x800) Syntax
background-position: center center; -> 如果你 trim 图像通常 trim 它在各个方向均匀分布更安全,但这取决于你。 Syntax
奖励:为了确保避免小图像像模式一样重复,您还可以添加:
background-repeat: no-repeat;
如果你设置 background-size: cover 你不需要这个。
/* where the magic happens */
.img-as-bg {
height: 300px;
overflow: hidden;
background-size: cover;
background-position: center center;
}
/* extra style copied from your source */
.cards {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 5px;
}
.card{
display: flex;
flex-direction: column;
padding: 5px;
margin: 20px;
margin-bottom: 5px;
height: 280px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
<div class="cards">
<div class="card" style="width: 18rem;">
<div class="img-as-bg" style="background-image: url('https://via.placeholder.com/600x800/0055ff');"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#">Read more</a>
</div>
</div>
<div class="card" style="width: 18rem;">
<div class="img-as-bg" style="background-image: url('https://via.placeholder.com/500x900/00ffff');"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#">Read more</a>
</div>
</div>
</div>
我想在我的新闻网站(学校项目)的移动版上制作 8 张卡片。我希望所有卡片的比例都相同,但是,我在电脑上保存的卡片图像在尺寸上有所不同(有些更高)。
有没有办法让图片、标题和文本都适合卡片,而无需在将每张图片添加到项目之前调整其大小?
I wish for all cards to look like this
How the cards look like when the image size differ
代码:
img {
max-width: 100%;
max-height: 100%;
display: block;
}
.cards {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 5px;
}
.card{
display: flex;
flex-direction: column;
padding: 5px;
margin: 20px;
margin-bottom: 5px;
height: 280px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
只需为图片指定一个您希望图片达到的最高高度值即可。您必须将宽度设置为自动以避免拉伸图片。
img {
height: 300px; /* play around with this number until you get the height you need */
width: auto;
max-width: 100%;
}
通常我把图片作为背景,然后居中放置。您也可以将所有背景属性放在一起,但是如果您使用 php,我建议将 style 标签放入 background-image 和 class 属性 其余所有。
css的解释:
溢出:隐藏; -> 这会隐藏图像的额外边界
background-size: cover; -> 这使得图像 stretch/resize 尽可能多地与容器大小相关(事实上我使用了一个图像600x800) Syntax
background-position: center center; -> 如果你 trim 图像通常 trim 它在各个方向均匀分布更安全,但这取决于你。 Syntax
奖励:为了确保避免小图像像模式一样重复,您还可以添加: background-repeat: no-repeat; 如果你设置 background-size: cover 你不需要这个。
/* where the magic happens */
.img-as-bg {
height: 300px;
overflow: hidden;
background-size: cover;
background-position: center center;
}
/* extra style copied from your source */
.cards {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 5px;
}
.card{
display: flex;
flex-direction: column;
padding: 5px;
margin: 20px;
margin-bottom: 5px;
height: 280px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
<div class="cards">
<div class="card" style="width: 18rem;">
<div class="img-as-bg" style="background-image: url('https://via.placeholder.com/600x800/0055ff');"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#">Read more</a>
</div>
</div>
<div class="card" style="width: 18rem;">
<div class="img-as-bg" style="background-image: url('https://via.placeholder.com/500x900/00ffff');"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#">Read more</a>
</div>
</div>
</div>