表格布局
利用浏览器自身优化
引起回流、重绘的属性操作会放入队列,达到一定数量或时间,再一次渲染
- 用变量缓存元素的属性值
- 要设置的属性值减少依赖其它属性值
- 避免频繁读取计算属性值
手动一次渲染
强制使用style.cssText或setAttribute('style', 样式)将所有设置的属性,一次写入内联样式
优化 DOM 树
- 使用文档碎片或display:none隐藏节点,缓存要插入的节点,之后将缓存结果一次性插入 DOM 树并显示
- 使用replaceChild cloneNode减少先删除、创建再插入 DOM 的场景
设计
何写小于 10 像素的字
Chrome
浏览器在 chrome://settings/fonts 中允许用户设置最小字号
最小字号默认值为12px``font-size小于最小字号,会被强制设为最小字号
可以通过-webkit-transform来缩小字号,并且只对webkit内核浏览器生效
<style>
font-size : 10px;
-webkit-transform : scale(0.8333);
</style>
实现小于 1px 像素的边
随时显示屏技术的发展,很多移动设备的分辨率媲美甚至超过大屏设备
在 CSS 中设置的像素宽度1px,在高分辨率的小屏设备上,可能会变粗
现代webkit内核浏览器提供私有属性-webkit-min-device-pixel-ratio或-webkit-max-device-pixel-ratio
用来当前设备的物理像素分辨率与CSS像素分辨率比值的最小值和最大值
- 通过媒体查询,将边框宽度设为1px / devicePixelRatio
.border { border: 1px solid }
@media screen and (-webkit-min-device-pixel-ratio: 2) {
.border { border: 0.5px solid }
}
@media screen and (-webkit-min-device-pixel-ratio: 3) {
.border { border: 0.333333px solid }
}
- 伪类 +resolution+transform
resolution是标准属性,可以实现与上面的私有属性相同的设置
transform缩小伪类的高度为1 / devicePixelRatio
<style>
div::after {
content: '';
display: block;
border-bottom: 1px solid;
}
@media only screen and (min-resolution: 2dppx) {
div::after {
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
}
}
@media only screen and (min-resolution: 3dppx) {
div::after {
-webkit-transform: scaleY(0.33);
transform: scaleY(0.33);
}
}
</style>
<div></div>
对比多方法实现图标
实现方式 |
特殊符号 |
PNG / GIF |
Sprites |
Icon Font |
SVG |
大小 |
font-size |
width height |
background-size |
font-size |
width height |
无锯齿 |
受 font-family 影响 |
放大可能有锯齿 |
放大可能有锯齿 |
Windows 字号较小时 可能有锯齿 |
√ |
颜色 |
单色 用 color 设置 |
图片本身颜色 可用滤镜修改 |
图片本身颜色 可用滤镜修改 |
单色 用 color 设置 |
彩色 可修改 |
兼容性 |
不同浏览器显示效果可能不同 |
IE6 不支持 PNG透明 |
改大小IE9+ SVG Sprites IE9+ |
|
IE9+ |
响应式 |
- |
√ |
√ |
- |
√ |
透明 |
√ |
√ |
√ |
√ |
√ |
场景 |
简单图标 + emoji |
常用 |
HTTP1.1时合并请求 |
自定义字体 |
可定制图标 |
多方法实现圆角边框
- 背景图片
绘制圆角边框,切成 4 个圆角 + 4 个边框的小图片,拼成圆角边框
<style>
div {
height: 250px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAiCAIAAAC1JZyVAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA01pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDYuMC1jMDAyIDc5LjE2NDQ4OCwgMjAyMC8wNy8xMC0yMjowNjo1MyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHl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repeat-x, repeat-y, repeat-x, repeat-y;
background-position: top left, top right, bottom left, bottom right, top, right, bottom, left;
}
</style>
<div></div>
<style>
div {
height: 250px;
border: 10px solid;
border-radius: 10px;
}
</style>
<div></div>
<style>
div {
height: 250px;
border: 10px solid;
clip-path: inset(0 round 10px);
}
</style>
<div></div>
多方法实现小三角
- 伪类 + 特殊符号
<style>
div {
float: left;
margin-right: 10px;
}
.top::before {
content: '▲'
}
.right::before {
content: '▶';
}
.bottom::before {
content: '▼';
}
.left::before {
content: '◀';
}
</style>
<div class="top"></div>
<div class="right"></div>
<div class="bottom"></div>
<div class="left"></div>
- 特殊符号 + 缩进 + 溢出 + 旋转
<style>
div {
float: left;
margin-right: 6px;
font-size: 20px;
font-family: '宋体';
overflow: hidden;
}
.top {
text-indent: -10px;
transform: rotate(-90deg);
}
.right {
text-indent: -10px;
}
.bottom {
width: 10px;
transform: rotate(-90deg);
}
.left {
width: 10px;
}
</style>
<div class="top">◆</div>
<div class="right">◆</div>
<div class="bottom">◆</div>
<div class="left">◆</div>
- 宽高为 0 的边框
<style>
div {
float: left;
margin-right: 6px;
width: 0;
height: 0;
border: 10px solid transparent;
}
.top {
border-top-color: black;
}
.right {
border-right-color: black;
}
.bottom {
border-bottom-color: black;
}
.left {
border-left-color: black;
}
</style>
<div class="top"></div>
<div class="right"></div>
<div class="bottom"></div>
<div class="left"></div>
多方法隐藏元素
属性 |
不占位 |
读屏软件隐藏 |
用于隐藏主体内容 SEO |
display: none |
√ |
√ |
不抓取 |
visibility: hidden |
× |
√ |
可能抓取 |
opacity: 0 |
× |
× |
疑似作弊 |
input type = hidden |
√ |
√ |
不抓取 |
position:absolute / fixed |
√ |
× |
疑似作弊 |
aria-hidden = true |
× |
√ |
未知 |
text-indent < 0 |
√ |
× |
常用于在 Logo 处标识网站名称 |
font-size: 0 |
√ |
× |
疑似作弊 |
overflow: hidden |
√(裁剪) |
× |
抓取 |
clip-path: polygon(0 0, 0 0, 0 0, 0 0) |
√ |
× |
未知 |
实现文字描边
<style>
div {
font-size: 100px;
color: #fff;
text-shadow: 0 0 3px black;
}
</style>
<div>文字描边</div>
<style>
div {
font-size: 100px;
color: #fff;
-webkit-text-stroke: 3px black;
}
</style>
<div>文字描边</div>
- position: relative/position: absolute子绝父相
<style>
div {
position: relative;
font-size: 100px;
color: #fff;
}
div p {
position: absolute;
margin: 0;
}
div p:first-child {
font-weight: bolder;
color: black;
}
</style>
<div>
<p>文字描边</p>
<p>文字描边</p>
</div>
实现渐变背景
- 背景图片
一张宽 1px 像素,高度沿渐变方向固定,重复铺满即可
<style>
div {
height: 250px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAD6CAIAAADvIroEAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA01pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDYuMC1jMDAyIDc5LjE2NDQ4OCwgMjAyMC8wNy8xMC0yMjowNjo1MyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIDIyLjAgKDIwMjAwODE4Lm0uMTAxMiAyMDIwLzA4LzE4OiBmODJmNDMzNzYxKSAgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkQxQ0JDMDQyNzlEOTExRUJBQkYyQzQzRUVCM0ZDNjg0IiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkQxQ0JDMDQzNzlEOTExRUJBQkYyQzQzRUVCM0ZDNjg0Ij4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6RDFDQkMwNDA3OUQ5MTFFQkFCRjJDNDNFRUIzRkM2ODQiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6RDFDQkMwNDE3OUQ5MTFFQkFCRjJDNDNFRUIzRkM2ODQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz470lWNAAAAp0lEQVR42pRTQQ6DMAxLInHc/x+0M/8BaWAKbQQobtQdkGmtuLHTCr6zYdtNRAzlq6jvtRZEQ8Y3vP81cBeWekArMp5oUy70k/dFee+B+OprEF9ei1NTu3UiSPo7M2k89ZVkeu0jng33GM9OdbV5efIqSS4dTz4jlvlA/SsXKM98JFfnH76qxh93hd7jbN7ZvDwbJX5G5h3fQcy31i4b7DOZrL/9EGAAxQGYLZj/m8EAAAAASUVORK5CYII=);
background-repeat: repeat-x;
background-size: 1px 100%;
}
</style>
<div></div>
<style>
div {
height: 250px;
background-image: linear-gradient(to bottom, pink, skyblue);
}
</style>
<div></div>
对比常见图片格式和 base64 图片?
可以将图片转为 Base64 编码,直接将编码放入 CSS 中,即可引入图片
编码后的图片通常比原图大 30% 或更多,但可以与 CSS 一起被 gzip 或 br 压缩
适用小图片和没有权限上传图片的场景,来减少请求,但也应设置代码编辑器不换行或折叠图片编码区域,避免影响 CSS可读性
为什么要重置浏览器默认样式,对比 Reset.css 和 Normalize.css?
什么是浏览器默认样式
对于部分HTML标签,如段落、列表,部分表单元素,浏览器会提供默认样式,包含外观及交互,开发者只需引入标签,不需要重复定义这些样式,便于开发
为什么要重置默认样式
- 不同浏览器,默认样式可能不同,特别是尺寸,位置的不同,让开发者无法统一用户体验,甚至有错位的风险
- 只使用标签的语义,而不想引入样式
- UI稿与浏览器默认样式不同
基于以上,都需要开发者重置或部分重置浏览器的默认样式,以满足开发需求
对比常用的 Reset.css 和 Normalize.css
共同点
- 两者都能抹平浏览器间的默认样式差异
- 都部分重置了浏览器默认样式,尤其是内外边距属性
不同点
- Reset.css让元素在不同浏览器样式完全一样
- Normalize.css适当保留部分浏览器默认样式,只重置影响开发的样式,此外
- Normalize.css修复了表单、SVG溢出等BUG
- Normalize.css适当提高了可用性
- Normalize.css避免大量使用群组选择器,通过注释提高调试体验
最佳实践
对于绝大多数小型项目:
- 只重置在页面中使用到的标签
- 只重置有默认属性的属性名
- 适当保留浏览器的默认样式,如表单的outline
工程化
对比 Less、Sass、Stylus、PostCSS?
Less、Sass 和 Stylus 是 CSS 预处理器,PostCSS 是转换 CSS 工具的平台
Less
- 变量:$标识符变量,使用{}插值
- 嵌套:支持{ }大括号嵌套
- 混入器:支持 选择器 混入 或 使用.selector(@param)创建纯混入器
- 扩展 / 继承 / 运算符 / @import:支持
- 流程:支持if条件判断,支持when递归模拟循环
- 映射:支持@声明和使用 Map
- 特有:提供 Less.js,直接在浏览器使用 Less
SaSS
- 变量:支持$标识符变量,使用#{}插值
- 嵌套:SCSS 支持{ }大括号嵌套 SASS 支持缩进嵌套
- 混入器:@mixin创建@include使用
- 扩展 / 继承 / 运算符 / @import:支持
- 流程:支持if else条件判断,支持for while each循环
- 映射:支持$()声明 Map,提供map-get(map, key) map-keys(map) map-values(map)等一系列方法操作 Map,支持遍历 Map
- 特有:支持 compass ,内含 自动私有前缀 等一系列有用SASS模块,支持压缩输出
Stylus
- 变量:支持$标识符变量 和 赋值表达式变量,使用{}插值
- 嵌套:支持{ }大括号嵌套 和 缩进嵌套
- 混入器:像函数一样创建和使用
- 扩展 / 继承 / 运算符 / @import:支持
- 流程:支持if else unless三元 条件判断,支持for循环
- 映射:像 JS 一样创建和使用对象
- 特有:支持中间件,自动分配函数变量,提供 JS API。支持压缩输出
PostCSS
- 接受 CSS 文件,把 CSS 规则转换为抽象语法树
- 提供 API 供插件调用,对 CSS 处理
- 插件:支持 Autoprefixer 自动添加私有前缀、css-modules CSS 模块 stylelint CSS 语法检查等插件,PostCSSS 是工具的工具
Webpack 处理 SASS 文件时,sass-loader, css-loader,style
作用
- sass-loader
- 将 SASS / SCSS 文件编译成 CSS
- 调用node-sass,支持options选项向node-sass传参
- css-loader
- 支持读取 CSS 文件,在 JS 中将 CSS 作为模块导入
- 支持 CSS 模块 @规则@import @import url()
- style-loader