CSS系列 - 性能

116 阅读3分钟

加载

  • css 压缩:将写好的 css 进行打包压缩,可以减少很多的体积
  • css 使用单一属性
  • 减少使用 @import

渲染

  • 慎重使用高性能属性:浮动、定位
  • 尽量减少页面重排、重绘

重排

  • 位置:positon、top、left、z-index、float、dispay
  • 大小:width、height、margin、padding
  • 文字系列: font、line-height、color、letter-spacing
  • 背景边框:background、 border
  • 其它:anmation、transition

重绘

  • border、outline、background、box-shadow
  • 去除空规则:为了预留样式减少 css 文档体积
  • 属性值为 0 时,不加单位
  • 属性值为浮动小数,可以省略小数点之前的 0
  • 标准化各种浏览器前缀:带浏览器前缀的在前。标准属性在后
  • 选择器优化嵌套,尽量避免层级过深
  • css 雪碧图
  • 正确使用 display 的属性
  • 不滥用 web 字体
  • 将具有相同属性的样式抽离出来
  • 样式与内容分离
  • text-shadow 不要放在动画里面
  • 字体抗锯齿
    -webkit-font-smoothing: antialiased;
    
  • CSS 动画怎么实现的
  • 合理利用 GPU 加速
  • Flexible Box Model
  • 有没有测试换个布局对渲染影响
  • CSS render performance
  • CSS animation performance

查找

  • 关键选择器(key selector)当使用后代选择器的时候,浏览器会遍历所有子元素来确定是否是指定的元素
  • 如果规则拥有 ID 选择器作为其关键选择器,则不要使用其他选择
  • 避免使用通配规则
  • 尽量少的去对标签进行选择
  • 尽量少的去使用后代选择器,更多的使用类来关联每一个标签元素
  • 了解哪些属性是可以通过继承而来的,然后避免对这些属性重复指定规则

规范

  • 命名合理
  • 结构层次设计是否足够健壮
  • 对样式进行抽象复用
  • OOCSS(Object-Oriented CSS)
    • 原则
      • 独立的结构和样式
      • 独立的容器和内容
    • 关键部分
      • 创建一个组件库

        找出所有可重用的组件,并给这个组件创建 HTML 和 CSS 样式规

      • 独立的容器和内容,并且避免样式来依赖位置

        避免样式依赖位置,内容可以插入到任何容器

      • 独立的结构和样式

        创建基础的结构 HTML 和创建基础的类名

      • 使用类名为扩展基本对象

      • 坚持以语义类来命名类名

    • 优缺点
      • 优点
        • 减少 CSS 代码
        • 具有清洁的 HTML 标记,有语义的类名,逻辑性强的层次关系
        • 语义标记,有助于 SEO
        • 更好的页面优化,更快的加载时间(因为有很多组件重用)
        • 可扩展的标记和 CSS 样式,有更多的组件可以放到库中,而不影响其他的组件
        • 能轻松构造新的页面布局,或制作新的页面风格
      • 缺点
        • OOCSS 适合真正的大型网站开发
        • 巧妙使用
        • 最好给每一个组件备写一份说明文档
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>box-shadow</title>
    <style>
      /* The slow way */
      .make-it-slow {
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
        transition: box-shadow 0.3s ease-in-out;
      }

      /* Transition to a bigger shadow on hover */
      .make-it-slow:hover {
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
      }

      /* The fast way */
      .make-it-fast {
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
      }

      /* Pre-render the bigger shadow, but hide it */
      .make-it-fast::after {
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        opacity: 0;
        transition: opacity 0.3s ease-in-out;
      }

      /* Transition to showing the bigger shadow on hover */
      .make-it-fast:hover::after {
        opacity: 1;
      }

      body {
        background-color: #f4f4f4;
      }
      .box {
        position: relative;
        display: inline-block;
        width: 100px;
        height: 100px;
        background-color: #fff;
        border-radius: 5px;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);
      }

      .box::after {
        content: "";
        border-radius: 5px;
        position: absolute;
        z-index: -1;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        opacity: 0;
        transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);
      }

      .box:hover {
        transform: scale(1.25, 1.25);
      }

      .box:hover::after {
        opacity: 1;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
  </body>
</html>