How to create a responsive gallery layout using HTML and CSS
Introduction:
With the popularity of mobile devices, responsive design has become important in web design One of the considerations. When developing a gallery website, how to design a page with both beautiful and responsive layout will become an important issue. This article will discuss in detail how to use HTML and CSS to create a responsive gallery layout, and provide specific code examples.
<div class="gallery"> <figure> <img src="image1.jpg" alt="Image 1"> <figcaption>Image 1</figcaption> </figure> <figure> <img src="image2.jpg" alt="Image 2"> <figcaption>Image 2</figcaption> </figure> <!-- More images... --> </div>
In this example, we wrap each image with the <figure>
element, <img alt="How to create a responsive gallery layout using HTML and CSS" >## The # element is used to display the image, and
is used to add a title to the image.
.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); grid-gap: 20px; } .gallery figure { margin: 0; } .gallery img { width: 100%; height: auto; display: block; } .gallery figcaption { text-align: center; }
display: gridConvert the
.gallery container to a grid layout.
grid-template-columns defines the width of each column.
repeat(auto-fit, minmax(300px, 1fr))Indicates the number and size of grid columns, which can automatically adapt to the width of the container, and the minimum width is 300px.
grid-gap is used to set the spacing between grid items.
@media (max-width: 768px) { .gallery { grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); } }
.gallery container when the width is less than 768px.
.galleryThe container will be reset to a minimum width of 250px per column.
.gallery figure:hover img { transform: scale(1.1); transition: transform 0.3s ease; } .gallery figure:hover figcaption { opacity: 1; transition: opacity 0.3s ease; } .gallery figcaption { opacity: 0; transition: opacity 0.3s ease; }
hover pseudo-class selector to specify that the mouse is hovering over the
.gallery figure element when changing the image's scaling and title's opacity. The
transform attribute is used to set the scaling of the image, and the
transition attribute is used to set the duration and transition function of the transition effect.
The above is the detailed content of How to create a responsive gallery layout using HTML and CSS. For more information, please follow other related articles on the PHP Chinese website!