Vue PostCSS的使用介紹
PostCSS
postcss 一種對(duì)css編譯的工具,類似babel對(duì)js的處理,常見(jiàn)的功能如:
1 . 使用下一代css語(yǔ)法
2 . 自動(dòng)補(bǔ)全瀏覽器前綴
3 . 自動(dòng)把px代為轉(zhuǎn)換成rem
4 . css 代碼壓縮等等
使用
創(chuàng)建好項(xiàng)目并且初始化npm init -y
創(chuàng)建一個(gè)頁(yè)面,一個(gè)css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PostCSS</title> <link rel="stylesheet" href="./index.css" rel="external nofollow" > </head> <body> <div class="box"> <div class="box_1">盒子1</div> <div class="box_2">盒子2</div> <div class="box_3">盒子3</div> </div> </body> </html>
css
body{ background-color: black; } .box{ display: flex; justify-content: space-between; text-align: center; } .box_1{ width: 200px; height: 100px; background-color: red; font-size: 18px; &:hover{ background-color: blue; } } .box_2{ width: 200px; height: 100px; background-color: yellow; } .box_3{ width: 200px; height: 100px; background-color: blue; }
安裝依賴
npm i postcss postcss-cli
運(yùn)行
npx
是高版本node可以使用的
npx postcss 源文件名.css -o 編譯后的文件名.css
這樣就能轉(zhuǎn)換一個(gè)新css文件,然而并沒(méi)有啥變化
使用第三方插件autoprefixer
Autoprefixer是一款自動(dòng)管理瀏覽器前綴的插件,它可以解析CSS文件并且添加瀏覽器前綴到CSS內(nèi)容里
主要用于處理兼容性問(wèn)題
可以查看瀏覽器前綴信息
npx autoprefixer --info
運(yùn)行
在-u 后面加上插件
npx postcss index.css -o dist.css -u autoprefixer
如果覺(jué)得以上運(yùn)行方式太垃,那我們就開(kāi)啟新的方式吧!!!
使用第三方插件postcss-preset-env
postcss-preset-env是一個(gè)兼容瀏覽器,給一些css加上前綴的插件
npm i --dev postcss-preset-env
運(yùn)行后可以發(fā)現(xiàn)會(huì)自動(dòng)給你做兼容性處理
源代碼:
body{ background-color: black; } .box{ display: flex; justify-content: space-between; text-align: center; } .box_1{ width: 200px; height: 100px; background-color: red; &:hover{ background-color: blue; } } .box_2{ width: 200px; height: 100px; background-color: yellow; } .box_3{ width: 200px; height: 100px; background-color: blue; }
編譯后
body{ background-color: black; } .box{ display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -webkit-justify-content: space-between; -moz-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; text-align: center; } .box_1{ width: 200px; height: 100px; background-color: red; font-size: 1.125rem; } .box_1:hover{ background-color: blue; } .box_2{ width: 200px; height: 100px; background-color: yellow; } .box_3{ width: 200px; height: 100px; background-color: blue; }
是不是覺(jué)得很方便很beautiful~
使用第三方插件postcss-pxtorem
它是一款自動(dòng)將px轉(zhuǎn)rem的插件
npm i --dev postcss-pxtorem
然后就可以正常使用了
本來(lái)是這樣的:
.box_1{ width: 200px; height: 100px; background-color: red; font-size: 18px; }
用了它就這樣了:
.box_1{ width: 200px; height: 100px; background-color: red; font-size: 1.125rem; }
Is so good!!!
上方插件就演示這么多了,再介紹一下如何方便的運(yùn)行:
運(yùn)行的新方式
創(chuàng)建config文件
postcss.config.js
const postcssPresetEnv=require('postcss-preset-env') module.exports={ plugins: [ require("autoprefixer"), postcssPresetEnv({ stage:0 }), require("postcss-pxtorem"),//單位轉(zhuǎn)換 ] }
這樣就能使用了
通過(guò)npx postcss 源文件名.css -o 編譯后文件名.css
如果還覺(jué)得繁瑣可以在package.json中進(jìn)行配置簡(jiǎn)化該運(yùn)行命令!!
到此這篇關(guān)于Vue PostCSS的使用介紹的文章就介紹到這了,更多相關(guān)Vue PostCSS內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue如何實(shí)現(xiàn)iframe的上一步、下一步操作
這篇文章主要介紹了Vue如何實(shí)現(xiàn)iframe的上一步、下一步操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn)
VuePress為每一個(gè)由它生成的頁(yè)面提供預(yù)加載的html,不僅加載速度極佳,同時(shí)對(duì)seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn),需要的朋友可以參考下2018-04-04五分鐘教你使用vue-cli3創(chuàng)建項(xiàng)目(新手入門)
本文主要介紹了五分鐘教你使用vue-cli3創(chuàng)建項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Vue3管理后臺(tái)項(xiàng)目使用高德地圖選點(diǎn)的實(shí)現(xiàn)
本文主要介紹了Vue3管理后臺(tái)項(xiàng)目使用高德地圖選點(diǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07vue3使用el-radio-group獲取表格數(shù)據(jù)無(wú)法選中問(wèn)題及解決方法
這篇文章主要介紹了vue3使用el-radio-group獲取表格數(shù)據(jù)無(wú)法選中問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05淺談VueUse中useAsyncState的實(shí)現(xiàn)原理
useAsyncState?是 VueUse 庫(kù)中提供的一個(gè)實(shí)用工具,它用于處理異步狀態(tài),本文主要介紹了VueUse中useAsyncState的實(shí)現(xiàn)及其原理,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08