Here's a complete example of an image gallery using HTML, CSS, and JavaScript that:
- Takes a URL input from the user (e.g.,
https://example.com/images/06.jpg)
- Extracts the base path by removing the numeric part of the filename
- Generates image URLs from
01.jpg to 16.jpg
- Displays them in a modern, responsive grid layout
✅ Features:
- Modern CSS Grid layout
- Responsive design
- Input validation
- Automatically generates 16 images based on the pattern
✅ Full Code (HTML + CSS + JS)
You can copy and paste this into an .html file and open it in your browser.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Image Gallery Generator</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 2rem;
background-color: #f5f7fa;
}
h1 {
text-align: center;
color: #333;
}
.input-container {
display: flex;
justify-content: center;
margin-bottom: 2rem;
}
input[type="text"] {
width: 400px;
padding: 0.6rem;
font-size: 1rem;
border: 2px solid #ccc;
border-radius: 5px;
margin-right: 1rem;
}
button {
padding: 0.6rem 1.2rem;
font-size: 1rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 1rem;
max-width: 1200px;
margin: 0 auto;
}
.gallery img {
width: 100%;
height: auto;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.gallery img:hover {
transform: scale(1.05);
}
.error {
text-align: center;
color: red;
margin-top: 1rem;
}
</style>
</head>
<body>
<h1>Image Gallery Generator</h1>
<div class="input-container">
<input type="text" id="imageUrlInput" placeholder="Enter image URL like https://example.com/images/06.jpg" />
<button onclick="generateGallery()">Generate</button>
</div>
<div id="error" class="error"></div>
<div class="gallery" id="gallery"></div>
<script>
function generateGallery() {
const input = document.getElementById('imageUrlInput').value.trim();
const errorDiv = document.getElementById('error');
const gallery = document.getElementById('gallery');
errorDiv.textContent = '';
gallery.innerHTML = '';
if (!input) {
errorDiv.textContent = 'Please enter a valid image URL.';
return;
}
// Extract the base path by removing number before .jpg
const match = input.match(/(.*?)(\d+)(\.\w+)$/);
if (!match) {
errorDiv.textContent = 'URL must end with a number before the extension (e.g., 06.jpg)';
return;
}
const basePath = match[1];
const extension = match[3];
for (let i = 1; i <= 16; i++) {
const paddedNumber = i.toString().padStart(2, '0');
const imgUrl = `${basePath}${paddedNumber}${extension}`;
const imgElement = document.createElement('img');
imgElement.src = imgUrl;
imgElement.alt = `Image ${i}`;
imgElement.onerror = () => {
imgElement.src = 'https://via.placeholder.com/150?text=Not+Found';
imgElement.alt = 'Image not found';
};
gallery.appendChild(imgElement);
}
}
</script>
</body>
</html>
🔍 How It Works
- The script uses a regular expression to extract the base path by removing the numeric portion before the extension.
- Then it loops from 1 to 16, pads each number with leading zero (
01, 02, ..., 16), and appends the extension.
- Each image is added to a responsive CSS Grid gallery.
- If an image fails to load, a placeholder is shown.
📝 Example
If you input:
code
https://example.com/images/06.jpg
It will try to load:
code
https://example.com/images/01.jpg
https://example.com/images/02.jpg
...
https://example.com/images/16.jpg
Let me know if you'd like to add features like lightbox preview, lazy loading, or support for other extensions!