Beginner60 minutesHTMLJavaScriptImagesInteractive

Photo Gallery Project

Project Objectives

  • Create a responsive photo gallery using HTML
  • Practice working with image elements and their attributes
  • Implement proper semantic HTML structure
  • Add captions and descriptions to images
  • Ensure accessibility with proper alt text

What You'll Learn

Basic Skills

  • HTML image elements
  • Semantic HTML structure
  • Basic file organization

Advanced Topics

  • Image optimization
  • Search functionality
  • Accessibility practices

Prerequisites: Before starting this project, you should complete these tutorials:

Required Knowledge

Project Description

In this project, you'll create a photo gallery webpage that showcases a collection of images with captions. This is a great way to practice working with the HTML <img> element and its attributes, while creating an engaging visual experience.

Your Photo Gallery Should Include:

  • A proper HTML document structure
  • A heading and brief introduction to your gallery
  • At least 6-8 images with appropriate alt text
  • Captions for each image
  • Proper organization using semantic HTML elements
  • Optional: image thumbnails that link to larger versions

Getting Started

Step 1: Set Up Your Project

Create a new directory for your project and set up the basic file structure. You'll need an index.html file and an images folder to store your photos.

Step 2: Create the Structure

Start with a basic HTML5 document structure. Include proper meta tags and a title for your gallery.

Step 3: Add Content

Add your images to the gallery, making sure to include descriptive alt text and captions for each image.

Step 4: Enhance Your Project

Consider adding features like image thumbnails, a grid layout, or hover effects to make your gallery more engaging.

Step 2: Add Your Gallery Header

Inside the body element, add a header section with your gallery title and introduction:

Step 3: Create Your Gallery Structure

Set up the main gallery section using semantic HTML:

Step 4: Add Individual Gallery Items

Add your images with captions to the gallery:

Important: For this project, you'll need to have some image files. You can use your own photos or download free stock images from sites like Unsplash, Pexels, or Pixabay.

Project Example

Here's what your completed photo gallery might look like:

Remember: This is just an example. Use your own images and customize the gallery to reflect your personal style or theme!

Challenge Yourself

Once you've completed the basic requirements, try these challenges:

Level Up Your Photo Gallery

  • Create thumbnail images that link to larger versions of each photo
  • Add a lightbox effect using the <dialog> element
  • Organize your images into different categories or sections
  • Add metadata like the date taken, location, or camera settings
  • Include a contact or about section for the photographer
  • Add proper image dimensions using width and height attributes

Submit Your Project

Once you've completed your photo gallery, you can:

  • Save it to your computer for future reference
  • Share it with friends and family to showcase your photos
  • Deploy it to a free hosting service like GitHub Pages
  • Continue to expand it with more photos and features

Next Steps: After completing this project, you might want to check out the CSS Basics tutorials to learn how to style your gallery and make it even more visually appealing.

Search Your Gallery

JavaScript Guidance

To add interactivity to your photo gallery, you can implement a search functionality using JavaScript. This will allow users to filter images based on their captions. Here's how you can do it:

Example JavaScript Code:


const searchQuery = ref('');

const images = ref([
	{ id: 1, src: 'image1.jpg', alt: 'Image 1', caption: 'Beautiful Landscape' },
	{ id: 2, src: 'image2.jpg', alt: 'Image 2', caption: 'City Skyline' },
	{ id: 3, src: 'image3.jpg', alt: 'Image 3', caption: 'Mountain View' },
	{ id: 4, src: 'image4.jpg', alt: 'Image 4', caption: 'Ocean Breeze' },
	{ id: 5, src: 'image5.jpg', alt: 'Image 5', caption: 'Forest Path' },
	{ id: 6, src: 'image6.jpg', alt: 'Image 6', caption: 'Desert Dunes' },
]);

const filteredImages = computed(() => {
	return images.value.filter(image =>
		image.caption.toLowerCase().includes(searchQuery.value.toLowerCase())
	);
});
				

In this example, we use a ref to store the search query and an array of image objects. The computed property filteredImages dynamically filters the images based on the search query, updating the displayed images in real-time.

Your Photo Gallery

Setting Up Your Images

Getting Free Images

You can get high-quality, free images from these websites:

  • Unsplash - Free to use images, no attribution required
  • Pexels - Free to use images, attribution appreciated but not required
  • Pixabay - Free to use images under Pixabay license

Tips for Image Management

Tip: When downloading images, make sure to:

  • Choose appropriate sizes (800px to 1200px width is usually good)
  • Save them with meaningful filenames (e.g., "mountain-sunset.jpg")
  • Store them in an "images" folder in your project

Adding Search Functionality

To make your gallery searchable, you'll need to:

1. Create an Image Data Structure


const images = [
	{
		id: 1,
		src: 'images/mountain-sunset.jpg',
		alt: 'Beautiful mountain landscape at sunset',
		caption: 'Mountain Sunset',
		tags: ['nature', 'mountains', 'sunset']
	},
	// Add more images here
];
				

2. Add the Search Input


<div class="search-section">
	<input 
		type="text"
		placeholder="Search images..."
		id="gallery-search"
	>
</div>
				

3. Add JavaScript for Search


const searchInput = document.getElementById('gallery-search');
const galleryContainer = document.querySelector('.gallery');

searchInput.addEventListener('input', (e) => {
	const searchTerm = e.target.value.toLowerCase();
	
	images.forEach(image => {
		const galleryItem = document.getElementById(`gallery-item-${image.id}`);
		const matchesSearch = image.caption.toLowerCase().includes(searchTerm) ||
							 image.tags.some(tag => tag.toLowerCase().includes(searchTerm));
		
		galleryItem.style.display = matchesSearch ? 'block' : 'none';
	});
});
				

Challenge

Challenge: Try extending the search functionality to:

  • Add filters for different categories
  • Sort images by date or name
  • Add a "no results found" message

Common Issues & Solutions

Images Not Loading

  • Check file paths are correct
  • Verify image file extensions
  • Ensure images are in the correct folder

Performance Tips

  • Optimize images before uploading (use tools like ImageOptim)
  • Use appropriate image dimensions
  • Consider using WebP format with fallbacks