How to use CSS to write different styles according to sub-elements

How to use CSS to write different styles according to sub-elements

The effect we need to achieve:

What is needed

The styles of 1 picture, 2 pictures, and 3 pictures are different. You can use js to complete the judgment of child elements, but here I use css to complete it

Core knowledge points

Use CSS selectors to determine child elements

example:

Use CSS selector to match only one element

div {
    &:last-child:nth-child(1) {
      // Related styles}
}

It is easy to understand: the last element under the div is also the first element, so doesn’t it have only one child element?

Use CSS selector to match only two child elements

div{
    &:nth-last-child(2):nth-child(2) {
    
    }
}

Following the same pattern: The second element at the end is also the second element. Doesn’t that mean there are only two elements under this div?

Finished Style

HTML part

     <div class="box" v-for="(item,index) in list" :key="index">
          <div class="header">
            <img :src="item.userImage" alt="">
            <span>{{item.name}}</span>
          </div>
          <div class="content">
            <img :src="v" alt="" v-for="(v, i) in item.imageUrl" :key="i">
          </div>
          <div class="bottom">
            <span class="left-icon">{{item.createTime}}</span>
            <div class="right">
              <img src="./img/6.1.png" alt="">
              <span>{{item.fabulousNumber}}</span>
            </div>
          </div>
        </div>

CSS part

.box {
      padding: 0.26rem;

      .header {
        display: flex;
        align-items: center;

        img {
          width: 0.58rem;
          height: 0.58rem;
          margin-right: 0.17rem;
        }
      }

      .bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #999999;
        font-size: 0.17rem;

        img {
          width: 0.17rem;
          height: 0.17rem;
        }
      }

      .content {
        display: flex;
        margin: 0.17rem 0;

        img {
          flex: 1;
          height: 1.37rem;
          width: 0;
          margin-right: 0.09rem;
          &:last-child {
            margin-right: 0;
          }
          &:last-child:nth-child(1) {
            height: 2.75rem;
          }
        }
      }
    }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to view and set the mysql time zone

>>:  JavaScript to implement a simple clock

Recommend

Linux kernel device driver kernel linked list usage notes

/******************** * Application of linked lis...

A brief discussion on the design of Tomcat multi-layer container

Table of contents Container Hierarchy The process...

MySQL data compression performance comparison details

Table of contents 1. Test environment 1.1 Hardwar...

MySQL 8.0.20 compressed version installation tutorial with pictures and text

1. MySQL download address; http://ftp.ntu.edu.tw/...

JavaScript Interview: How to implement array flattening method

Table of contents 1 What is array flattening? 2 A...

Detailed deployment of Alibaba Cloud Server (graphic tutorial)

I have recently learned web development front-end...

Vue+webrtc (Tencent Cloud) practice of implementing live broadcast function

Table of contents 1. Live broadcast effect 2. Ste...

CSS realizes process navigation effect (three methods)

CSS realizes the process navigation effect. The s...

Centos8 builds nfs based on kdc encryption

Table of contents Configuration nfs server (nfs.s...

Detailed explanation of XML syntax

1. Documentation Rules 1. Case sensitive. 2. The a...

mysql5.7 installation and configuration tutorial under Centos7.3

This article shares the MySQL 5.7 installation an...

React Router 5.1.0 uses useHistory to implement page jump navigation

Table of contents 1. Use the withRouter component...

JavaScript realizes the drag effect of modal box

Here is a case of modal box dragging. The functio...

Learn MySQL database in one hour (Zhang Guo)

Table of contents 1. Database Overview 1.1 Develo...