optimizer_使用CSS Optimizer缩小CSS

optimizer

The following is an extract from our book, CSS Master, written by Tiffany Brown. Copies are sold in stores worldwide, or you can buy it in ebook form here.

以下是蒂芙尼·布朗(Tiffany Brown)所著《 CSS Master》一书的摘录。 副本在世界各地的商店中都有出售,您也可以在这里以电子书的形式购买。

Developer tools help you find and fix rendering issues, but what about efficiency: are our file sizes as small as they can be? For that, we need minification tools.

开发人员工具可帮助您发现并解决渲染问题,但效率又如何:我们的文件大小是否尽可能小? 为此,我们需要缩小工具。

Minification in the context of CSS simply means removing excess characters. Consider, for example, this block of code:

在CSS上下文中的缩小只是意味着删除多余的字符。 例如,考虑以下代码块:

h1 {
    font: 16px / 1.5 'Helvetica Neue', arial, sans-serif;
    width: 80%;
    margin: 10px auto 0px;
}

That’s 98 bytes long, including line breaks and spaces. Let’s look at a minified example:

这是98个字节长,包括换行符和空格。 让我们看一个缩小的示例:

h1{font:16px/1.5 'Helvetica Neue',arial,sans-serif;width:80%;
↵margin:10px auto 0}

Now our CSS is only 80 bytes long and 18% reduction. Fewer bytes, of course, means faster download times and data transfer savings for you and your users.

现在我们CSS只有80字节长,减少了18%。 当然,更少的字节意味着为您和您的用户缩短下载时间并节省数据传输。

In this section, we’ll look at CSS Optimizer, or CSSO, a minification tool that runs on Node.js. To install CSSO, you’ll first have to install Node.js and NPM. NPM is installed as part of the Node.js installation process, so you’ ll only need to install one package.

在本部分中,我们将研究CSS优化器或CSSO,它是在Node.js上运行的缩小工具。 要安装CSSO,首先必须安装Node.js和NPM 。 NPM是作为Node.js安装过程的一部分安装的,因此您只需要安装一个软件包即可。

Using CSSO does require you to be comfortable using the command-line interface. Linux and OS X users can use the Terminal application (Applications > Terminal.app for OS X). If you’re using Windows, utilize the command prompt. Go to the Start or Windows menu and type cmd in the search box.

使用CSSO确实需要您习惯使用命令行界面。 Linux和OS X用户可以使用终端应用程序(对于OS X,为应用程序> Terminal.app)。 如果您使用的是Windows,请使用命令提示符。 转到“开始”或“ Windows”菜单,然后在搜索框中键入cmd

安装CSSO (Installing CSSO)

Once you have set up Node.js and NPM, you can install CSSO. At a command line prompt, type:

设置完Node.js和NPM之后,就可以安装CSSO。 在命令行提示符下,键入:

npm install -g csso The -g flag installs CSSO globally so that we can use it from the command line. OS X and Linux users may need to use sudo (sudo npm install -g csso). You’ll know it’s installed when NPM prints its installation path to the command line window, and the command line prompt reappears, as depicted in Figure 3.25.

npm install -g csso -g标志可全局安装CSSO,以便我们可以从命令行使用它。 OS X和Linux用户可能需要使用sudo( sudo npm install -g csso )。 当NPM将其安装路径打印到命令行窗口时,您将知道它已安装,并且命令行提示符再次出现,如图3.25所示。

alt

Figure 3.25. Installing CSSO using Windows’ command prompt

图3.25。 使用Windows的命令提示符安装CSSO

Now we’re ready to minify our CSS.

现在我们准备缩小CSS。

使用CSSO缩小 (Minification with CSSO)

To minify CSS files, run the csso command, passing the name of a file as an argument:

要缩小CSS文件,请运行csso命令,并将文件名作为参数传递:

csso style.css This will perform basic compression. CSSO strips unneeded whitespace, removes superfluous semicolons, and deletes comments from your CSS input file.

csso style.css这将执行基本压缩。 CSSO去除不需要的空格,删除多余的分号,并从CSS输入文件中删除注释。

Once complete, CSSO will print the optimized CSS to standard output, meaning the current terminal or command prompt window. In most cases, however, we’ll want to save that output to a file. To do that, pass the second argument to csso the name of the minified file. For example, if we wanted to save the minified version of style.css as style.min.css, we’d use the following:

完成后,CSSO将优化CSS打印到标准输出,即当前的终端或命令提示符窗口。 但是,在大多数情况下,我们希望将该输出保存到文件中。 为此,请将第二个参数传递给csso缩小文件的名称。 例如,如果我们想将style.css的缩小版本另存为style.min.css,则可以使用以下代码:

csso style.css style.min.css By default, CSSO will rearrange parts of your CSS. It will, for example, merge declaration blocks with duplicated selectors and remove some overridden properties. Consider the following CSS:

csso style.css style.min.css默认情况下,CSSO将重新排列CSS的各个部分。 例如,它将合并具有重复选择器的声明块,并删除一些覆盖的属性。 考虑以下CSS:

body { margin: 20px 30px; padding: 100px; margin-left: 0px; }

正文{边距:20px 30px; 内边距:100px; 左边距:0px; }

h1 { font: 200 36px / 1.5 sans-serif; }

h1 {字体:200 36px / 1.5 sans-serif; }

h1 { color: #ff6600; } In this snippet, margin-left overrides the earlier margin declaration. We’ve also repeated h1 as a selector for consecutive declaration blocks. After optimization and minification, we end up with this:

h1 {颜色:#ff6600; 在此代码段中, margin-left会覆盖之前的margin声明。 我们还重复了h1作为连续声明块的选择器。 经过优化和缩小之后,我们得出以下结论:

body{padding:100px;margin:20px 30px 20px 0}h1{font:200 36px/1.5
↵ sans-serif;color:#f60}

CSSO removed extraneous spaces, line breaks, and semicolons, and shortened #ff6600 to #f60. CSSO also merged the margin and margin-left properties into one declaration (margin: 20px 30px 20px 0) and combined our separate h1 selector blocks into one.

CSSO删除了多余的空格,换行符和分号,并将#ff6600缩短为#f60 。 CSSO还将marginmargin-left属性合并为一个声明( margin: 20px 30px 20px 0 ),并将我们单独的h1选择器块合并为一个。

Now, if you’re skeptical of how CSSO will rewrite your CSS, you can disable its restructuring features. Just use the --restructure-off or -off flags. For example, running csso style.css style.min.css -off gives us the following:

现在,如果您对CSSO将如何重写CSS持怀疑态度,则可以禁用其重组功能。 只需使用--restructure-off-off标志。 例如,运行csso style.css style.min.css -off可提供以下内容:

body{margin:20px 30px;padding:100px;margin-left:0}h1{font:200 36px/ ↵1.5 sans-serif}h1{color:#f60}

Now our CSS is minified, but not optimized. Disabling restructuring will keep your CSS files from being as small as they could be. Avoid disabling restructuring unless you run into problems.

现在,我们CSS已缩小,但尚未优化。 禁用重组将使CSS文件尽可能小。 除非遇到问题,否则请避免禁用重组。

Preprocessors, introduced in Chapter 9, offer minification as part of their toolset; however, using CSSO can shave additional bytes from your file sizes.

第9章介绍的预处理器将缩小功能作为其工具集的一部分。 但是,使用CSSO可以从文件大小中省掉更多字节。

翻译自: https://www.sitepoint.com/minifying-css-with-css-optimizer/

optimizer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值