✨ A CSS post-processor that converts px to rem.

Overview

postcss-pxtorem

English docs: [README-en.md]

PostCSS插件,可以从像素单位生成rem单位

新功能

  • 在样式文件中设置任意 postcss-pxtorem 支持的选项
  • 在样式文件中忽略某一行

安装

pnpm install postcss @minko-fe/postcss-pxtorem -D

用法

像素是最容易使用的单位。它们的唯一问题是,它们不能让浏览器改变默认的16号字体大小。postcss-pxtorem将每一个px值转换为你所选择的属性中的rem,以便让浏览器设置字体大小。

postcss.config.js

example

module.exports = {
  plugins: [
    require('@minko-fe/postcss-pxtorem')({
      rootValue: 16,
      selectorBlackList: ['some-class'],
      propList: ['*'],
      atRules: ['media'],
      // ...
    }),
  ],
}

options

Name Type Default Description
rootValue number | ((input: Input) => number) 16 代表根元素的字体大小或根据 input 参数返回根元素的字体大小
unitPrecision number 5 小数点后精度
propList string[] ['*'] 可以从px改变为rem的属性,参考:propList
selectorBlackList (string | RegExp)[] [] 忽略的选择器,保留为px。参考:selectorBlackList
replace boolean true 直接在css规则上替换值而不是添加备用
atRules boolean | string[] false 允许at-rules中转换rem。参考 At-rule
minPixelValue number 0 最小的px转化值(小于这个值的不转化)
exclude string | RegExp | ((filePath: string) => boolean) | null null 忽略的文件路径。参考:exclude
include string | RegExp | ((filePath: string) => boolean) | null null 包括的文件路径,与 exclude 相反,优先级高于 exclude。规则同 exclude
disable boolean false 关闭插件

propList

  • 值需要完全匹配
  • 使用通配符 * 来启用所有属性. Example: ['*']
  • 在一个词的开头或结尾使用 *. (['*position*'] will match background-position-y)
  • 使用 ! 不匹配一个属性. Example: ['*', '!letter-spacing']
  • 组合 !*. Example: ['*', '!font*']

selectorBlackList

  • 如果值是字符串,它会检查选择器是否包含字符串.
    • ['body'] will match .body-class
  • 如果值是正则,它会检查选择器是否与正则相匹配.
    • [/^body$/] will match body but not .body

exclude

  • 如果值是字符串,它检查文件路径是否包含字符串
    • 'exclude' will match \project\postcss-pxtorem\exclude\path
  • 如果值是正则,它将检查文件路径是否与正则相匹配
    • /exclude/i will match \project\postcss-pxtorem\exclude\path
  • 如果值是函数,你可以使用排除函数返回true,文件将被忽略
    • 回调将传递文件路径作为一个参数,它应该返回一个boolean
    • function (file) { return file.includes('exclude') }

关于新特性

⚙️ 在css中,动态设置插件选项

当前文件禁用插件

/* pxtorem?disabled=true */
.rule {
  font-size: 15px; // 15px
}

设置rootValue

/* pxtorem?rootValue=32 */
.rule {
  font-size: 30px; // 0.9375rem
}

🌰 以上只是简单的栗子,你可以在css文件中设置任意 postcss-pxtorem 支持的选项

聪明的你,或许已经看出来了,/* pxtorem?disabled=true */ 很像浏览器url? 😼 没错。关于规范,只需参考:query-string

例子

/* postcss-pxtorem?disable=false&rootValue=32&propList[]=*&replace=false&selectorBlackList[]=/some-class/i */

在css中,忽略某一行

.rule {
  /* pxtorem-disable-next-line */
  font-size: 15px; // 15px
}

如果这个仓库帮了你的忙,请不吝给个star,谢谢! 😎

❤️ 感谢

postcss-pxtorem

@tcstory/postcss-px-to-viewport

👀 相关

A CSS post-processor that converts px to viewport: postcss-pxtoviewport

You might also like...

Data-tip.css - Wow, such tooltip, with pure css!

Notice: hint.css has been much better since I complained about it months ago, so try out its new features instead of this one! data-tip.css Wow, such

May 26, 2021

Tiny CSS framework with almost no classes and some pure CSS effects

Tiny CSS framework with almost no classes and some pure CSS effects

no.css INTERACTIVE DEMO I am tired of adding classes to style my HTML. I just want to include a .css file and I expect it to style the HTML for me. no

Dec 10, 2022

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

Bootstrap Sleek, intuitive, and powerful front-end framework for faster and easier web development. Explore Bootstrap docs » Report bug · Request feat

Jan 1, 2023

Modern CSS framework based on Flexbox

Modern CSS framework based on Flexbox

Bulma Bulma is a modern CSS framework based on Flexbox. Quick install Bulma is constantly in development! Try it out now: NPM npm install bulma or Yar

Dec 31, 2022

A utility-first CSS framework for rapid UI development.

A utility-first CSS framework for rapidly building custom user interfaces. Documentation For full documentation, visit tailwindcss.com. Community For

Dec 30, 2022

Materialize, a CSS Framework based on Material Design

MaterializeCSS Materialize, a CSS Framework based on material design. -- Browse the docs -- Table of Contents Quickstart Documentation Supported Brows

Jan 2, 2023

Material Design Components in HTML/CSS/JS

Material Design Lite An implementation of Material Design components in vanilla CSS, JS, and HTML. Material Design Lite (MDL) lets you add a Material

Jan 4, 2023

A set of small, responsive CSS modules that you can use in every web project.

A set of small, responsive CSS modules that you can use in every web project.

Pure A set of small, responsive CSS modules that you can use in every web project. http://purecss.io/ This project is looking for maintainers to suppo

Jan 3, 2023

Functional css for humans

TACHYONS Functional CSS for humans. Quickly build and design new UI without writing CSS. Principles Everything should be 100% responsive Everything sh

Jan 4, 2023
Comments
  • `atQuery` option is renamed `mediaQuery` in v0.5.0

    `atQuery` option is renamed `mediaQuery` in v0.5.0

    In 16b66b0, the mediaQuery option has been changed to atQuery, but it isn’t documented, so I guess a choice should be made and documented.

    I propose to go for atRule, because it’s the official CSS terminology (and it’s also used by PostCSS).

    Also, I’d consider accepting an array (in addition to boolean) for this one, in case someone wants to convert @media and @container but leave @supports untouched for some reasons.

    What do you think?

    opened by meduzen 2
  • Changelog not updated

    Changelog not updated

    Hi! As I’m watching the repository, I noticed the releases always refer to the changelog for the changes, but the changelog is not up-to-date.

    (I don’t know if this repo uses an automatic process for this or not, sorry if this issue feels off for some reason.)

    Thanks again for your work on this package, it’s a very important CSS area. 🙏

    opened by meduzen 1
  • Use with Vant

    Use with Vant

    vant适配文档

    // postcss.config.js
    module.exports = {
      plugins: [
        require("@minko-fe/postcss-pxtorem")({
          rootValue({ file }) {
            return file.includes('node_modules/vant') ? 37.5 : 75;
          },
        }),
      ],
    };
    
    opened by hemengke1997 0
Releases(v1.1.0)
Owner
null
Gerçekten her yeni özellik için npm'de hazır bir modül aramayın, gerek yok, lütfen. DiscordJS ile çok temel bir POST request yapabilirsiniz.

client.api.channels("847172505260261449").messages.post({ data: {"content":"Aşağıdaki menüden kendinize oyun seçebilirsiniz. Bir oyunun rolünü almak i

Toasty 23 Nov 29, 2022
Reseter.css - A Futuristic CSS Reset / CSS Normalizer

Reseter.css A CSS Reset/Normalizer Reseter.css is an awesome CSS reset for a website. It is a great tool for any web designer. Reseter.css resets all

Krish Dev DB 1.1k Jan 2, 2023
Spectre.css - A Lightweight, Responsive and Modern CSS Framework

Spectre.css Spectre.css is a lightweight, responsive and modern CSS framework. Lightweight (~10KB gzipped) starting point for your projects Flexbox-ba

Yan Zhu 11.1k Jan 8, 2023
Low-level CSS Toolkit – the original Functional/Utility/Atomic CSS library

Basscss Low-level CSS toolkit – the original Functional CSS library https://basscss.com Lightning-Fast Modular CSS with No Side Effects Basscss is a l

Basscss 5.8k Dec 31, 2022
Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation

Aphrodite Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation. Support for colocating y

Khan Academy 5.3k Jan 1, 2023
CSS Boilerplate / Starter Kit: Collection of best-practice CSS selectors

Natural Selection Natural Selection is a CSS framework without any styling at all. It is just a collection of selectors that can be used to define glo

FrontAid CMS 104 Dec 8, 2022
Source code for Chrome/Edge/Firefox/Opera extension Magic CSS (Live editor for CSS, Less & Sass)

Live editor for CSS, Less & Sass (Magic CSS) Extension Live editor for CSS, Less & Sass (Magic CSS) for Google Chrome, Microsoft Edge, Mozilla Firefox

null 210 Dec 13, 2022
Easily create css variables without the need for a css file!

Tailwind CSS Variables This plugin allows you to configure CSS variables in the tailwind.config.js Similar to the tailwindcss configurations you are u

Mert Aşan 111 Dec 22, 2022
Cooltipz.css - A highly customisable, minimal, pure CSS tooltip library

Cooltipz.css - Cool tooltips Cool customisable tooltips made from pure CSS Lightweight • Accessible • Customisable • Simple Cooltipz.css is a pure CSS

Jack Domleo 110 Dec 24, 2022
micro-library for CSS Flexbox and CSS Grid

SpeedGrid micro-library for CSS Flexbox and CSS Grid Overview SpeedGrid dynamically generates inline CSS by specifying the class name. Easy maintenanc

Toshihide Miyake 7 Mar 26, 2022