SmartCodingTips

Photo Gallery in HTML & CSS

What is a Photo Gallery?

A photo gallery is a grid or layout of images displayed on a web page, often used to showcase photography, products, portfolios, or memories in an organized and visually appealing format.

HTML Structure

You can use a combination of HTML elements and CSS (Flexbox or Grid) to layout your gallery. Below is a simple example:


<div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
  <img src="img1.jpg" alt="Image 1" class="w-full h-auto rounded">
  <img src="img2.jpg" alt="Image 2" class="w-full h-auto rounded">
  <img src="img3.jpg" alt="Image 3" class="w-full h-auto rounded">
  <img src="img4.jpg" alt="Image 4" class="w-full h-auto rounded">
</div>
            

Best Practices

  • Use descriptive alt attributes for accessibility and SEO.
  • Compress images for faster page loading.
  • Use a consistent aspect ratio (e.g., square or 3:2) for a clean layout.
  • Consider adding hover effects or lightbox popups for a better user experience.

Responsive Gallery with Tailwind

Tailwind's grid utilities help create responsive galleries easily:


<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
  <img src="gallery1.jpg" class="w-full rounded shadow">
  <img src="gallery2.jpg" class="w-full rounded shadow">
  <img src="gallery3.jpg" class="w-full rounded shadow">
  <img src="gallery4.jpg" class="w-full rounded shadow">
</div>