Table of Contents
css preprocessor Sass
Let’s talk about some problems encountered when using sass
postcss new revolution
postcss configuration in webpack
postcss插件参考
Home Web Front-end CSS Tutorial What is postcss? Why use it?

What is postcss? Why use it?

Oct 20, 2021 pm 04:16 PM
css cssframework

Why use postcss

With the development of technology, css has now developed to the third stage css3.css3 can support more dynamic effects. In the past, you needed to use Most of the animation, transition, calculation and other functions implemented with js can now be implemented with css, and the performance is better. Of course, with the needs of business, in the process of writing css, in order to make css have the reusability, flexibility, modular development and better management of style files of js, css frameworks like sass came into being. .

css preprocessor Sass

sass can solve some shortcomings of css, including but not limited to:

1. Variable: declare a variable, multiple Use

$content: "Non-null content";
.main {
 content: $content;
}
编译为
.main {
 content: "Non-null content”;
}
Copy after login

2. Nesting: It can better clarify the parent-child hierarchical relationship, facilitate modification and search, and reduce style naming

.main {

  .redbox {
    background-color: #ff0000;
    color: #000000;
  }
Copy after login

}
Compile to
.main .redbox {

    background-color: #ff0000;
    color: #000000;
Copy after login

}

3. Reference mixed style: defined in one place, used in multiple places

Before compilation:

@mixin clearfix {
 display: inline-block;
 &:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
}
.box{
@include clearfix
}
Copy after login

Compile to:

.box{
display: inline-block;
}
.box:after{
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
Copy after login

4. Function instructions: start programming like js

$grid-width: 40px;
$gutter-width: 10px;
@function grid-width($n) {
 @return $n * $grid-width + ($n - 1) * $gutter-width;
}
.sidebar { width: grid-width(5); }
编译为
.sidebar { width: 240px; }
Copy after login

The above 4 types are the most common. For more usage, please go to the Sass official website to learn more.

Css preprocessor allows front-end developers to greatly improve the speed of CSS development. Similar to sass, there are less and Stylus.

Let’s talk about some problems encountered when using sass

1. Based on Ruby, Ruby must be installed to use sass, and Ruby is used internally to compile.

2. You need to install node-sass. Currently, the front-end uses construction tools such as gulp and webpack. If you use sass, the webpack build must install sass-loader, and sass-loader depends on node-sass. , you must know that the installation speed of node-sass is extremely slow, especially when developing using the window system, when npm <5.4, the installation of node-sass often fails.

3. Contamination of global variables. In the process of multi-person development, when defining a selector, you need to take into account whether the same name is used in other places.

4. Static compilation: pre-compiled, the displayed page is the compiled css.

4. Future css is not supported. Currently in the CSS3 stage, the future development direction of CSS is worth looking forward to. In the future, CSS will support more attributes and functions, including variables, nesting, value calculations, etc.

postcss new revolution

postcss definition:

PostCSS is a tool for transforming CSS with JS plugins. These plugins can support variables and variables mixins, transpile future CSS syntax, inline images, and more.

Advantages of postcss:

1. Support future css: Use cssnext to write future css css(postcss-cssnext plugin)

:root {
 --heading-color: #ff0000;
}
/ custom selectors /
@custom-selector :--headings h1, h2, h3, h4, h5, h6;
/ usage /
:--headings {
 color: var(--heading-color);
}
Copy after login

Through cssnext, the above code will be processed into the following content

h1,
h2,
h3,
h4,
h5,
h6 {
 color: #ff0000;
}
Copy after login

2. The compilation speed is greatly improved. PostCSS claims to be 3-30x faster than preprocessors.
3. Rich plug-in system, free your hands.
4.css is modular and limits the scope to components, avoiding the problem of global scope. You no longer have to worry about duplicate style names.

Postcss is a css post-processor that dynamically compiles css. That is, compilation occurs at runtime.
Postcss itself will not do anything to your css. You can think of postcss as a shell. Along with the postcss ecology, more postcss plug-ins are derived, which can help you solve more than 95% of css problems. If you need Customize a css writing specification that suits your own business needs, then you can also develop a specific postcss plugin for this purpose.

postcss configuration in webpack

npm Install postcss-loader, postcss-cssnext: npm install postcss-loader postcss-cssnext -D

webpack.config.js

What is postcss? Why use it?

postcss插件参考

  • postcss-modules and react-css-modules automatically isolate selectors within components.
  • postcss-autoreset is an alternative to using a global reset that is better for isolatable components.
  • postcss-initial adds all: initial support, which resets all inherited styles.
  • autoprefixer adds vendor prefixes, using data from Can I Use.
  • postcss-preset-env allows you to use future CSS features today.
  • precss contains plugins for Sass-like features, like variables, nesting, and mixins.
  • postcss-assets inserts image dimensions and inlines files.
  • postcss-sprites generates image sprites.
  • postcss-inline-svg allows you to inline SVG and customize its styles.
  • postcss-write-svg allows you to write simple SVG directly in your CSS.
  • postcss-syntax switch syntax automatically by file extensions.
  • postcss-html parsing styles in
  • postcss-markdown parsing styles in code blocks of Markdown files.
  • postcss-jsx parsing CSS in template / object literals of source files.
  • postcss-styled parsing CSS in template literals of source files.
  • postcss-scss allows you to work with SCSS (but does not compile SCSS to CSS).
  • postcss-sass allows you to work with Sass (but does not compile Sass to CSS).
  • postcss-less allows you to work with Less (but does not compile LESS to CSS).
  • postcss-less-engine allows you to work with Less (and DOES compile LESS to CSS using true Less.js evaluation).
  • postcss-js allows you to write styles in JS or transform React Inline Styles, Radium or JSS.
  • postcss-safe-parser finds and fixes CSS syntax errors.
  • postcss-will-change this plugin uses backface-visibility to force the browser to create a new layer, without overriding existing backface-visibility properties.

推荐学习:《css视频教程

The above is the detailed content of What is postcss? Why use it?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles