How to make my image box responsive using HTML and CSS
real john doe
I'm trying to build a simple HTML and CSS card, I have a container with box-image and body-card, and the design works fine on the desktop version, but when I hit a media query breakpoint, my image doesn't Not shrinking with the container, when I change the width of the viewport, I don't want the image to go through the container. I'm looking for best practices :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 inquiries
@media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px;
height: 308px;
}
}
Runin
You're almost there, but you need to make some small changes:
- You
section
really should have an explicitdisplay
statement - You can give
section
onedisplay: block
, but it makes more sense just to movedisplay: flex; justify-content: center; align-items: center;
from bottombody
to bottomsection
- There are several places where you've used elements
width
with flex-parent - but the whole idea is that these flex-children elements should have flexible dimensions, so instead useflex: [expand tendency] [shrink tendency] [start-width]
Working example:
Note use the full page link to see the full effect
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
min-height: 100vh;
}
section.card {
display: flex;
justify-content: center;
align-items: center;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
display: block;
flex: 0 1 540px;
max-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 {
flex: 0 1 602px;
max-height: 308px;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://picsum.photos/540/442" 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>
Further reading: