首页  >  文章  >  web前端  >  postcss是什么东西?为什么要用?

postcss是什么东西?为什么要用?

藏色散人
藏色散人转载
2021-10-20 16:16:568470浏览

为什么要用postcss

随着技术的发展,目前css已经发展到了第三阶段css3.css3能够支持更多的动效,以前需要用js来实现的动画、过渡,计算等功能,如今大多都能够用css来实现,而且性能更佳。当然,随着业务的需要,在编写css过程当中,为了能够让css具备js的可复用性,灵活性、模块化开发以及更好的管理样式文件,像sass这样的css框架就应运而生。

css预处理器Sass

sass能够解决css一些缺憾,包括但不限于:

1.变量:声明一个变量,多处使用

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

2.嵌套:能够更好的明确父子层级关系,方便修改查找以及减少样式命名

.main  {

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

}
编译为
 .main  .redbox {

    background-color: #ff0000;
    color: #000000;

}

3.引用混合样式:一处定义,多处使用

编译前:

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

编译为:

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

4.函数指令:像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; }

以上4种是最为常见的,更多用法,请自行到Sass官网了解。

Css预处理器让前端开发人员大大提升了css开发速度,跟sass类拟的还有less,Stylus。

说说使用sass遇到的一些问题

1.基于Ruby,使用sass必须安装Ruby,内部是使用Ruby来编译的。

2.需要安装node-sass.目前前端都使用了gulp,webpack这些构建工具,那么如果用sass的话,webpack构建必须安装sass-loader,而sass-loader又依赖于node-sass,要知道node-sass安装速度极其慢,特别是使用window系统开发时,npm199517f3f5b0083273477e86ae26b558 tags of HTML-like files.

  • 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视频教程

    以上是postcss是什么东西?为什么要用?的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明:
    本文转载于:segmentfault.com。如有侵权,请联系admin@php.cn删除