Created
February 26, 2025 10:42
-
-
Save carefree-ladka/42d8173d313b3519fc5d7c2d912f4744 to your computer and use it in GitHub Desktop.
Masonry Grid Layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Masonry Grid Layout</title> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| background-color: #f8f9fa; | |
| margin: 0; | |
| padding: 20px; | |
| } | |
| /* Masonry Layout using column-count */ | |
| .container { | |
| column-count: 5; | |
| /* Change this for different column numbers */ | |
| column-gap: 15px; | |
| max-width: 1200px; | |
| margin: 0 auto; | |
| } | |
| .box { | |
| width: 100%; | |
| display: inline-block; | |
| margin-bottom: 15px; | |
| border-radius: 10px; | |
| overflow: hidden; | |
| box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); | |
| } | |
| .box img { | |
| width: 100%; | |
| display: block; | |
| border-radius: 10px; | |
| } | |
| /* Responsive */ | |
| @media (max-width: 1024px) { | |
| .container { | |
| column-count: 3; | |
| } | |
| } | |
| @media (max-width: 768px) { | |
| .container { | |
| column-count: 2; | |
| } | |
| } | |
| @media (max-width: 480px) { | |
| .container { | |
| column-count: 1; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container" id="imageContainer"></div> | |
| <script> | |
| const container = document.getElementById("imageContainer"); | |
| for (let i = 1; i <= 40; i++) { | |
| const imgId = Math.floor(Math.random() * 100) + 1; | |
| const height = 200 + (i % 3) * 100; // Varying heights | |
| const imgUrl = `https://picsum.photos/id/${imgId}/300/${height}`; | |
| const box = document.createElement("div"); | |
| box.className = "box"; | |
| const img = document.createElement("img"); | |
| img.src = imgUrl; | |
| img.alt = `Image ${i}`; | |
| img.onload = () => { | |
| box.appendChild(img); | |
| container.appendChild(box); | |
| }; | |
| img.onerror = () => console.warn(`Image ${i} failed to load.`); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment