顾轶灵 @ EFE
「还原设计意图」
「设计稿发给你了,看一下大概需要几天?」
↑ 真实的故事。
还原设计 ~ 曲线拟合
Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g., fonts, colors, spacing) to Web documents.
不是传统意义上的「编程语言」
「但是 Web 发展到现在,与当初已经有了很大的变化。」
Too simple!
.post {
background-color: white;
color: rebeccapurple;
}
.post h1, .post h2, .post h3,
.post h4, .post h5, .post h6 {
/* reversed colors */
background-color: rebeccapurple;
color: white;
}
DRY? No.
button {
font-family: inherit;
}
非理想的样式继承机制
选择器组
.title,
.comment {
color: #333;
}
在文档中添加用来复用样式的内容
<h1 class="post-title block">...<h1>
...
<article class="post block">...<article>
...
<article class="comment block">...<article>
clearfix, cf
float-right
, fr
, pull-right
, ...red
, mr10
, ......
没有!
On the other hand, CSS stops short of even more powerful features that programmers use in their programming languages: macros, variables, symbolic constants, conditionals, expressions over variables, etc. That is because these things give power-users a lot of rope, but less experienced users will unwittingly hang themselves; or, more likely, be so scared that they won't even touch CSS. It's a balance. And for CSS the balance is different than for some other things.
Adding any form of macros or additional scopes and indirections, including symbolic constants, is not just redundant, but changes CSS in ways that make it unsuitable for its intended audience. Given that there is currently no alternative to CSS, these things must not be added.
但是……
@media
, @supports
--*
, var()
@custom-selector
, @custom-property
, ...Document statuses: WD - LC - CR - PR - REC
可似乎有点遥远……
提供了在纯样式层提高可维护性的机制
<h1>
推荐样式)47KB
? Are you serious?
See compiled code.
DSL → 生产环境 CSS
如果不同项目需要支持不同浏览器?
↓
适合处理兼容性问题、进行 polyfill、优化输出
广义上「后处理」也属于预处理的范畴,其实 CSS 压缩等已经是其中之一
标准 CSS (甚至草案) → 生产环境 CSS
解决兼容性问题不是样式本身的复用
DSL → 标准 CSS → 生产环境 CSS
用来增强 DSL 的表达能力受限的不足
用来增强原生 CSS 抽象能力的不足、弥补语法缺陷
流量 === ¥
rebeccapurple
→ #639
, ...0px
→ 0
0.5
→ .5
from
→ 0%
100%
→ to
bold
→ 700
, normal
→ 400
1000ms
→ 1s
(需要解析 CSS 语法)
@keyframes
, 重复属性, ...@media
提高冗余
冗余 → 有序 → 低信息量
.post a {
color: #6a0;
text-decoration: underline;
padding: 3px;
background-color: rgba(255, 255, 255, .1);
border-radius: 3px;
}
.comment {
margin: 5px 0;
padding: 3px;
background-color: rgba(255, 255, 255, .1);
text-decoration: underline;
color: #cc9900;
border-radius: 3px;
}
164 Bytes zipped
.post a {
padding: 3px;
border-radius: 3px;
background-color: rgba(255, 255, 255, .1);
color: #6a0;
text-decoration: underline;
}
.comment {
margin: 5px 0;
padding: 3px;
border-radius: 3px;
background-color: rgba(255, 255, 255, .1);
color: #cc9900;
text-decoration: underline;
}
158 Bytes zipped
访问 DSL 的 AST
只传输会被使用的 CSS 代码
datauri
)<link>
→ <style>
(?)谨慎使用内嵌(可能降低缓存效果)
<link>
> @import
*
多年前的建议?
CSS selector matching is now reasonably fast for the absolute majority of common selectors that used to be slow at the time of the profiler implementation. This time is also included into the Timeline "Recalculate Style" event.
As such, I believe the CSS selector profiler is not as useful as it [might have been] used to and can safely be dropped. This will also reduce the fraction of developers trying to micro-optimize already fast selectors.
Chrome 30 起去掉了 Selector Profiler
translateZ()
, translate3d()
translate()
, scale()
, rotate()
, opacity
will-change
Layout, paint, composite