gemini-2.5-flash-lite-preview-06-17-thinking· Google LMArena Human Preference 140K
write on file html\css\js modern code for smart grid\tiles with small and big images and…
- User
write on file html\css\js modern code for smart grid\tiles with small and big images and tiny gap, also make modal for full view. example url https://ik.imagekit.io/ll20/tr:w-800,h-400/R/VIP2000/0001.JPG (all file names range: 0001-0086).
- Assistant
Okay, here is the modern HTML, CSS, and JavaScript code to create a responsive image gallery grid with a modal view for full-size images.
File Structure:
/your-project-folder index.html style.css script.js
1.
index.html:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Smart Image Grid</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Image Gallery</h1> <div class="gallery-grid" id="gallery-grid"> <!-- Images will be loaded here by JavaScript --> <p>Loading images...</p> </div> <!-- The Modal --> <div class="modal" id="image-modal"> <span class="modal-close" id="modal-close">×</span> <img class="modal-content" id="modal-image-content" src="" alt="Full Size View"> <div class="modal-caption"></div> </div> <script src="script.js"></script> </body> </html>
2.
style.css:/* Basic Reset & Body Styling */ * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f4f4; color: #333; line-height: 1.6; padding: 20px; } h1 { text-align: center; margin-bottom: 30px; color: #2c3e50; } /* Grid Container */ .gallery-grid { display: grid; /* Responsive Columns: */ /* Creates columns that are at least 200px wide, */ /* and expands them to fill available space. */ /* Adjust minmax(min-width, max-width) as needed */ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; /* Tiny gap between items */ padding: 10px; max-width: 1200px; /* Optional: constrain max width */ margin: 0 auto; /* Center the grid */ } /* Grid Item */ .grid-item { background-color: #fff; border-radius: 8px; overflow: hidden; /* Crucial for containing the image */ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease, box-shadow 0.3s ease; cursor: pointer; /* Indicate clickable */ } .grid-item:hover { transform: translateY(-5px) scale(1.02); /* Subtle lift and scale */ box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2); } .grid-item img { display: block; /* Remove extra space below image */ width: 100%; height: 250px; /* Fixed height for consistent grid appearance */ object-fit: cover; /* Cover the area, cropping if needed */ transition: filter 0.3s ease; } .grid-item:hover img { filter: brightness(0.9); /* Slightly dim image on hover */ } /* Modal Styles */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1000; /* Sit on top */ left: 0; top: 0; width: 100%; height: 100%; overflow: auto; /* Enable scroll if needed */ background-color: rgba(0, 0, 0, 0.85); /* Dim background */ /* Use flexbox for centering */ display: flex; justify-content: center; align-items: center; } /* Style for when modal is active */ .modal.active { display: flex; /* Show the modal */ } .modal-content { position: relative; margin: auto; display: block; max-width: 90%; max-height: 85%; animation-name: zoom; animation-duration: 0.4s; border-radius: 5px; /* Slightly rounded corners for image */ } /* Modal Close Button */ .modal-close { position: absolute; top: 25px; right: 35px; color: #f1f1f1; font-size: 45px; font-weight: bold; transition: 0.3s; cursor: pointer; background: none; border: none; line-height: 1; z-index: 1001; /* Ensure it's above the image */ } .modal-close:hover, .modal-close:focus { color: #bbb; text-decoration: none; } /* Caption text */ .modal-caption { color: #ccc; text-align: center; padding: 15px 0; font-size: 1.1em; } /* Animation */ @keyframes zoom { from {transform: scale(0.8)} to {transform: scale(1)} } /* Optional: Media Query for smaller screens if needed */ @media (max-width: 480px) { .gallery-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 5px; } .modal-close { font-size: 35px; top: 15px; right: 25px; } .modal-content { max-width: 95%; max-height: 80%; } }
3.
script.js:document.addEventListener('DOMContentLoaded', () => { const galleryGrid = document.getElementById('gallery-grid'); const modal = document.getElementById('image-modal'); const modalImage = document.getElementById('modal-image-content'); const modalClose = document.getElementById('modal-close'); // --- Configuration --- const imageBaseUrl = "https://ik.imagekit.io/ll20/R/VIP2000/"; const thumbnailUrlParams = "tr:w-800,h-400,f-auto"; // Params for grid thumbnail const fullUrlParams = "tr:w-1200,f-auto"; // Params for modal view (adjust width as needed) const startNumber = 1; const endNumber = 86; const filePadding = 4; // Number of digits for padding (e.g., 4 -> 0001) // --- End Configuration --- // Function to generate image URLs and populate the grid function populateGallery() { let gridHTML = ''; // Use a string builder for efficiency for (let i = startNumber; i <= endNumber; i++) { // Format the number with leading zeros const formattedNumber = String(i).padStart(filePadding, '0'); const imageUrlPart = `${formattedNumber}.JPG`; const thumbnailUrl = `${imageBaseUrl}${thumbnailUrlParams}/${imageUrlPart}`; const fullUrl = `${imageBaseUrl}${fullUrlParams}/${imageUrlPart}`; // Create the grid item HTML // Use data-* attributes to store the full URL gridHTML += ` <div class="grid-item" data-full-src="${fullUrl}"> <img src="${thumbnailUrl}" alt="Gallery Image ${i}"> </div> `; } // Clear the "Loading..." message and insert the generated HTML galleryGrid.innerHTML = gridHTML; } // Function to open the modal function openModal(imageUrl) { modalImage.src = imageUrl; modal.classList.add('active'); // Use class to show/hide document.body.style.overflow = 'hidden'; // Prevent background scrolling } // Function to close the modal function closeModal() { modal.classList.remove('active'); document.body.style.overflow = ''; // Restore background scrolling modalImage.src = ""; // Clear src to potentially release memory } // Event Listener for Grid Clicks (using event delegation) galleryGrid.addEventListener('click', (event) => { // Find the closest ancestor .grid-item that was clicked const gridItem = event.target.closest('.grid-item'); if (gridItem) { const imageUrl = gridItem.getAttribute('data-full-src'); if (imageUrl) { openModal(imageUrl); } } }); // Event Listener for Close Button modalClose.addEventListener('click', closeModal); // Event Listener for clicking outside the image in the modal modal.addEventListener('click', (event) => { // Check if the click was directly on the modal background (not the image or close button) if (event.target === modal) { closeModal(); } }); // --- Initial Setup --- populateGallery(); // Load the gallery when the script runs });
Explanation:
-
HTML (
index.html):- Sets up the basic structure.
- Links the
style.cssfile. - Includes a container
divwith theid="gallery-grid"where JavaScript will insert the image items. - Defines the modal structure (
<div class="modal">) which is initially hidden (display: none;in CSS). It contains a close button (.modal-close) and the image display area (.modal-content). - Links the
script.jsfile at the end of thebodyfor better performance.
-
CSS (
style.css):- Grid Layout: Uses
display: gridandgrid-template-columns: repeat(auto-fit, minmax(200px, 1fr)). This is the core of the responsive grid. It automatically creates as many columns as can fit, ensuring each column is at least200pxwide but can grow to fill space (1fr). - Gap:
gap: 10px;creates the small spacing between grid cells. - Grid Items: Styles the individual items, adds a subtle hover effect (
transform,box-shadow), and setscursor: pointer. - Images in Grid: Sets
width: 100%, a fixedheight: 250px, andobject-fit: cover;.object-fit: coverensures the images fill the grid item's dimensions proportionally, cropping the parts that don't fit, maintaining aspect ratio. - Modal: Uses
position: fixedto overlay the page,background-color: rgba(0,0,0,0.85)for the dark overlay. Flexbox (display: flex,justify-content: center,align-items: center) is used to perfectly center the modal content. - Modal Active State: The
.modal.activeclass (toggled by JS) changes the display fromnonetoflex, making the modal visible. - Modal Image:
max-widthandmax-heightensure the image fits within the viewport boundaries. An animation (zoom) provides a nice transition effect. - Close Button: Positioned absolutely in the top-right corner.
- Grid Layout: Uses
-
JavaScript (
script.js):- Configuration: Sets up variables for the base URL, transformation parameters, image number range, and number padding. This makes it easy to modify later.
populateGallery():- Loops from
startNumbertoendNumber. - Uses
String(i).padStart(filePadding, '0')to ensure numbers like1become0001. - Constructs the thumbnail URL and the full URL using the base path and parameters.
- Creates an HTML string for each grid item, importantly storing the
fullUrlin adata-full-srcattribute on the.grid-itemdiv. - Finally, it sets the
innerHTMLof thegalleryGridcontainer.
- Loops from
openModal(imageUrl): Sets thesrcof the.modal-contentimage and adds theactiveclass to the modal element. It also setsdocument.body.style.overflow = 'hidden'to prevent scrolling the background page while the modal is open.closeModal(): Removes theactiveclass from the modal and restores the body's overflow. It also clears the modal image'ssrc.- Event Listeners:
- An event listener is added to the
galleryGridcontainer itself. This uses event delegation. When a click occurs within the grid, it checks if the click originated from (or inside) a.grid-item. If so, it retrieves thedata-full-srcand callsopenModal. This is more efficient than adding a listener to every individual image. - Listeners are added to the close button and the modal background itself to call
closeModal.
- An event listener is added to the
This setup provides a robust, responsive, and visually appealing image gallery with a smooth modal viewing experience.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.