Vue PostCSS的使用介紹
PostCSS
postcss 一種對css編譯的工具,類似babel對js的處理,常見的功能如:
1 . 使用下一代css語法
2 . 自動補全瀏覽器前綴
3 . 自動把px代為轉(zhuǎn)換成rem
4 . css 代碼壓縮等等
使用
創(chuàng)建好項目并且初始化npm init -y
創(chuàng)建一個頁面,一個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
運行
npx是高版本node可以使用的
npx postcss 源文件名.css -o 編譯后的文件名.css
這樣就能轉(zhuǎn)換一個新css文件,然而并沒有啥變化
使用第三方插件autoprefixer
Autoprefixer是一款自動管理瀏覽器前綴的插件,它可以解析CSS文件并且添加瀏覽器前綴到CSS內(nèi)容里
主要用于處理兼容性問題
可以查看瀏覽器前綴信息
npx autoprefixer --info
運行
在-u 后面加上插件
npx postcss index.css -o dist.css -u autoprefixer
如果覺得以上運行方式太垃,那我們就開啟新的方式吧!!!
使用第三方插件postcss-preset-env
postcss-preset-env是一個兼容瀏覽器,給一些css加上前綴的插件
npm i --dev postcss-preset-env
運行后可以發(fā)現(xiàn)會自動給你做兼容性處理
源代碼:
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
它是一款自動將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!!!
上方插件就演示這么多了,再介紹一下如何方便的運行:
運行的新方式
創(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)行配置簡化該運行命令!!
到此這篇關(guān)于Vue PostCSS的使用介紹的文章就介紹到這了,更多相關(guān)Vue PostCSS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗
VuePress為每一個由它生成的頁面提供預(yù)加載的html,不僅加載速度極佳,同時對seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗,需要的朋友可以參考下2018-04-04
五分鐘教你使用vue-cli3創(chuàng)建項目(新手入門)
本文主要介紹了五分鐘教你使用vue-cli3創(chuàng)建項目,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
vue3使用el-radio-group獲取表格數(shù)據(jù)無法選中問題及解決方法
這篇文章主要介紹了vue3使用el-radio-group獲取表格數(shù)據(jù)無法選中問題及解決方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
淺談VueUse中useAsyncState的實現(xiàn)原理
useAsyncState?是 VueUse 庫中提供的一個實用工具,它用于處理異步狀態(tài),本文主要介紹了VueUse中useAsyncState的實現(xiàn)及其原理,具有一定的參考價值,感興趣的可以了解一下2024-08-08

