In-depth explanation of the style feature in Vue3 single-file components

In-depth explanation of the style feature in Vue3 single-file components

style scoped

Things to note:

  • The style will not affect other components and will only take effect on the current component.
  • The root element of a child component will be affected by both the scoped styles of the parent component and the scoped styles of the child component. The purpose of this is to allow the parent component to adjust the layout of the child component.
  • There are 3 special selectors:

1. Deep selector: can affect child components. Use pseudo class => :deep(cls: affected selector)

    .a :deep(.b) {
        ...
    }

2. Slot selector: can affect the style of the slot content. Use pseudo-class => :slotted(selector)

    :slloted(.a) {
        ...
    }

3. Global selector: The style affects the global environment. Using pseudo-class => :global(selector)

    :slloted(.a) {
        ...
    }

scoped style can exist with style

style module

The style tag contains a module. Its style, like style scoped, can only be scoped to the current component.

This method compiles CSS into CSS modules and exposes them to the component $styles object to use CSS styles.

<template>
  <p :class="$style.red">
    This should be red
  </p>
</template>

<style module>
.red {
  color: red;
}
</style>

You can assign values ​​to the module to customize the name of the exposed object

<template>
  <p :class="style.red">
    This should be red
  </p>
</template>

<style module='style'>
.red {
  color: red;
}
</style>

You can use the useCssModule() api to use cssModule in the combined api.

// By default, returns the class in <style module> useCssModule()

// Naming, returning the class in <style module="classes"> useCssModule('classes')

State-driven dynamic CSS

You can use v-bind() to associate CSS values ​​with dynamic component states.

<template>
  <div class="text">hello</div>
</template>

<script>
export default {
  data() {
    return {
      color: 'red'
    }
  }
}
</script>

<style>
.text {
  color: v-bind(color);
}
</style>

Summarize

This is the end of this article about the style feature in vue3 single-file components. For more relevant vue3 single-file component style feature content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Practice of using Vite2+Vue3 to render Markdown documents
  • Vue3 navigation bar component encapsulation implementation method
  • Vue3 Vue Event Handling Guide
  • vue3.0+echarts realizes three-dimensional column chart
  • This article will show you how to use Vue 3.0 responsive
  • Detailed explanation of the underlying principle of defineCustomElement added in vue3.2
  • Vue3 + TypeScript Development Summary
  • Vue3+TypeScript implements a complete example of a recursive menu component
  • Vue3 implements a todo-list
  • Vue3+script setup+ts+Vite+Volar project
  • How to implement logic reuse with Vue3 composition API
  • Vue3 realizes the image magnifying glass effect
  • Implementation of vue3.0+vant3.0 rapid project construction
  • Vue3 Documentation Quick Start

<<:  MySQL 8.0.20 installation and configuration method graphic tutorial under Windows 10

>>:  How to add shortcut commands in Xshell

Recommend

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

How to change the database data storage directory in MySQL

Preface The default database file of the MySQL da...

Example code for implementing dynamic skinning with vue+element

Sometimes the theme of a project cannot satisfy e...

HTML basic structure_Powernode Java Academy

Many times when learning web page development, th...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

A brief discussion on the use of React.FC and React.Component

Table of contents 1. React.FC<> 2. class xx...

Key features of InnoDB - insert cache, write twice, adaptive hash index details

The key features of the InnoDB storage engine inc...

How to configure Nginx load balancing

Table of contents Nginx load balancing configurat...

Detailed explanation of JS array methods

Table of contents 1. The original array will be m...

A detailed discussion on detail analysis in web design

In design work, I often hear designers participati...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...

linux No space left on device 500 error caused by inode fullness

What is an inode? To understand inode, we must st...

MySQL series 6 users and authorization

Table of contents Tutorial Series 1. User Managem...