


How to use style penetration to modify elementUI default style in vue3 project
1. Style modularization
In a single css file, we write the style of the component in the style tag. You can see that generally the style tag will have a scoped attribute, so that different components can be realized in real time. The selectors are the same, but the styles do not interfere with each other.
Look at an example. We define a hello-world-box
class in both components and set different values in the corresponding scope tags. style.
As you can see, vue adds a unique attribute (PostCSS translation implementation)
to the labels in different components. Then, through the attribute selector, the label styles of different attributes do not interfere with each other.
The function of the css attribute selector is to set styles for HTML elements with specific attributes
.hello-world-box[data-v-e17ea971] { color: red; }
This is also the principle of style tag scoped attribute to achieve style modularization.
When a style tag has a scoped attribute, its CSS style can only be applied to the current component, that is, the style can only be applied to the current component element. Through this attribute, the styles between components can be prevented from contaminating each other. If all style tags in a project are scoped, it is equivalent to realizing style modularization.
2. Style penetration implementation
After understanding the implementation of style modularization in vue, let’s get to the point. How to customize the style of the components in the elementUI component library?
This is actually a relatively common requirement, because some UI diagrams are not drawn using components from the element component library, so there must be deviations in the style.
Let’s take a look at el-table
As you can see, the styles of element components are implemented through external style files, so there is no vue on the corresponding label added attributes.
Then if we directly add styles to components using element components, it will not take effect. Externally imported style files have higher priority.
1. External css file
We can define a css file ourselves, and then write the corresponding style to be modified.
For example: styles.css
Introduced in the entry file main.js:
Pay attention to the introduction here The order in which element css files and custom css files are introduced, because css styles take effect first.
It took effect.
But there is actually a problem with this: the style file affects all components, that is, when we call this component in other pages, the style is also modified.
One of the solutions is to add a custom class name to the class corresponding to the component.
2, :deep()
:deep()
: Change the position of private attributes when css is parsed
.outer { .el-input__inner { // 此时css解析的为 .outer .el-input__inner[data-v-xxxx] 样式无效 background: pink; } :deep(.el-input__inner) { // 此时css解析的为 .outer[data-v-xxxx] .el-input__inner 样式生效 background: red; } }
3, :slotted()
:slotted()
: Define the style slot content style in the sub-component
By default, the scope style will not affect <slot></slot>
Rendered content, because they are considered to be held and passed in by the parent component.
<template> <div> <slot>插槽</slot> </div> </template> <style lang="less" scoped> :slotted(.red) { color: red; } </style>
4. :global()
:global(): Global selector, defining global style without opening an unscoped style.
<style scoped> :global(.red-box) { color: red; } </style> <!-- 等效于 --> <style> .red-box{ color:red } </style>
5. Dynamic css (v-bind)
The <style></style>
tag of vue3 single file component supports the use of v-bind CSS function
Link the CSS value to the dynamic component state
, that is, we can introduce the responsive variable in the script tag in the style tag:
<template> <el-table :data="tableData" > <el-table-column prop="date" label="Date" width="180" /> <el-table-column prop="name" label="Name" width="180" /> <el-table-column prop="address" label="Address" /> </el-table> </template> <script lang="ts" setup> import { ref } from 'vue'; const tableData = [ { date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-02', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-04', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-01', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', }, ] const redColor = ref('red') </script> <style scoped> .el-table { color: v-bind(redColor); } </style>
You can see that even in scoped style tags, style penetration takes effect.
The actual values are compiled into hashed CSS custom properties, so the CSS itself remains static. Custom attributes will be applied to the root element of the
component through inline styles, and will be updated responsively when the source value changes.
The above is the detailed content of How to use style penetration to modify elementUI default style in vue3 project. For more information, please follow other related articles on the PHP Chinese website!

Vue.js is a progressive JavaScript framework suitable for building efficient and maintainable front-end applications. Its key features include: 1. Responsive data binding, 2. Component development, 3. Virtual DOM. Through these features, Vue.js simplifies the development process, improves application performance and maintainability, making it very popular in modern web development.

Vue.js and React each have their own advantages and disadvantages, and the choice depends on project requirements and team conditions. 1) Vue.js is suitable for small projects and beginners because of its simplicity and easy to use; 2) React is suitable for large projects and complex UIs because of its rich ecosystem and component design.

Vue.js improves user experience through multiple functions: 1. Responsive system realizes real-time data feedback; 2. Component development improves code reusability; 3. VueRouter provides smooth navigation; 4. Dynamic data binding and transition animation enhance interaction effect; 5. Error processing mechanism ensures user feedback; 6. Performance optimization and best practices improve application performance.

Vue.js' role in web development is to act as a progressive JavaScript framework that simplifies the development process and improves efficiency. 1) It enables developers to focus on business logic through responsive data binding and component development. 2) The working principle of Vue.js relies on responsive systems and virtual DOM to optimize performance. 3) In actual projects, it is common practice to use Vuex to manage global state and optimize data responsiveness.

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment