There are four ways to set styles in Vue.js: using inline styles, local (scoped) styles, Sass/SCSS preprocessors, and CSS modules. Inline styles are written directly into templates; local styles only apply to the current component; Sass/SCSS provides more powerful style writing capabilities; CSS modules generate unique class names to avoid conflicts.
Methods to set styles in Vue
In Vue.js, you can use a variety of methods to set styles for components and Add styles to elements.
1. Inline style
Inline style is written directly into the component template, which is the simplest style setting method.
<template> <div style="color: red; font-size: 20px;">Hello World</div> </template>
2. Local style (scoped)
Scoped style only applies to the current component and its internal elements, which can prevent style pollution.
<template> <style scoped> .my-class { color: blue; font-weight: bold; } </style> <div class="my-class">Hello World</div> </template>
3. Sass/SCSS
Sass and SCSS are CSS preprocessors that can be used in Vue.js to write more powerful styles.
// main.scss .my-component { color: #f00; font-size: 1.2rem; }
<template> <style lang="scss"> @import "./main.scss"; </style> <div class="my-component">Hello World</div> </template>
4. CSS Modules
CSS modules prevent style conflicts by generating unique class names, often used with webpack.
// App.vue import styles from './App.module.css'; export default { render() { return ( <div className={styles.myClass}>Hello World</div> ); } }
The above is the detailed content of The method used to set styles in vue is. For more information, please follow other related articles on the PHP Chinese website!