用postcss cli运行postcss
一、验证autoprefixer插件
1、新建项目
- 新建文件夹postcss;
- 在postcss目录中,新建package.json文件,新建css文件夹;
- 在css文件夹新建outfile.css,infile.css文件;
- 在outfile.css文件中写css3语法。
2、安装插件
A、安装postcss-cli插件
cnpm i -D postcss-cli
B、安装autoprefixer插件
cnpm i -D autoprefixer
3、配置package.json文件
{
"name": "postcss",
"version": "1.0.0",
"scripts": {
"postcss": "postcss -u autoprefixer -o css/outfile.css css/infile.css"
},
"devDependencies": {
"autoprefixer": "^7.1.4",
"postcss-cli": "^4.1.1"
}
}
4、执行命令
npm run postcss
5、查看文件
/* infile.css */
.app {
display: flex;
border-radius: 5px;
width: 150px;
height: 50px;
border: 1px solid #0f0;
transform: rotate(30deg);
}
/* outfile.css */
.app {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
border-radius: 5px;
width: 150px;
height: 50px;
border: 1px solid #0f0;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
}
如果想在命令行验证其他插件功能,重复上述步骤即可,下面只说插件们输入和输出。
二、postcss-import
/* imported.css */
body {
background: red;
}
/* infile.css */
@import './imported.css';
body {
color: blue;
font-size: 12px;
}
/* outfile.css */
body {
background: red;
}
body {
color: blue;
font-size: 12px;
}
三、postcss-mixins
/* infile.css */
@define-mixin amixin $num, $color: blue {
.ele-$(num) {
color: $color;
@mixin-content;
}
.ele-$(num):hover {
color: white;
background: $color;
}
}
@mixin amixin one {
background: #000;
border: 1px solid #dfdfdf;
}
@mixin amixin two, red {
background: pink;
font-size:18px;
}
/* outfile.css */
.ele-one {
color: blue;
background: #000;
border: 1px solid #dfdfdf;
}
.ele-one:hover {
color: white;
background: blue;
}
.ele-two {
color: red;
background: pink;
font-size:18px;
}
.ele-two:hover {
color: white;
background: red;
}
四、postcss-simple-vars
/* infile.css */
$dir: top;
$blue: #056ef0;
$column: 200px;
.vars-1 {
background: $blue;
width: $column;
}
.vars-2 {
width: calc(4 * $column);
margin-$(dir): 10px;
}
/* outfile.css */
.vars-1 {
background: #056ef0;
width: 200px;
}
.vars-2 {
width: calc(4 * 200px);
margin-top: 10px;
}
五、postcss-nested
/* infile.css */
.nested{
font-size: 15px;
p{
line-height: normal;
}
&_title{
font-weight:;
}
}
/* outfile.css */
.nested{
font-size: 15px;
}
.nested p{
line-height: normal;
}
.nested_title{
font-weight:;
}
六、postcss-extend
/* infile.css */
%placeholder1{
color: red;
}
.extended1{
font-size: 15px;
@extend %placeholder1;
}
@define-placeholder placeholder2{
color: blue;
}
.extended2{
font-size: 18px;
@extend placeholder2;
}
.placeholder3{
color: orange;
}
.extended3{
font-size: 25px;
@extend .placeholder3;
}
/* outfile.css */
.extended1{
color: red;
}
.extended1{
font-size: 15px;
}
.extended2{
color: blue;
}
.extended2{
font-size: 18px;
}
.placeholder3, .extended3{
color: orange;
}
.extended3{
font-size: 25px;
}
七、postcss-conditionals
/* infile.css */
@define-mixin amixin $val:5{
.condition{
background: red;
@mixin-content;
@if $val < 5{
color: black;
} @else {
color: white;
}
}
}
@mixin amixin{
font-size: 12px;
}
@mixin amixin 3{
font-size:15px;
}
/* outfile.css */
.condition{
background: red;
font-size: 12px;
color: white
}
.condition{
background: red;
font-size:15px;
color: black
}
条件语句在混合中定义的,运行的时候,记得安装混合命令和条件命令。
八、postcss-for
/* infile.css */
@for $i from 1 to 4 {
.tab-$i { width: $(i)px; }
}
@for $i from 1 to 5 by 2 {
.menu-$i { width: $(i)px; }
}
/* outfile.css */
.tab-1 { width: 1px; }
.tab-2 { width: 2px; }
.tab-3 { width: 3px; }
.tab-4 { width: 4px; }
.menu-1 { width: 1px; }
.menu-3 { width: 3px; }
.menu-5 { width: 5px; }
九、postcss-each
/* infile.css */
@each $icon in dog, tiger, lien {
.icon-$(icon) {
background: url('icons/$(icon).png');
}
}
@each $val, $i in dog, tiger {
.$(val)-img {
background: url("$(val)_$(i).png");
}
}
@each $animal, $color in (dog, lien), (black, blue) {
.$(animal)-icon {
background-image: url('/images/$(animal).png');
border: 2px solid $color;
}
}
/* outfile.css */
.icon-dog {
background: url('icons/dog.png');
}
.icon-tiger {
background: url('icons/tiger.png');
}
.icon-lien {
background: url('icons/lien.png');
}
.dog-img {
background: url("dog_0.png");
}
.tiger-img {
background: url("tiger_1.png");
}
.dog-icon {
background-image: url('/images/dog.png');
border: 2px solid black;
}
.lien-icon {
background-image: url('/images/lien.png');
border: 2px solid blue;
}
十、postcss-color-alpha
/* infile.css */
.alpha-c1 {
color: black(.1)
}
.alpha-c2 {
color: white(0.2);
}
.alpha-c3 {
color: #0fc.3;
}
.alpha-c4 {
color: #00ffcc.45;
}
.alpha-c5 {
border-color: #000 #000.5 white white(0.5);
}
.alpha-c6 {
text-shadow: 1px 1px 1px #0fc.1, 3px 3px 5px rgba(#fff, .5);
}
/* outfile.css */
.alpha-c1 {
color: rgba(0, 0, 0, 0.1)
}
.alpha-c2 {
color: rgba(255, 255, 255, 0.2);
}
.alpha-c3 {
color: rgba(0, 255, 204, 0.3);
}
.alpha-c4 {
color: rgba(0, 255, 204, 0.45);
}
.alpha-c5 {
border-color: #000 rgba(0, 0, 0, 0.5) white rgba(255, 255, 255, 0.5);
}
.alpha-c6 {
text-shadow: 1px 1px 1px rgba(0, 255, 204, 0.1), 3px 3px 5px rgba(255, 255, 255, 0.5);
}
十一、总结
本篇内容通过命令行方式介绍了一些postcss插件,来模拟sass预处理器的功能,变量,继承,嵌套,混合,引入,个人还是觉得sass在预处理器方面做得好。
遇到了一个问题,在infile.css文件中,写入变量和混合代码,运行postcss-simple-vars的相关命令时,老是报错说混合中的形参没定义,导致变量代码不能成功编译出来。然而,如果运行postcss-mixins相关命令,混合代码可以正常编译。
用postcss cli运行postcss的更多相关文章
- PostCSS深入学习: PostCSS和Sass、Stylus或LESS一起使用
如果你喜欢使用PostCSS,但又不想抛弃你最喜欢的预处理器.不用担心,你不需要作出二选一的选择,你可以把PostCSS和预处理器(Sass.Stylus或LESS)结合起来使用. 有几个PostCS ...
- 在Linux上用自己编译出来的coreclr与donet cli运行asp.net core程序
先在 github 上签出 coreclr 的源代码,运行 ./build.sh 命令进行编译,编译结果在 coreclr/bin/Product/Linux.x64.Debug/ 文件夹中. 接着签 ...
- 尝试在mac上用dotnet cli运行asp.net core示例程序
自从知道微软用dotnet cli取代dnx之后,一直在等dotnet cli支持asp.net core... 昨天看到这篇新闻(ASP.NET Core 1.0 Hello World)后,才知道 ...
- Windows登录服务器CLI运行脚本出现 syntax error: unexpected end of file 错误的解决
0.前言 通常我们在编辑 Linux 服务器上的文件时,直接在 Linux 环境比较麻烦(当然熟练使用 VIM 的程序员除外哈哈),有时我们会使用 Windows 将文件编辑好再上传到服务器端,我用的 ...
- 在 CSS 预编译器之后:PostCSS
提到css预编译器(css preprocessor),你可能想到Sass.Less以及Stylus.而本文要介绍的PostCSS,正是一个这样的工具:css预编译器可以做到的事,它同样可以做到. “ ...
- [译]PostCSS介绍
PostCSS介绍 原文链接:http://www.smashingmagazine.com/2015/12/introduction-to-postcss/ 转自:http://www.zcfy.c ...
- PostCss 从0开始
PostCss 摘自 http://ju.outofmemory.cn/entry/215105 http://www.w3cplus.com/PostCSS/postcss-deep-dive-pr ...
- [翻译]用PostCSS改善你的CSS代码质量
“代码质量”这个术语对于程序员来说并不陌生.毕竟,每个开发人员都知道,代码只是能工作是不够的.它还应该具备其他要素:它应该是可读的,良好的格式和一致性.它也应该符合一些标准的量化指标.不过这些在写CS ...
- [翻译]PostCSS简介
许多开发人员花时间在使用CSS的预处理器上如less,sass和stylus.这些工具已经成为Web开发的重要组成部分.写一个网站的样式,不使用嵌套,变量或混入等功能很少见.它们每个都是非常实用的,让 ...
随机推荐
- android okhttp的使用
OkHttpClient client = new OkHttpClient(); String url = ""; Request request = new Request.B ...
- <crtdbg.h> 的作用
1.在调试状态下让win程在输出窗口中显示调试信息,可以用_RPTn 宏n为显示参数比如_RPT0(_CRT_WARN,"text"); _RPT1(_CRT_WARN," ...
- c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage
vs 2005 ,vs 2008, vs 2010,安装后有时出现这个错误(我的机器装的x64的win7),很烦人.找了很多地方都不能解决.其实说起来还是开发国家牛,轻易就解决了这个问题.其实出现这个 ...
- ftruncate(改变文件大小)
ftruncate(改变文件大小) 定义函数 int ftruncate(int fd,off_t length); 函数说明 ftruncate()会将参数fd指定的文件大小改为参数length指定 ...
- kafka系列十、kafka常用管理命令
一.Topic管理 1.创建topic kafka-topics.sh --zookeeper 47.52.199.52:2181 --create --topic test-15 --replica ...
- 高级 Java 必须突破的 10 个知识点!
1.Java基础技术体系.JVM内存分配.垃圾回收.类装载机制.性能优化.反射机制.多线程.网络编程.常用数据结构和相关算法. 2.对面向对象的软件开发思想有清晰的认识.熟悉掌握常用的设计模式. 3. ...
- pl sql 记住用户名密码
tools--Preferences--Logon History 选择 “Store history”是默认勾选的,勾上“Store with password” 登录时从下拉框选择用户名则自动登 ...
- win7设置固定IP
正文: 你必须知道你的路由器网关,一般是192.168.1.1(或192.168.0.1) 按传统的来:开始——控制面板——网络和共享中心——更改适配器设置.一般来讲,这里应该有两个图标,一个是有线网 ...
- QQ空间说说如何批量删除
事件背景: 今天突发奇想,想把自己之前发的说说都删除了,结果就有了下面的代码 1.按F12 2.点击 Console,进入Console项 3.使用以下代码: var delay = 1000; fu ...
- pytorch实现花朵数据集读取
import os from PIL import Image from torch.utils import data import numpy as np from torchvision imp ...