I have a flexbox grid of images and I'm trying to only add one border to each of the cells — the challenge is that this is a grid that will continue to have images added to it, so I'll never know exactly how many images/cells there will be. I'm trying to accomplish this via CSS alone. My code:
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-wrap: wrap;
padding: 5px;
}
.item {
padding: 5px;
}
<div class="container">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
</div>
The obvious challenge is to avoid the double borders something like this would inevitably create on the class .item
:
border: 1px solid #000;
And not knowing in advance how many images there will be in the grid means I can't use something like :nth-of-type(xn)
to remove or add borders. Thanks for any help you can provide here.
Slightly hacky, but using a negative left and top margin
of 1 pixel will cover the double border.
* {
box-sizing: border-box;
}
.container {
display: inline-flex;
flex-wrap: wrap;
padding: 5px;
}
.item {
border: 1px solid #000;
padding: 5px;
margin: -1px 0 0 -1px;
}
<div class="container">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
<img class="item" src="https://via.placeholder.com/150x100" alt="Example image">
</div>