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

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

MySQL 8.0.19 Installation Tutorial

Download the installation package from the offici...

Detailed introduction to JS basic concepts

Table of contents 1. Characteristics of JS 1.1 Mu...

Detailed explanation of the production principle of jQuery breathing carousel

This article shares the specific process of the j...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

Element avatar upload practice

This article uses the element official website an...

How many pixels should a web page be designed in?

Many web designers are confused about the width of...

MySQL 8.0.18 adds users to the database and grants permissions

1. It is preferred to use the root user to log in...

Pure CSS to achieve click to expand and read the full text function

Note When developing an article display list inte...

Solution to forget password when installing MySQL on Linux/Mac

Preface This article mainly introduces the releva...

A brief discussion on the binary family of JS

Table of contents Overview Blob Blob in Action Bl...

Code for aligning form checkbox and radio text

Alignment issues like type="radio" and t...

Detailed explanation of the marquee attribute in HTML

This tag is not part of HTML3.2 and is only suppo...

How to implement real-time polygon refraction with threejs

Table of contents Preface Step 1: Setup and front...