Home > headlines > 21 Ways to Optimize CSS and Speed ​​Up Your Website

21 Ways to Optimize CSS and Speed ​​Up Your Website

angryTom
Release: 2019-10-21 10:04:49
forward
4341 people have browsed it

CSS must go through a relatively complex pipeline, just like HTML and JavaScript, the browser must download the file from the server, then parse it and apply it to the DOM. Due to the high degree of optimization, this process is usually very fast - for small web projects that are not based on frameworks, CSS usually accounts for only a small part of the total resource consumption.

21 Ways to Optimize CSS and Speed ​​Up Your Website

The framework breaks this balance. Include a JavaScript GUI stack like jQuery UI and watch the CSS, JS and HTML sizes gradually increase. Typically, developers get stressed last, when they're behind a powerful 8-core workstation, using T3 internet, and no one cares about speed, which changes with the advent of latency or cpu-bound devices.

php Chinese website new course announcement:
"PHP Development of Super Large CMS Website Management System Online Live Class" is now open for registration! (Class starts on October 28th)
For details, see: https://www.php.cn/k.html

Optimizing CSS requires a multi-dimensional approach. While hand-written code can be simplified using various techniques, manually inspecting framework code is inefficient. In these cases, using automated simplification will produce better results.

The following steps will take us into the world of CSS optimization. Not every one of them will be directly applicable to your project, but be sure to keep them in mind.

01. Use abbreviations

21 Ways to Optimize CSS and Speed ​​Up Your Website

Using abbreviation statements, such as the margin statement shown below, can fundamentally reduce CSS The size of the file. A google search for CSS Shorthand will reveal many other shorthand forms.

p { margin-top: 1px;
    margin-right: 2px;
    margin-bottom:  3px;
    margin-left: 4px; }
p { margin: 1px 2px 3px 4px; }
Copy after login

02. Find and delete unused CSS

21 Ways to Optimize CSS and Speed ​​Up Your Website

#Removing unnecessary parts of CSS will obviously speed up the loading speed of the web page. Google's Chrome browser has this functionality out of the box. Just go to View > Developer > Developer Tools and open the Sources tab in Recent Builds and then open the command menu. Then, select Show Coverage to highlight unused code on the current page in the Coverage analysis window, which is an eye-opener for you.

Open the Google Chrome development tools, select Coverage next to Conlse, you can see the unused CSS, click on the corresponding item and highlight it Eye-opening display of unused code on the current page:

21 Ways to Optimize CSS and Speed ​​Up Your Website

03. Do it the easier way

21 Ways to Optimize CSS and Speed ​​Up Your Website

Navigating in line-by-line analysis is not necessarily convenient. Using Google Chrome’s Audits can quickly help us analyze and use it. Open the developer tools, click the Audits field, and click Run audits Then start analyzing the results.

04. 注意这些问题

加载外部样式表需要花费时间,这是由于延迟造成的——因此,可以把最关键的代码位放在 head 中。但是,请确保不要做得过火,记住,执行维护任务的人员也必须读取代码。

<html>
  <head>
    <style>
      .blue{color:blue;}
    </style>
    </head>
  <body>
    <p class="blue">
      Hello, world!
    </p>
Copy after login

06.允许反并行解析

@import 将 CSS 样式方便添加代码中。遗憾的是,这些好处并不是没有代价的:由于 @import 可以嵌套,因此无法并行解析它们。更并行的方法是使用一系列 标记,浏览器可以立即获取这些标记。

@import url("a.css");
@import url("b.css");
@import url("c.css");
<link rel="stylesheet" href="a.css">
<link rel="stylesheet" href="b.css">
<link rel="stylesheet" href="c.css">
Copy after login

07. 用 CSS 替换图片

几年前,一套半透明的 png 在网站上创建半透明效果是司空见惯的。现在,CSS过 滤器提供了一种节省资源的替代方法。例如,以下这个代码片段可以确保所讨论的图片显示为其自身的灰度版本。

img {
    -webkit-filter: grayscale(100%); 
    /* old safari */
    filter: grayscale(100%);
}
Copy after login

08.使用颜色快捷方式

常识告诉我们,六位数的颜色描述符是表达颜色最有效的方式。事实并非如此——在某些情况下,速记描述或颜色名称可以更短。

target { background-color: #ffffff; }
target { background: #fff; }
Copy after login

09. 删除不必要的零和单位

CSS 支持多种单位和数字格式。它们是一个值得感谢的优化目标——可以删除尾随和跟随的零,如下面的代码片段所示。此外,请记住,零始终是零,添加维度不会为包含的信息附带价值。

padding: 0.2em;
margin: 20.0em;
avalue: 0px;
padding: .2em;
margin: 20em;
avalue: 0;
Copy after login

10. 消除过多分号

这种优化需要谨慎,因为它会影响代码的更改。CSS的规范允许省略属性组中的最后一个分号。由于这种优化方法所节省的成本很小,所以我们主要针对那些正在开发自动优化的程序员说明这一点。

p {. . .    font-size: 1.33em}
Copy after login

11.使用纹理图集

由于协议开销的原因,加载多个小图片的效率很低。CSS 精灵将一系列小图片组合成一个大的PNG 文件,然后通过 CSS 规则将其分解。TexturePacker (https://www.codeandweb.com/texturepacker)等程序大大简化了创建过程。

.download {
  width:80px; 
  height:31px; 
  background-position: -160px -160px
}
.download:hover {
  width:80px; 
  height:32px; 
  background-position: -80px -160px
}
Copy after login

12. 省略 px

提高性能的一个简单方法是使用CSS标准的一个特性。为 0 的数值默认单位是 px—— 删除 px 可以为每个数字节省两个字节。

h2 {padding:0px; margin:0px;}
h2 {padding:0; margin:0}
Copy after login

13. 避免需要性能要求的属性

分析表明,一些标签比其他标签更昂贵。以下这些解析会影响性能—如果在没有必要的情况,尽量不要使用它们。

border-radius
box-shadow
transform
filter
:nth-child
position: fixed;
Copy after login

14. 删除空格

空格——考虑制表符、回车符和空格——使代码更容易阅读,但从解析器的角度看,它没有什么用处。在发布前删除它们,更好的方法是将此任务委托给 shell 脚本或类似的工具。

15. 删除注释

注释对编译器也没有任何作用。创建一个自定义解析器,以便在发布之前删除它们。这不仅节省了带宽,而且还确保攻击者和克隆者更难理解手头代码背后的思想。

16. 使用自动压缩

Yahoo 的用户体验团队创建了一个处理许多压缩任务的应用程序。它以 JAR 文件的形式发布,可在此处获得(http://yui.github.io/yuicompressor/),并且可以使用所选的JVM运行。

java -jar yuicompressor-x.y.z.jar
Usage: java -jar yuicompressor-x.y.z.jar
 [options] [input file]
Global Options
    -h, --help                Displays this
 information
    --type <js|css>           Specifies the
 type of the input file
Copy after login

17. 在 NPM 运行它

如果你希望将产品集成到 Node.JS 中,请访问 npmjs.com/package/yuicompressor。维护不良的存储库包含一组包装器文件和JavaScript API。

var compressor = require(&#39;yuicompressor&#39;);
 compressor.compress(&#39;/path/to/
file or String of JS&#39;, {
    //Compressor Options:
    charset: &#39;utf8&#39;,
    type: &#39;js&#39;,
Copy after login

18. 保持 Sass 的检查

21 Ways to Optimize CSS and Speed ​​Up Your Website

虽然 CSS 选择器的性能不像几年前那么重要(请参阅参考资料),但是像 Sass 这样的框架有时会产生非常复杂的代,不时查看输出文件,并考虑优化结果的方法。

什么是Sass?(https://www.creativebloq.com/web-design/what-is-sass-111517618)

19. 设置缓存

有句老话说,最快的文件永远不会通过网络发送。让浏览器缓存请求有效地实现这一点。遗憾的是,缓存头的设置必须在服务器上进行。充分上面讲的的两个 Chrome 工具,它们提供了一种快速分析更改结果的方法。

20. 打破缓存

设计人员通常不喜欢缓存,因为他们担心浏览器会缓存上次的样式表。解决这个问题的一个简单方法是包含带有文件名的标记。遗憾的是,由于一些代理拒绝缓存具有“动态”路径的文件,此步骤所附带的代码中概述的方案并不适用于所有地方。

<Link rel="stylesheet" href="style.css?v=1.2.3">
Copy after login

21. 不要忘记基础知识

优化CSS只是游戏的一部分。如果你的服务器不使用 HTTP/2 和 gzip 压缩,那么在数据传输期间会损失很多时间。幸运的是,解决这两个问题通常很简单。我们的示例显示了对常用Apache 服务器的一些调整。如果您发现自己在一个不同的系统上,只需参考服务器文档即可。

pico /etc/httpd/conf/httpd.conf
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
Copy after login

英文原文地址:https://www.creativebloq.com/how-to/21-ways-to-optimise-your-css-and-speed-up-your-site

为了保证的可读性,本文采用意译而非直译。

相关推荐:
Web 性能优化:图片优化让网站大小减少 62%

Related labels:
source:www.html.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template