Vue PostCSS的使用介紹
PostCSS
postcss 一種對css編譯的工具,類似babel對js的處理,常見的功能如:
1 . 使用下一代css語法
2 . 自動(dòng)補(bǔ)全瀏覽器前綴
3 . 自動(dòng)把px代為轉(zhuǎn)換成rem
4 . css 代碼壓縮等等
使用
創(chuàng)建好項(xiàng)目并且初始化npm init -y
創(chuàng)建一個(gè)頁面,一個(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文件,然而并沒有啥變化
使用第三方插件autoprefixer
Autoprefixer是一款自動(dòng)管理瀏覽器前綴的插件,它可以解析CSS文件并且添加瀏覽器前綴到CSS內(nèi)容里
主要用于處理兼容性問題
可以查看瀏覽器前綴信息
npx autoprefixer --info
運(yùn)行
在-u 后面加上插件
npx postcss index.css -o dist.css -u autoprefixer
如果覺得以上運(yùn)行方式太垃,那我們就開啟新的方式吧!!!
使用第三方插件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;
}
是不是覺得很方便很beautiful~
使用第三方插件postcss-pxtorem
它是一款自動(dòng)將px轉(zhuǎn)rem的插件
npm i --dev postcss-pxtorem
然后就可以正常使用了
本來是這樣的:
.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)換
]
}
這樣就能使用了
通過npx postcss 源文件名.css -o 編譯后文件名.css
如果還覺得繁瑣可以在package.json中進(jìn)行配置簡化該運(yùn)行命令!!
到此這篇關(guān)于Vue PostCSS的使用介紹的文章就介紹到這了,更多相關(guān)Vue PostCSS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue結(jié)合echarts繪制一個(gè)支持切換的折線圖實(shí)例
這篇文章主要介紹了vue結(jié)合echarts繪制一個(gè)支持切換的折線圖實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue深度監(jiān)聽(監(jiān)聽對象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實(shí)例
這篇文章主要介紹了vue深度監(jiān)聽(監(jiān)聽對象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Vue聲明式導(dǎo)航與編程式導(dǎo)航示例分析講解
這篇文章主要介紹了Vue中聲明式導(dǎo)航與編程式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
element-UI el-table修改input值視圖不更新問題
這篇文章主要介紹了element-UI el-table修改input值視圖不更新問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
使用idea創(chuàng)建vue項(xiàng)目的圖文教程
Vue.js是一套構(gòu)建用戶界面的框架,只關(guān)注視圖層,它不僅易于上手,還便于與第三方庫或既有項(xiàng)目整合,下面這篇文章主要給大家介紹了關(guān)于使用idea創(chuàng)建vue項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-08-08
vue使用echarts實(shí)現(xiàn)立體柱形圖
這篇文章主要為大家詳細(xì)介紹了vue使用echarts實(shí)現(xiàn)立體柱形圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
移動(dòng)端調(diào)試神器vConsole使用詳解
vConsole?是框架無關(guān)的,可以在?Vue、React?或其他任何框架中使用,今天通過本文給大家介紹移動(dòng)端調(diào)試神器vConsole使用,感興趣的朋友一起看看吧2022-04-04

