I'm creating an adaptable row of images that will break on to another row if needed when there are too many images for the size. At the moment there's 4 in a row on desktop and 2 in a row on mobile.
All of the images are set to 25% or 50% in width. I want to have them spaced out slightly and evenly.
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
@media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
<div class="images-row">
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
</div>
This works fine and all of the images are touching. I feel like some space would look better visually. Is the best approach to pad the width of the main container? I've tried making the images smaller, like this;
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
@media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
<div class="images-row">
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
</div>
Which looks fine on desktop but the bottom of the images still touches the top of the ones below them. Doing something like this:
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
@media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
.image-padding {
padding: 5px
}
.image {
width: 100%
}
<div class="images-row">
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
</div>
Works fine for padding also but runs into the issue that they don't then stack on mobile. I feel like I'm close but I'm not sure how to get this to work.
EDIT
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
}
@media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
.image-padding {
padding: 5px
}
.image {
width: 100%
}
<div class="images-row">
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
</div>
<style>
</style>