html - 如何使用 HTML 和 CSS 使我的图像框响应
问题描述
我正在尝试构建一个简单的 HTML 和 CSS 卡,我有一个包含 box-image 和 body-card 的容器,并且该设计在桌面版本上完美运行,但是当我到达媒体查询我的图像的断点时它不是随着容器缩小,我不希望图像在更改视口宽度时通过容器。我正在寻找最佳实践:D
HTML
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="./images/drawers.jpg" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
CSS
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
媒体查询
@media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px;
height: 308px;
}
}
解决方案
只需将宽度设置为 100%?
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
@media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 100%;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://images.unsplash.com/photo-1531171596281-8b5d26917d8b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>