
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@nikonee/postcss-pxtovpt
Advanced tools
A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).
基于 evrone/postcss-px-to-viewport 项目编写,使用 Typescript 和 PostCss8 重构。
注意:因为使用 PostCss 8 重构,所以不支持 v8 以下,如使用 v8 以下的 PostCss 版本,请使用evrone/postcss-px-to-viewport。
将 px 单位转换为视口单位的 (vw, vh, vmin, vmax) 的 PostCSS 插件.
如果你的样式需要做根据视口大小来调整宽度,这个脚本可以将你 CSS 中的 px 单位转化为 vw,1vw 等于 1/100 视口宽度。
.class {
margin: -10px 0.5vh;
padding: 5vmin 9.5px 1px;
border: 3px solid black;
border-bottom-width: 1px;
font-size: 14px;
line-height: 20px;
}
.class2 {
padding-top: 10px; /* px-to-viewport-ignore */
/* px-to-viewport-ignore-next */
padding-bottom: 10px;
/* Any other comment */
border: 1px solid black;
margin-bottom: 1px;
font-size: 20px;
line-height: 30px;
}
@media (min-width: 750px) {
.class3 {
font-size: 16px;
line-height: 22px;
}
}
.class {
margin: -3.125vw 0.5vh;
padding: 5vmin 2.96875vw 1px;
border: 0.9375vw solid black;
border-bottom-width: 1px;
font-size: 4.375vw;
line-height: 6.25vw;
}
.class2 {
padding-top: 10px;
padding-bottom: 10px;
/* Any other comment */
border: 1px solid black;
margin-bottom: 1px;
font-size: 6.25vw;
line-height: 9.375vw;
}
@media (min-width: 750px) {
.class3 {
font-size: 16px;
line-height: 22px;
}
}
使用 npm 安装
$ npm install @nikonee/postcss-pxtovpt --save-dev
使用 yarn 进行安装
$ yarn add -D @nikonee/postcss-pxtovpt
默认参数:
{
unitToConvert: 'px',
viewportWidth: 320,
unitPrecision: 5,
propList: ['*'],
viewportUnit: 'vw',
fontViewportUnit: 'vw',
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: undefined,
include: undefined,
landscape: false,
landscapeUnit: 'vw',
landscapeWidth: 568,
rules: []
}
unitToConvert
(String) 需要转换的单位,默认为"px"viewportWidth
(Number) 设计稿的视口宽度unitPrecision
(Number) 单位转换后保留的精度propList
(Array) 能转化为 vw 的属性列表
viewportUnit
(String) 希望使用的视口单位fontViewportUnit
(String) 字体使用的视口单位selectorBlackList
(Array) 需要忽略的 CSS 选择器,不会转为视口单位,使用原有的 px 等单位。
selectorBlackList
为 ['body']
的话, 那么 .body-class
就会被忽略selectorBlackList
为 [/^body$/]
, 那么 body
会被忽略,而 .body
不会minPixelValue
(Number) 设置最小的转换数值,如果为 1 的话,只有大于 1 的值会被转换mediaQuery
(Boolean or Regexp or Array of Regexp) 媒体查询里的单位是否需要转换单位
replace
(Boolean) 是否直接更换属性值,而不添加备用属性exclude
(Array or Regexp) 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
include
(Array or Regexp) 如果设置了include
,那将只有匹配到的文件才会被转换,例如只转换 'src/mobile' 下的文件
(include: /\/src\/mobile\//
)
landscape
(Boolean) 是否添加根据 landscapeWidth
生成的媒体查询条件 @media (orientation: landscape)
landscapeUnit
(String) 横屏时使用的单位landscapeWidth
(Number) 横屏时使用的视口宽度rules
(Array) 自定义路径规则
exclude
和include
是可以一起设置的,将取两者规则的交集。
rules
option根据路径来自定义规则进行覆盖 Example:
module.exports = {
plugins: {
// ...
'@nikonee/postcss-pxtovpt': {
// ...otherOptions
rules: [
[
/\/node_modules\/vant\//, // 路径的正则或者字符串
(pixels, parsedVal, prop) => {
if (prop.includes('font')) {
return parsedval * 2 + 'vmin'
}
return parsedval * 2 + 'vw'
}
]
]
}
}
}
mediaQuery
option仅转换由 mediaQuery
参数过滤的规则
Example:
module.exports = {
plugins: {
// ...
'@nikonee/postcss-pxtovpt': {
// ...otherOptions
mediaQuery: /min\-width/, // 或者
mediaQuery: [/min\-width/, /max\-width/]
}
}
}
可以使用特殊注释来忽略单行的转换:
/* px-to-viewport-ignore-next */
— 在单行的上方,防止在下一行进行转换。/* px-to-viewport-ignore */
— 在右边的属性之后,防止在同一行转换。Example:
/* example input: */
.class {
/* px-to-viewport-ignore-next */
width: 10px;
padding: 10px;
height: 10px; /* px-to-viewport-ignore */
border: solid 2px #000; /* px-to-viewport-ignore */
}
/* example output: */
.class {
width: 10px;
padding: 3.125vw;
height: 10px;
border: solid 2px #000;
}
如果你写的像素单位不能转换, 以下配置项可能有影响:
propList
,selectorBlackList
,minPixelValue
,mediaQuery
,exclude
,include
.
在postcss.config.js
添加如下配置
module.exports = {
plugins: {
// ...
'@nikonee/postcss-pxtovpt': {
// options
}
}
}
在 gulpfile.js
添加如下配置:
var gulp = require('gulp')
var postcss = require('gulp-postcss')
var pxtoviewport = require('@nikonee/postcss-pxtovpt')
gulp.task('css', function () {
var processors = [
pxtoviewport({
viewportWidth: 320,
viewportUnit: 'vmin'
})
]
return gulp
.src(['build/css/**/*.css'])
.pipe(postcss(processors))
.pipe(gulp.dest('build/css'))
})
为了跑测试案例,您需要安装开发套件:
$ yarn install
然后输入下面的命令:
$ yarn test
本项目使用 MIT License.
FAQs
A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).
We found that @nikonee/postcss-pxtovpt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.