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;
  }
}

that's the problem enter image description here

Runin

You're almost there, but you need to make some small changes:

  • You sectionreally should have an explicit displaystatement
  • You can give sectionone display: block, but it makes more sense just to move display: flex; justify-content: center; align-items: center;from bottom bodyto bottomsection
  • There are several places where you've used elements widthwith 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:

Related


How to make an image inside a box responsive css

Coderhhz I'm having trouble modifying my site template. I have a total of four images and I want them to be displayed in the first row set, and the other three in the second row. By default, the template code has an arrangement to place three images side by si

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

How to make a responsive box using HTML code?

Eiffel Bear On my blog post I made boxes with pictures and text. They look broken on mobile, but work fine on PC . So I try to use html code to make a box that will display fine in both mobile and PC environment. But it seems "using px or percentage doesn't he

Make my box responsive in CSS

Andrew Yed I have 8 boxes on my html file, everything looks fine when viewed on desktop, visited on mobile, and the columns are as far from the actual values as they should be, they should be in the middle, not on the right, I How can I do this to make it resp

Make my box responsive in CSS

Andrew Yed I have 8 boxes on my html file, everything looks fine when viewed on desktop, visited on mobile, and the columns are as far from the actual values as they should be, they should be in the middle, not on the right, I How can I do this to make it resp

How do I make my HTML, CSS and JavaScript popups responsive?

Zanway I've been trying to make my html, css and javascript popups responsive. I looked at similar posts and videos but couldn't find anything that worked for my code. Of course I know I can make it responsive! Any help would be greatly appreciated. Thanks in

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make my circles responsive using only CSS?

Paula I have a rectangle divthat contains a circle with an image in it. My design is responsive and I don't know how to make my circles responsive. I've read other articles mentioning the same thing vw, vhbut it doesn't work for me. Both have the same widthsum

How can I make this box in CSS (please see my image)?

Munira In my divbox I have one bodyand headerpart. I want to give this headerpart a separate border which must be inside the div box and not have any padding. should indeed look like my given image: Here is what I have tried: HTML: <div id="story"> <p>

How can I make this box in CSS (please see my image)?

Munira In my divbox I have one bodyand headerpart. I want to give this headerpart a separate border which must be inside the div box and not have any padding. should indeed look like my given image: Here is what I have tried: HTML: <div id="story"> <p>

How to make CSS background image responsive?

User 3420034 Ok so I came across this solution to make background images responsive: Responsive CSS Background Images I apologize for asking the question again, but none of their solutions actually worked for me. HTML: <div id="fourohfour_home"></div> CSS: #f

How to make CSS background image responsive?

User 3420034 ok so i came across this solution to make the background image responsive: Responsive CSS Background Image I apologize for asking the question again, but none of their solutions actually worked for me. HTML: <div id="fourohfour_home"></div> CSS:

How to make CSS background image responsive?

User 3420034 ok so i came across this solution to make the background image responsive: Responsive CSS Background Image I apologize for asking the question again, but none of their solutions actually worked for me. HTML: <div id="fourohfour_home"></div> CSS:

Make tables responsive on mobile using HTML/CSS

AC_DCEDH I have this form which is part of a larger email. When the user's device is a mobile phone or a small screen, I want the table to go from 2 columns/2 rows to 1 column and 4 rows. Want to use minimal code. <table style="width:100%;table-layout:fixed;bo

Make tables responsive on mobile using HTML/CSS

AC_DCEDH I have this form which is part of a larger email. When the user's device is a mobile phone or a small screen, I want the table to go from 2 columns/2 rows to 1 column and 4 rows. Want to use minimal code. <table style="width:100%;table-layout:fixed;bo

Make tables responsive on mobile using HTML/CSS

AC_DCEDH I have this form which is part of a larger email. When the user's device is a mobile phone or a small screen, I want the table to go from 2 columns/2 rows to 1 column and 4 rows. Want to use minimal code. <table style="width:100%;table-layout:fixed;bo