To crop the top side of an element using the CSS property clip-path
:
:root {
--offset: 1.5em;
}
.diagonal-grid > div:not(:first-child) {
margin-top: calc(var(--offset) * -1);
}
.diagonal-grid > div:nth-child(even) {
clip-path: polygon(0 var(--offset), 100% 0, 100% 100%, 0 100%);
}
.diagonal-grid > div:not(:first-child):nth-child(odd) {
clip-path: polygon(0 0, 100% var(--offset), 100% 100%, 0 100%);
}
<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<!-- Body -->
<section class="diagonal-grid">
<div class="bg-primary-subtle text-emphasis-primary p-5">
Lorem ipsum.
</div>
<div class="bg-danger-subtle text-emphasis-primary p-5">
Lorem ipsum.
</div>
<div class="bg-info text-emphasis-primary p-5">
Lorem ipsum.
</div>
<div class="bg-body-tertiary text-emphasis-primary p-5">
Lorem ipsum.
</div>
<div class="bg-success text-emphasis-primary p-5">
Lorem ipsum.
</div>
</section>
If you prefer SCSS, you can use the following snippet:
.diagonal-grid {
$offset: 1.5em;
> div {
&:nth-child(even) {
clip-path: polygon(0 $offset, 100% 0, 100% 100%, 0 100%);
}
&:not(:first-child) {
margin-top: calc(#{$offset} * -1);
&:nth-child(odd) {
clip-path: polygon(0 0, 100% var(--offset), 100% 100%, 0 100%);
}
}
}
}
You can dynamically assign classes to choose which corner to crop. Note that cropping the top side of an element may not work as expected, so it's best to limit it to the top of the first element.
:root {
--offset: 1.5em;
}
.diagonal-grid > .cut-top-left {
clip-path: polygon(0 var(--offset), 100% 0, 100% 100%, 0 100%);
}
.diagonal-grid > .cut-top-right {
clip-path: polygon(0 0, 100% var(--offset), 100% 100%, 0 100%);
}
.diagonal-grid > .cut-bottom-left {
margin-bottom: calc(var(--offset) * -1);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 calc(100% - var(--offset)));
}
.diagonal-grid > .cut-bottom-right {
margin-bottom: calc(var(--offset) * -1);
clip-path: polygon(0 0, 100% 0, 100% calc(100% - var(--offset)), 0 100%);
}
<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>