
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
postcss-px2vp
Advanced tools
A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).
将 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 {
border: 1px solid black;
margin-bottom: 1px;
font-size: 20px; /* px-to-viewport-ignore */
/* px-to-viewport-ignore-next */
line-height: 30px;
}
@media (min-width: 750px) {
.class3 {
font-size: 16px;
line-height: 22px;
}
}
.class {
margin: -1.33333vw 0.5vh;
padding: 5vmin 1.26667vw 1px;
border: 0.4vw solid black;
border-bottom-width: 1px;
font-size: 1.86667vw;
line-height: 2.66667vw;
}
.class2 {
border: 1px solid black;
margin-bottom: 1px;
font-size: 20px;
line-height: 30px;
}
@media (min-width: 750px) {
.class3 {
font-size: 2.13333vw;
line-height: 2.93333vw;
}
}
使用 npm 安装
npm install postcss-px2vp --save-dev
或者使用 yarn 进行安装
yarn add -D postcss-px2vp
默认参数:
{
unitToConvert: 'px',
viewportWidth: 320,
unitPrecision: 5,
propList: ['*'],
viewportUnit: 'vw',
fontViewportUnit: 'vw',
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: [],
landscape: false,
landscapeUnit: 'vw',
landscapeWidth: 568
}
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) 媒体查询里的单位是否需要转换单位
replace
(Boolean) 是否直接更换属性值,而不添加备用属性
exclude
(Array or Regexp) 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
landscape
(Boolean) 是否添加根据 landscapeWidth
生成的媒体查询条件 @media (orientation: landscape)
landscapeUnit
(String) 横屏时使用的单位
landscapeWidth
(Number) 横屏时使用的视口宽度
P.S. 所有参数都可以传入一个函数,动态改变参数
示例
{
viewportWidth(rule: PostCss.Rule){
const file = rule.source?.input.file;
if (file?.includes('main')) return 750;
return 375;
}
}
在 gulpfile.js
添加如下配置:
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var pxtoviewport = require('postcss-px2vp');
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'));
});
在postcss.config.js
添加如下配置
module.exports = {
plugins: {
...
'postcss-px2vp': {
// options
}
}
}
变更日志在 这.
本项目使用 MIT License.
本项目基本逻辑都是从postcss-px-to-viewport的 clone 过来。 本项目主要做了如下工作:
FAQs
A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).
The npm package postcss-px2vp receives a total of 60 weekly downloads. As such, postcss-px2vp popularity was classified as not popular.
We found that postcss-px2vp 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 MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.