Summary of the use of CSS scope (style splitting)

Summary of the use of CSS scope (style splitting)

1. Use of CSS scope (style division)

In Vue, make the CSS style take effect only in the current component: scoped attribute is a new attribute in HTML5. It is a Boolean attribute. If this attribute is used, the style is only applied to the parent element and its child elements of the style element.

insert image description here

2. Implementation principle of scoped

The effect of scoped attributes in Vue is mainly achieved through PostCSS translation. The following is the Vue code before translation:

  <style scoped>
    .test {
      color: blue;
    }
  </style>
  
  <template>
    <div class="test">Hi world</div>
  </template>

After translation:

  <style>
    .test[data-v-5559930f] {
      color: blue;
    }
  </style>
  
  <template>
    <div class="test" data-v-5559930f>Hi world</div>
  </template>

PostCSS adds a unique dynamic property to all DOM in a component, and then adds an additional corresponding attribute selector to the CSS selector to select the DOM in the component. This approach makes the style only work on the DOM containing the property <component internal DOM>.

3. Scoped penetration method

In many projects, when referencing a third-party component, you need to modify the style of the third-party component locally in the component, but you don't want to remove the scoped attribute to cause style pollution between components. At this time, some special methods are needed to penetrate the scoped.

Method 1: Use >>> to penetrate the scoped attribute and modify the styles of other third-party components.

insert image description here

Method 2: Use two style tags, one with a scoped attribute and one without scoped attribute, to modify the style of third-party components.

insert image description here

Method 3: Use sass or less styles to penetrate /deep/

insert image description here

Method 4: Distinguish by adding id or class at the outermost layer.

This is the end of this article about CSS scope (style splitting). For more relevant CSS scope content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  How to quickly query 10 million records in Mysql

>>:  Introduction to setting up Tomcat to start automatically on Linux system

Recommend

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

Detailed explanation of the error problem of case when statement

Preface In the MySQL database, sometimes we use j...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

How to check if data exists before inserting in mysql

Business scenario: The visitor's visit status...

Examples of using provide and inject in Vue2.0/3.0

Table of contents 1. What is the use of provide/i...

Do you know how to use vue-cropper to crop pictures in vue?

Table of contents 1. Installation: 2. Use: 3. Bui...

Import csv file into mysql using navicat

This article shares the specific code for importi...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

Tutorial on installing MySQL 5.7.28 on CentOS 6.2 (mysql notes)

1. Environmental Preparation 1.MySQL installation...

Nginx/Httpd load balancing tomcat configuration tutorial

In the previous blog, we talked about using Nginx...

Solution to the gap between divs

When you use HTML div blocks and the middle of th...

Linux system disk formatting and manually adding swap partition

Windows: Support NTFS, FAT Linux supports file fo...

Analysis of the use of the MySQL database show processlist command

In actual project development, if we have a lot o...

Use of Linux stat command

1. Command Introduction The stat command is used ...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...