CSS code for arranging photos in Moments

CSS code for arranging photos in Moments

First, you can open Moments and observe several layouts with different numbers of pictures, or refer to the example below;

It can be found that except for 1 picture and 4 pictures, all other pictures are arranged in a row and three columns;

Assume there is the following HTML code, where imgList is an array of image addresses;

<ul>
    <li v-for="(item, index) in imgList" :key=index >
        <img :src="item">
    </li>
</ul>

1. First, we use flex to achieve a normal three-column layout:

Set to wrap, each element occupies 1/3 or the specified width, except for the last element (3n) of each line, which sets margin-right and reserves space;

ul{
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
li{
  width: 32%; 
  height: 100px;
  margin-top: 5px;
}
.list:not(:nth-child(3n)) {
  margin-right: 2%;
}

2. If there is only one picture, just use the CSS selector to determine that it is a picture and change the picture size;

Selector logic: When the element is the last element && the first element, it can be determined that there is only one element: set its style separately to overwrite the original style

ul li:nth-last-child(1):first-child{
  width: 200px;
  height: 200px;
}

3. For the case of four pictures, the pictures need to be laid out in two rows and two columns: Here, you need to add margin-right to the second picture in this case to change three columns into two columns:

Selector logic: When the element is the fourth to last && the first element, it is judged that there are four elements in total.
Then select the 2nth element of the same level and add the margin-right attribute to it;

ul li:nth-last-child(4):first-child ~ li:nth-child(2n){
  margin-right: 32%;
}

Before that, you need to restore the interval of the third element, or similar to the Moments, the four photos are not displayed in intervals. If necessary, you can also set other attributes, as follows: (This attribute must be before the previous attribute)

ul li:nth-last-child(4):first-child , ul li:nth-last-child(4):first-child ~ li{
  width: 50%;
  margin-right: 0;
}

Summarize

This is the end of this article about how to use CSS to arrange photos in Moments. For more information about CSS picture arrangement and layout, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  SQL implementation of LeetCode (196. Delete duplicate mailboxes)

>>:  Analysis of the methods of visual structure layout design for children's websites

Recommend

Analysis of Sysbench's benchmarking process for MySQL

Preface 1. Benchmarking is a type of performance ...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

How to modify the initial password of a user in mysql5.7

When users install MySQL database for the first t...

CSS3 timeline animation

Achieve results html <h2>CSS3 Timeline</...

Example code for implementing the wavy water ball effect using CSS

Today I learned a new CSS special effect, the wav...

Detailed explanation of the life cycle of Angular components (Part 2)

Table of contents 1. View hook 1. Things to note ...

CSS3 achieves flippable hover effect

CSS3 implements a flippable hover effect. The spe...

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

CSS pixels and solutions to different mobile screen adaptation issues

Pixel Resolution What we usually call monitor res...

Native JS to implement the aircraft war game

This article example shares the specific code of ...