Home Web Front-end HTML Tutorial How to create a responsive photo album display layout using HTML and CSS

How to create a responsive photo album display layout using HTML and CSS

Oct 19, 2023 am 08:51 AM
css html Responsive layout Album display

How to create a responsive photo album display layout using HTML and CSS

How to use HTML and CSS to create a responsive photo album display layout

The photo album display layout is a common page layout type in websites, which can be used to display pictures, Photos, images, etc. In today's environment where mobile devices are popular, a good photo album display layout needs to have a responsive design that can adapt to different screen sizes and have good display effects on different devices.

This article will introduce how to use HTML and CSS to create a responsive photo album display layout, and provide specific code examples. I hope readers can understand and master this process through the sample code.

HTML structure

First, we need to create the HTML structure for album display. The following is a simple HTML structure example:

<div class="gallery">
  <div class="photo">
    <img src="/static/imghw/default1.png"  data-src="image1.jpg"  class="lazy" alt="Photo 1">
  </div>
  <div class="photo">
    <img src="/static/imghw/default1.png"  data-src="image2.jpg"  class="lazy" alt="Photo 2">
  </div>
  <div class="photo">
    <img src="/static/imghw/default1.png"  data-src="image3.jpg"  class="lazy" alt="Photo 3">
  </div>
  <!-- 添加更多图片 -->
</div>

In the above example, we used a container named gallery to contain all the photos in the album. Each photo is tagged using the photo class. The address and description information of the image can be obtained through the src and alt attributes of the img element. specified.

CSS Style

Next, we need to add CSS styles to the album display layout. Here is a basic CSS styling example:

.gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.photo {
  width: 30%;
  margin-bottom: 20px;
}

.photo img {
  width: 100%;
  height: auto;
}

In the above example, we first used display: flex to arrange the photo layout in the .gallery container in a row. Then use flex-wrap: wrap to achieve the effect of automatic line wrapping when the photo exceeds one line. justify-content: space-betweenUsed to set the alignment of photos in the container.

For each photo, we use the .photo class to set its width and bottom margin. To ensure that the photo automatically resizes on different screen sizes, we use a relative width of width: 30%.

Finally, we set the width and height of the photo through the .photo img selector. width: 100% means that the width of the picture fills the .photo container, height: auto is used to maintain the aspect ratio of the picture.

Responsive Design

The key to implementing responsive design is to use CSS media queries to apply different styles based on the screen size of different devices. Here is an example of responsive design:

@media only screen and (max-width: 768px) {
  .photo {
    width: 45%;
  }
}

@media only screen and (max-width: 480px) {
  .photo {
    width: 100%;
  }
}

In the above example, we have used the @media rule to create two media queries. The first media query max-width: 768px will take effect when the screen width is less than or equal to 768px, so we set the width of .photo to 45%. This ensures that the photo will fit on the screen on medium-sized devices.

The second media querymax-width: 480px will take effect when the screen width is less than or equal to 480px, so we set the width of .photo to 100%. This ensures that the photo takes up the full width of the screen on devices with small screens.

Summary

With the above HTML and CSS code examples, we can create a simple responsive photo album display layout. By using flex layout and media queries, we can ensure that the album displays well on different devices and adapts to the screens of different devices.

Of course, the above example is just a simple demonstration, and you can customize and modify it according to your needs and specific circumstances. I hope this article can help you. If you have any questions or needs, please leave a message for discussion.

The above is the detailed content of How to create a responsive photo album display layout using HTML and CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to style links in CSS? How to style links in CSS? Jul 29, 2025 am 04:25 AM

The style of the link should distinguish different states through pseudo-classes. 1. Use a:link to set the unreached link style, 2. a:visited to set the accessed link, 3. a:hover to set the hover effect, 4. a:active to set the click-time style, 5. a:focus ensures keyboard accessibility, always follow the LVHA order to avoid style conflicts. You can improve usability and accessibility by adding padding, cursor:pointer and retaining or customizing focus outlines. You can also use border-bottom or animation underscore to ensure that the link has a good user experience and accessibility in all states.

The Importance of Semantic HTML for SEO and Accessibility The Importance of Semantic HTML for SEO and Accessibility Jul 30, 2025 am 05:05 AM

SemanticHTMLimprovesbothSEOandaccessibilitybyusingmeaningfultagsthatconveycontentstructure.1)ItenhancesSEOthroughbettercontenthierarchywithproperheadinglevels,improvedindexingviaelementslikeand,andsupportforrichsnippetsusingstructureddata.2)Itboostsa

How to overlap elements in CSS? How to overlap elements in CSS? Jul 30, 2025 am 05:43 AM

To achieve CSS element overlap, you need to use positioning and z-index attributes. 1. Use position and z-index: Set elements to non-static positioning (such as absolute, relative, etc.), and control the stacking order through z-index, the larger the value, the higher the value. 2. Common positioning methods: absolute is used for precise layout, relative is used for relatively offset and overlap adjacent elements, fixed or sticky is used for fixed positioning of suspended layers. 3. Actual example: By setting the parent container position:relative, child element position:absolute and different z-index, the card overlap effect can be achieved.

What are user agent stylesheets? What are user agent stylesheets? Jul 31, 2025 am 10:35 AM

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

How to handle a button click with onclick? How to handle a button click with onclick? Jul 30, 2025 am 05:16 AM

Use the onclick attribute in HTML to directly bind click events, which is suitable for simple scenarios but is not conducive to code maintenance; 2. The onclick attribute assignment function of elements in JavaScript is more conducive to separating structures and behaviors, but will overwrite the previous event handler; 3. It is recommended to use the addEventListener method to support multiple event monitoring and better control the event flow, and should operate after the DOM is loaded to avoid common errors such as premature access to elements or quotation conflicts in HTML. Therefore, onclick is suitable for beginners and small projects, while addEventListener is more suitable for complex applications.

How to create a dashed line with CSS? How to create a dashed line with CSS? Jul 28, 2025 am 03:34 AM

Use the border attribute to set the dashed style to quickly create dotted lines, such as border-top:2pxdashed#000; 2. You can customize the appearance of the dotted line by adjusting the border width, color and style; 3. When applying the dotted line to dividers or inline elements, it is recommended to set height:0 or reset the default style of hr; 4. If you need to accurately control the length and spacing of the dotted line, you should use background-image and linear-gradient to cooperate with linear-gradient, for example, background:linear-gradient(toright, black33%, transparent33%) repe

What is the CSS `will-change` property best used for? What is the CSS `will-change` property best used for? Jul 29, 2025 am 01:05 AM

The best use scenario for CSS will-change attribute is to inform browser elements in advance of possible changes in order to optimize rendering performance, especially for animation or transition effects. ① It should be applied before the animation properties (such as transform, opacity or position) changes; ② Avoid premature use or long-term retention, and should be set before the change occurs and removed after completion; ③ It should only be used for necessary properties rather than using will-change:all; ④ Suitable for scenarios such as large scrolling animations, interactive UI components, and complex SVG/Canvas interfaces; ⑤ Modern browsers can usually optimize automatically, so there is no need to use will-change in all animations. Proper use can improve

How to create an unordered list in HTML? How to create an unordered list in HTML? Jul 30, 2025 am 04:50 AM

To create an HTML unordered list, you need to use a tag to define a list container. Each list item is wrapped with a tag, and the browser will automatically add bullets; 1. Create a list with a tag; 2. Each list item is defined with a tag; 3. The browser automatically generates default dot symbols; 4. Sublists can be implemented through nesting; 5. Use the list-style-type attribute of CSS to modify the symbol style, such as disc, circle, square, or none; use these tags correctly to generate a standard unordered list.

See all articles