Skip to content

Instantly share code, notes, and snippets.

@jenswittmann
Created October 15, 2024 12:49
Show Gist options
  • Save jenswittmann/91111bc42078ce6e9557675311b4b9dd to your computer and use it in GitHub Desktop.
Save jenswittmann/91111bc42078ce6e9557675311b4b9dd to your computer and use it in GitHub Desktop.
CurlyRandomGrid
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>CurlyRandomGrid</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<style>
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
width: 100%;
}
.grid__image {
grid-column-start: 1;
width: 100%;
}
</style>
</head>
<body>
<div class="grid">
<img src="https://picsum.photos/500/500?id=1" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=2" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=3" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=4" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=5" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=6" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=7" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=8" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=9" alt="" class="grid__image" />
<img src="https://picsum.photos/500/500?id=10" alt="" class="grid__image" />
</div>
<script>
let getRandomColumn = (min_start, max_end) => {
return Math.floor(Math.random() * (max_end - min_start + 1) + min_start);
};
let placeImages = () => {
let column_span = 8;
if (window.innerWidth > 1000) {
column_span = 3;
}
document.querySelectorAll(".grid__image").forEach((item) => {
let column_start = getRandomColumn(1, 12 - column_span),
column_end = getRandomColumn(column_start + column_span, 12);
item.style = `
grid-column-start: ${column_start};
grid-column-end: ${column_end};
`;
});
};
placeImages();
window.addEventListener("resize", () => placeImages());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment