Hello friends! Today we want to talk about how to create a simple image gallery using CSS. Galleries are a great way to display images beautifully and attractively, and they can enhance the user experience. In this tutorial, I will explain the steps to create a stylish and user-friendly image gallery in Persian language with simple explanations.
First of all, you should know that creating an image gallery solely with CSS is not very difficult. With a little practice and a good understanding of how to use Flexbox or CSS Grid, you can easily create a good gallery. However, before we dive into the code details, let's talk briefly about the importance of a good image gallery. Galleries can organize images better and provide easier access to them. Likewise, web designers use them to showcase samples or products that users want to see.
Now that we are familiar with the importance of image galleries, let's take a look at a code example that can help you create a beautiful image gallery with CSS. This example uses CSS Grid, but you can also achieve a similar result with Flexbox.
Example Code for Creating an Image Gallery
<div class="gallery">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<img src="image4.jpg" alt="Image 4">
</div>
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
}
.gallery img {
width: 100%;
height: auto;
border-radius: 8px;
}
</style>
Code Explanation Line by Line
<div class="gallery">
This is a main container for the gallery.
<img src="image1.jpg" alt="Image 1">
This line adds the first image to the gallery, using image1.jpg as the source.
.gallery { display: grid; }
This container gallery uses the CSS Grid system to organize the images.
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
This defines the columns that will automatically fill based on the display size.
gap: 10px;
This specifies the space between each image.
.gallery img { width: 100%; }
This ensures that images fill the entire width of the column they are placed in.
border-radius: 8px;
This adds a slight curve to the corners of each image for a more appealing look.