I was looking for a pure CSS way to collapse the borders of a grid, but since I could find none, I made a little prototype.
HTML
<div class="container">
<div id="grid" class="grid">
<div class="element">1</div>
<div class="element">2</div>
<div class="element">3</div>
<div class="element">4</div>
<div class="element">5</div>
<div class="element">6</div>
<div class="element">7</div>
<div class="element">8</div>
<div class="element">9</div>
<div class="element">10</div>
<div class="element">11</div>
</div>
</div>
CSS
.container {
max-width: 720px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.element {
text-align: center;
padding: 20px;
background: #f4f4f4;
border-bottom: 1px solid black;
border-right: 1px solid black;
}
.border-top {
border-top: 1px solid black;
}
.border-left {
border-left: 1px solid black;
}
.border-top-left-rounded {
border-top-left-radius: 8px;
}
.border-top-right-rounded {
border-top-right-radius: 8px;
}
.border-bottom-left-rounded {
border-bottom-left-radius: 8px;
}
.border-bottom-right-rounded {
border-bottom-right-radius: 8px;
}
JS
function dynamicRoundedCorners() {
// get
const grid = document.getElementById("grid");
const elements = grid.children;
const gridStyle = getComputedStyle(grid);
// reset
for (element of elements) {
element.classList = "";
element.classList.add("element");
}
// analyze
const elementsPerRowCount = gridStyle.gridTemplateColumns
.split(" ")
.filter((element) => Number(element.replace("px", ""))).length;
const rowCount = Math.ceil(elements.length / elementsPerRowCount);
const rowsFirstAndLastElements = [];
let firstAndLastElementIndex = 0;
for (let i = 1; i <= rowCount; i++) {
const rowFirstAndLastElements = [firstAndLastElementIndex];
if (i === rowCount && rowCount > 1) {
rowFirstAndLastElements.push(
firstAndLastElementIndex + (elements.length % elementsPerRowCount) - 1
);
} else {
rowFirstAndLastElements.push(
firstAndLastElementIndex + elementsPerRowCount - 1
);
}
rowsFirstAndLastElements.push(rowFirstAndLastElements);
firstAndLastElementIndex += elementsPerRowCount;
}
// apply
// -> add border-top on the first row
for (let i = 0; i <= rowsFirstAndLastElements[0][1]; i++) {
elements[i].classList.add("border-top");
}
// -> add border-left on every first element of a row
for (let i = 0; i < rowCount; i++) {
elements[rowsFirstAndLastElements[i][0]].classList.add("border-left");
}
// -> add top-left rounded corner on first element of first row
elements[0].classList.add("border-top-left-rounded");
// -> add top-right rounder corner on last element of first row
elements[rowsFirstAndLastElements[0][1]].classList.add(
"border-top-right-rounded"
);
// -> add bottom-left rounded corner on first element of last row
elements[rowsFirstAndLastElements[rowCount - 1][0]].classList.add(
"border-bottom-left-rounded"
);
// -> add bottom-right rounder corner on last element of last row
elements[elements.length - 1].classList.add("border-bottom-right-rounded");
// -> if elements.length % elementsPerRowCount != 0, add bottom-right rounder corner on last element of second to last row
if (elements.length % elementsPerRowCount !== 0) {
elements[
rowsFirstAndLastElements[rowsFirstAndLastElements.length - 2][1]
].classList.add("border-bottom-right-rounded");
}
}
// call
dynamicRoundedCorners();
window.addEventListener("resize", dynamicRoundedCorners);
Here is the link: https://codepen.io/RilDev/pen/gOmjNrQ
border-collapse: collapse
. – Telegraph