I would like to make a card (image 1) which is fully responsive and can support two cards sitting to the left of it for a total of three cards on a website I am building (image 2). I’ve coded this up without responsiveness but I am struggling to get a responsive card. Any help is great, thank you!
JSFiddle: https://jsfiddle.net/brax/2o01k7ps/
.alignment2 {
display: flex;
justify-content: space-between;
column-gap: 20px;
padding-bottom: 6%;
}
.container {
padding: 2px 16px;
}
.card2 {
box-shadow: 0 5px 13px 0 #000;
border-radius: 15px;
background-color: #121212;
border-color: #121212;
height: 188px;
}
.card2:hover {
box-shadow: 0 8px 16px 0 000;
}
.card2 img {
width: 50%;
float: left;
border-radius: 15px 0px 0px 15px;
margin-right: 20px;
display: inline;
}
.card2 h3 {
display: inline;
}
.card2 h4 {
display: inline;
font-size: 24px;
color: white;
}
.card2 p {
display: inline;
font-size: 18px;
color: white;
padding-top: 20px;
}
.card2 .link-a {
display: inline;
margin-top: 35px;
font-size: 12px;
font-weight: 700;
text-transform: uppercase;
text-decoration: none;
}
<div class="alignment2">
<div class="card2">
<img src="https://dummyimage.com/300x300" alt="Avatar">
<div class="container">
<h4>Lorem Ipsum</h4>
<br>
<br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor</p>
<br>
<br>
<p class="link-a" style="font-size: 15px;"><a href="link">CTA Link</a></p>
</div>
</div>
<div class="card2">
<img src="https://dummyimage.com/300x300" alt="Avatar">
<div class="container">
<h4>Lorem Ipsum</h4>
<br>
<br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor</p>
<br>
<br>
<p class="link-a" style="font-size: 15px;"><a href="link">CTA Link</a></p>
</div>
</div>
</div>
Image 1
Image 2
2
Answers
To make it responsive, first replace
width: 50%
onimg
withheight: 100%;
so that the image is stretched vertically on the card.Then add
flex-wrap: wrap;
to the.alignment2
and replacecolumn-gap
withgap
so that there is a gap between cards when they are displayed in rows.Also, add
flex: 1 1 calc(33.333% - 20px);
to.card2
so that they display side by side in a row with a little gap in between.Lastly, using "media queries", change the
flex
for smaller screens so that fewer cards are next to each other as the screen gets smaller.Here is the updated page (I added a third card):
there was an error in
.card2:hover
too, change000
to#000
Since you already use flex, you may keep using it for the children layouts too.
Here is a possible example you can play with.
I believe you already know enough about flex to find your own way . Else here is a usefull link: https://css-tricks.com/snippets/css/a-guide-to-flexbox/