CSS基础

css历史

李爵士的同事赖先生提出CSS

css层叠样式表

样式层叠 选择器层叠 文件层叠

小贴士

目前世界上最广泛使用的CSS是CSS2.1版本

语法样式

语法一

  1. 选择器 {
  2. 属性名: 属性值;
  3. /*注释*/
  4. }

注意事项

  1. 所有符号都是英文符号,如果写错了,浏览器会警告
  2. 区分大小写,a 和 A 是不同的东西
  3. 没有 // 注释
  4. 最后一个分号可以省略,但建议不要省略
  5. 任何地方写错了,都不会报错,浏览器会直接忽略

语法二 at语法

  1. @charset "UTF-8";
  2. @import url(2.css);
  3. @media (min-width: 100px) and (max-width: 200px) {
  4. 语法一
  5. }

注意事项

  1. @charset 必须放在第一行
  2. 前两个 at 语法必须以分号 ; 结尾
  3. @media 语法会单独教学
  4. charset 是字符集的意思,但 UTF-8 是字符编码 encoding,这是历史遗留问题

border调试法

在每一行代码中添加boder属性去查看代码是否有错 outline这个标签不占里面的像素

文档流(元素流动的方向)

解释:”文档流”或”流式布局”是在对布局进行任何更改之前,在页面上显示”块”和”内联”元素的方式。 这个”流”本质上是一系列的事物,它们都在你的布局中一起工作,并且互相了解。 一旦某部分脱离了”流”,它就会独立地工作。

inline 内联元素

用做链接的 元素、 、 以及

从左到右排列 到达最右边换行 它不能设置高宽 他的高宽与内部元素的宽度加起来 和line-height 的高度有关。

  1. 盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味
  2. 盒子会和父容器一样宽
  3. 每个盒子都会换行
  4. width 和 height 属性可以发挥作用
  5. 内边距(padding), 外边距(margin) 和 边框(border) 会将其他元素从当前盒子周围“推开”

block 块级元素

标题(等)和段落()默认情况下都是块级的盒子。

默认宽度是auto 可以设置高宽 永远不要写宽度100%。

  1. 盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味
  2. 盒子会和父容器一样宽
  3. 每个盒子都会换行
  4. width 和 height 属性可以发挥作用
  5. 内边距(padding), 外边距(margin) 和 边框(border) 会将其他元素从当前盒子周围“推开”

inline-block 行内块元素

既有inline元素的特点 也有 block元素的特点 但是不会把元素分为2部分换行。

小贴士

不能在内联元素里面用块级元素

  1. <style>
  2. .btn {
  3. border: 1px solid red;
  4. }
  5. .he {
  6. border: 1px solid green;
  7. /* <!-- 实际高度没有撑开 --> */
  8. padding: 20px 10px;
  9. /* span 最终高度是100px 实际高度和字体有关 */
  10. line-height: 100px;
  11. }
  12. </style>
  13. <div class="btn">
  14. <span class="he">
  15. 你好
  16. </span>
  17. </div>

定位可以脱离文档流

内容超过元素高度可以用:overflow来操作。

visible 默认值。内容不会被修剪,会呈现在元素框之外。
hidden 内容会被修剪,并且其余内容是不可见的。
scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。
auto 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
inherit 规定应该从父元素继承 overflow 属性的值。
  1. <style>
  2. .div01 {
  3. border: 2px solid red;
  4. height: 200px;
  5. overflow: auto;
  6. }
  7. </style>
  8. <div class="div01">
  9. <!-- 把屏幕缩小 里面的文字只会显示在第一屏 -->
  10. 这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字这是一段文字
  11. <div style="border: 1px solid blue; width: 1000px;">你好</div>
  12. <span style="border: 1px solid pink;">123</span>
  13. </div>

脱离文档流

  1. <!-- /* 这是显示脱离文档的第一个方法 */-->
  2. <style>
  3. .div01 {
  4. border: 1px solid red;
  5. }
  6. .div02 {
  7. border: 1px solid green;
  8. }
  9. span {
  10. position: absolute;
  11. /*第二种脱离文档流 让他浮动*/
  12. float:left;
  13. }
  14. </style>
  15. <div class="div01">
  16. <div class="div02">
  17. <span>你好</span>
  18. </div>
  19. </div>

盒模型

CSS盒模型是将所有元素表示为一个个矩形的盒子。由content(内容)、padding(内边距)、border(边框)、margin(外边距)组成。

SS盒模型有2种

一种是:content-box 标准模型

content-box 的宽度 width 表示内容区的宽度,不包含 padding 和 border。

一种是:border-box

border-box 的宽度 width 表示内容区 + padding + border 的总和。

这两种都没有将margin算入盒子大小内 。

如果算上margin就是在算盒子位置的大小。

02 CSS全解 - 图2

  1. <style>
  2. .content-box {
  3. margin: 25px;
  4. border: 5px solid red;
  5. padding: 15px;
  6. box-sizing: content-box;
  7. width: 100px;
  8. }
  9. .boder-box {
  10. margin: 25px;
  11. border: 5px solid red;
  12. padding: 15px;
  13. box-sizing: border-box;
  14. width: 100px;
  15. }
  16. </style>
  17. <body>
  18. <div class="content-box">content-box</div>
  19. <div class="boder-box">boder-box</div>
  20. </body>

margin合并(外边距重叠)

块的上外边距(margin-top)和下外边距(margin-bottom)有时合并(折叠)为单个边距,其大小为单个边距的最大值(或如果它们相等,则仅为其中一个),这种行为称为边距折叠。

注意:注意有设定float和position=absolute的元素不会产生外边距重叠行为。

只存在上下合并 有三种情况会形成外边距重叠:

第一种情况:

同一层相邻元素之间。

  1. <style>
  2. p:nth-child(1){
  3. margin-bottom: 13px;
  4. }
  5. p:nth-child(2){
  6. margin-top: 87px;
  7. }
  8. </style>
  9. <p>下边界范围会...</p>
  10. <p>...会跟这个元素的上边界范围重叠。</p>

相邻的两个元素之间的外边距重叠,除非后一个元素加上clear-fix清除浮动。

clear-fix清除浮动也就是下面代码:

  1. #container::after {
  2. content: "";
  3. display: block;
  4. clear: both;
  5. }

没有内容将父元素和后代元素分开:

  1. <style>
  2. .div03 {
  3. background-color: antiquewhite;
  4. /* 外边距合并 复合子和子盒子的外边距会合并 那个高留那个的外边距 */
  5. /* 方法一 加 边框 border */
  6. /* 方法二 加 padding */
  7. /* 方法三 加 overflow: hidden; */
  8. margin: 50px;
  9. }
  10. .div04 {
  11. border: 2px solid green;
  12. margin: 30px 0;
  13. }
  14. .div05 {
  15. border: 2px solid yellow;
  16. /* 外边距合并2 兄弟合并 一个盒子的底外边距 和 相邻盒子的上外边距会合并
  17. 那个margin值大留那个的外边距 */
  18. /* 兄弟合并 用 width: 100%; display: inline-block; */
  19. margin-top: 50px;
  20. }
  21. </style>
  22. <div class="div03">
  23. <div class="div04">盒子01</div>
  24. <div class="div05">盒子02</div>
  25. <div class="div05">盒子02</div>
  26. </div>

空的块级元素:

当一个块元素上边界margin-top 直接贴到元素下边界margin-bottom时也会发生边界折叠。这种情况会发生在一个块元素完全没有设定边框border、内边距paddng、高度height、最小高度min-height 、最大高度max-height 、内容设定为inline或是加上clear-fix的时候。

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. p {
  8. margin-bottom: 13px;
  9. background-color: rgb(255, 164, 164);
  10. }
  11. div {
  12. margin-top: 13px;
  13. margin-bottom: 87px;
  14. background-color: rgb(104, 182, 255);
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <p>下边界范围是 87 ...</p>
  20. <div></div>
  21. <p>... 上边界范围是 87</p>
  22. </body>

基本单位

长度单位

  1. px 像素
  2. em 在 font-size 中使用是相对于父元素的字体大小,在其他属性中使用是相对于自身的字体大小
  3. 百分数
  4. 整数
  5. rem:等你把 em 滚瓜烂熟了再问 rem
  6. vw 和 vh ( 视窗宽度的1%/视窗高度的1%)
  7. 其他长度单位都用得很少,不用了解

颜色

  1. 十六进制 #FF6600 或者 #F60
    02 CSS全解 - 图3

  2. RGBA 颜色 rgb(0,0,0) 或者 rgba(0,0,0,1)02 CSS全解 - 图4

  3. hsl 颜色 hsl(360,100%,100%)
    02 CSS全解 - 图5

    rgba与opacity的区别


opacity默认是继承父元素opacity属性,所以子元素会继承父元素的opacity属性值,从而产生相同的效果。也就整体变色。
例子:

  1. <div class="opacity" style="opacity: 0; width: 200px; height: 50px; background-color: black;">
  2. <p style="opacity: 1; color: aliceblue;">pd的opacity是1</p>
  3. </div>


rgba只会对元素本身产生影响,因为无法继承,所以不会对子元素产生影响。

  1. <div style=" width: 200px; height: 50px; background-color: rgba(0, 0,0, 0.5);">
  2. <p>pd的opacity是1</p>
  3. </div>

CSS布局

  1. 固定宽度布局

  2. 不固定宽度布局

  3. 响应式布局

    02 CSS全解 - 图6

float布局(更多看 05float布局.html)

最初,引入 float 属性是为了能让 web 开发人员实现简单的布局,包括在一列文本中浮动的图像,文字环绕在它的左边或右边。你可能在报纸版面上看到过。

小贴士

留一些空间或者最后一个不设置width。或者只设置max-width。

float布局做不了响应式

IE 6/7存在双倍 margin bug

  1. 方法一:针对IE 6/7margin减半
  2. 方法二 :加一个display: inline-block

图片与背景图有差距 用 vertical-align : middle;

float 二栏布局

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. body {
  8. width: 90%;
  9. max-width: 900px;
  10. margin: 0 auto;
  11. text-align: center;
  12. }
  13. div:nth-of-type(1) {
  14. width: 48%;
  15. float: left;
  16. }
  17. div:nth-of-type(2) {
  18. width: 48%;
  19. float: right;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <h1>2栏布局</h1>
  25. <div>
  26. <h2>First column</h2>
  27. <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at ultricies tellus laoreet sit
  28. amet.
  29. </p>
  30. </div>
  31. <div>
  32. <h2>Second column</h2>
  33. <p>Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada ultrices. Phasellus turpis est, posuere sit amet dapibus ut, facilisis sed est. Nam id risus quis ante semper consectetur eget aliquam lorem. Vivamus tristique elit dolor,
  34. sed pretium metus suscipit vel. Mauris ultricies lectus sed lobortis finibus.
  35. </p>
  36. </div>

float 三栏布局

float 四栏布局

loat 平均布局

float平均布局 的时候 如果 里面的盒子的超出了父盒子的大小 就可以在他们的上层在添加一个盒子 用负的margin来让他们在一行平均分配。

还可直接用最原始的办法 直接用nth-chlid 清除想去掉的边距。看清楚是外边距 还是内边距

  1. .image:nth-child(1) {
  2. margin-left: 0;
  3. }

负margin案例如下:

  1. <style>
  2. .images {
  3. outline: 1px solid red;
  4. width: 800px;
  5. margin-left: auto;
  6. margin-right: auto;
  7. }
  8. .images .image {
  9. height: 200px;
  10. width: 191px;
  11. background-color: bisque;
  12. float: left;
  13. margin-right: 12px;
  14. }
  15. .x {
  16. margin-right: -12px;
  17. }
  18. /* 或者直接用nth-chlid 清除最后一行孩子的右边距*/
  19. </style>
  20. <body>
  21. <div class="images clearfix">
  22. <div class="x">
  23. <div class="image"></div>
  24. <div class="image"></div>
  25. <div class="image"></div>
  26. <div class="image"></div>
  27. </div>
  28. </div>
  29. </body>

清楚浮动是方法

方法一 结尾处加空div标签 clear:both
  1. <style>
  2. .box {
  3. background: blue;
  4. }
  5. .left {
  6. background: red;
  7. float: left;
  8. height: 100px;
  9. width: 200px;
  10. }
  11. .right {
  12. background: pink;
  13. float: right;
  14. height: 100px;
  15. width: 200px;
  16. }
  17. .clear {
  18. clear: both;
  19. }
  20. </style>
  21. <body>
  22. <div class="box">
  23. <div class="left">Left</div>
  24. <div class="right">Right</div>
  25. <div class="clear"></div>
  26. </body>

方法二 父级div定义 伪类:after (常用这个)
  1. .clear{zoom:1;}/*为解决ie6,ie7浮动问题*/
  2. .clear:after{
  3. display:block;
  4. clear:both;
  5. content:"";
  6. }

方法三 父级div定义 overflow:hidden/auto

原理:

  1. 必须定义width或zoom:1,同时不能定义height,使用overflow:hidden/auto时,浏览器会自动检查浮动区域的高度.

缺点:

  1. overflow:hidden:不能和position一起用,因为超出的尺寸的会被隐藏。
  2. overflow:auto:内部宽高超过父级div时,会出现滚动条。
  1. <style>
  2. .box {
  3. background: red;
  4. overflow: hidden;
  5. }
  6. .left {
  7. background: blue;
  8. float: left;
  9. height: 100px;
  10. width: 40%;
  11. }
  12. .right {
  13. background: pink;
  14. float: right;
  15. height: 100px;
  16. width: 50%;
  17. }
  18. </style>
  19. <body>
  20. <div class="box">
  21. <div class="left">Left</div>
  22. <div class="right">Right</div>
  23. </div>
  24. 123
  25. </body>

flex布局

详细知识推荐 阮一峰Flex 布局教程:语法篇 博客 (侵权删除)

container样式

flex-direction

flex-wrap

flex-flow

justify-content

align-items

align-content

主轴 flex-direction 主轴一旦定义 另一各轴就是交叉轴

flex-direction 控制item的流动方向

  1. row 从左往右

  2. row-reverse 从右往左

  3. column 从上往下

  4. column-reverse 从下往上

02 CSS全解 - 图7

justify-content 主轴对齐方向

  1. flex-start 从行首开始排列。每行第一个弹性元素与行首对齐,同时所有后续的弹性元素与前一个对齐。
  2. flex-end 从行尾开始排列。每行最后一个弹性元素与行尾对齐,其他元素将与后一个对齐。
  3. center 伸缩元素向每行中点排列。每行第一个元素到行首的距离将与每行最后一个元素到行尾的距离相同。
  4. space-around 在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素到行首的距离和每行最后一个元素到行尾的距离将会是相邻元素之间距离的一半。
  5. space-between 在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素与行首对齐,每行最后一个元素与行尾对齐。

align-items 次轴对齐方向

align-content 有多行的时候对齐方向

  1. flex-start 元素向侧轴起点对齐。
  2. flex-end 元素向侧轴终点对齐。
  3. center 元素在侧轴居中。如果元素在侧轴上的高度高于其容器,那么在两个方向上溢出距离相同。

flex-wrap 换行

  1. nowrap 不换行

  2. wrap 换行,第一行在上方

  3. wrap-reverse 换行,第一行在下方

  1. <style>
  2. .container {
  3. display: flex;
  4. /* display: inline-flex; */
  5. border: 1px solid red;
  6. /* 列表示 */
  7. flex-direction: row;
  8. /*
  9. flex-direction 控制item的流动方向
  10. row 从左往右
  11. row-reverse 从右往左
  12. column 从上往下
  13. column-reverse 从下往上
  14. */
  15. /* flex-wrap 是否换行
  16. wrap-revers 翻折
  17. */
  18. flex-wrap: nowrap;
  19. /* justify-content 主轴对齐方向 */
  20. justify-content: flex-start;
  21. /* align-items 次轴对齐方向 */
  22. align-items: flex-start;
  23. /* align-content 有多行的时候 */
  24. align-content: flex-start;
  25. }
  26. .item {
  27. width: 50px;
  28. border: 1px solid blueviolet;
  29. }
  30. .item-a {
  31. height: 30px;
  32. }
  33. .item-b {
  34. height: 60px;
  35. }
  36. .item-c {
  37. height: 50px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <div class="item item-a"></div>
  44. <div class="item item-b"></div>
  45. <div class="item item-c"></div>
  46. </div>
  47. </body>

让item盒子垂直居中

  1. display: flex;
  2. justify-content: center;
  3. align-items: center;

item 样式

详细知识推荐 阮一峰Flex 布局教程:语法篇 博客 (侵权删除)

6个属性设置item上。

order

flex-grow

flex-shrink

flex-basis

flex

align-self

以下引用:

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0 ;可以为正数或负数。

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小.

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

小贴士:完成青蛙小游戏对flex布局更加的深刻

  1. <style>
  2. .container {
  3. display: flex;
  4. border: 1px solid red;
  5. }
  6. .item {
  7. border: 1px solid blue;
  8. width: 150px;
  9. height: 50px;
  10. margin: 0 5px;
  11. }
  12. /* order 从小到大排列 */
  13. /* 用来分配多余的空间 flex-grow: 0; 可以不写 */
  14. /* flex-shrink 空间不够指定某个盒子变瘦
  15. 0 不变瘦
  16. 1一起变瘦
  17. 大于1 当缩小到小于item盒子的总宽时 将自己的空间分给别人 */
  18. /* align-self 给加了这个属性的特立独行 */
  19. .item:first-child {
  20. order: 100;
  21. flex-shrink: 1;
  22. }
  23. .item:nth-child(2) {
  24. order: 1;
  25. flex-shrink: 5;
  26. }
  27. .item:last-child {
  28. order: 2;
  29. flex-shrink: 1;
  30. align-self: flex-end;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div class="item">1</div>
  37. <div class="item">2</div>
  38. <div class="item">3</div>
  39. </div>
  40. </body>

grid布局

详解见链接 引用阮一峰的一篇博客(侵权删除)

display: grid

grid-template-columns 定义每一列的列宽

grid-template-rows 属性定义每一行的行高

为了方便表示比例关系,网格布局提供了fr关键字(fraction 的缩写,意为”片段”)。如果两列的宽度分别为1fr和2fr,就表示后者是前者的两倍。同时还可以和像素 一起使用

grid-row-gap 属性设置行与行的间隔

grid-column-gap 属性设置列与列的间隔

grid-gap 属性是grid-column-gap和grid-row-gap的合并简写形式

grid-template-areas 属性 用于定义区域 在子元素上grid-area 连用 。

小贴士

设为网格布局以后,容器子元素(项目)的float、display: inline-block、display: table-cell、vertical-align和column-*等设置都将失效。

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .container {
  8. min-height: 100vh;
  9. display: grid;
  10. grid-row-gap: 10px;
  11. grid-column-gap: 10px;
  12. grid-template-columns: 60px auto 60px;
  13. grid-template-rows: 100px auto 100px;
  14. grid-template-areas:
  15. "header header header"
  16. "aside main ad"
  17. "footer footer footer";
  18. }
  19. .container>* {
  20. border: 1px solid red;
  21. }
  22. .container>header {
  23. grid-area: header;
  24. }
  25. .container>aside {
  26. grid-area: aside;
  27. }
  28. .container>main {
  29. grid-area: main;
  30. }
  31. .container>.ad {
  32. grid-area: ad;
  33. }
  34. .container>footer {
  35. grid-area: footer;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="container">
  41. <header>header</header>
  42. <aside>aside</aside>
  43. <main>main</main>
  44. <div class="ad">sd</div>
  45. <footer>footer</footer>
  46. </div>
  47. </body>

CSS定位

详细知识: 阮一峰CSS 定位详解 博客 (侵权删除)

position的属性

小贴士

不要写 z-index:9999;

absolute 相对于祖先元素中最近的一个定位元素决定的 除了 static。

不要再移动端使用 fixed。

static默认值,待在文档流里

relative相对定位,升起来,但不脱离文档流

absolute绝对定位,定位基准是祖先里的非static

fixed固定定位,定位基准是viewport (有诈)

sticky粘滞定位,适用于导航

层叠上下文

相关知识链接:点击这里那些属性能创建上下层叠。z-index / flex / opacity / transform 会创建层叠上下文

div的分层

02 CSS全解 - 图8

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .container {
  8. border: 1px solid red;
  9. height: 200px;
  10. position: relative;
  11. }
  12. .a {
  13. position: relative;
  14. z-index: 0;
  15. border: 1px solid red;
  16. }
  17. .a2 {
  18. position: relative;
  19. z-index: 10;
  20. height: 200px;
  21. width: 200px;
  22. background-color: aquamarine;
  23. }
  24. .b2 {
  25. position: relative;
  26. top: -20px;
  27. z-index: 5;
  28. height: 200px;
  29. width: 200px;
  30. background-color: pink;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <!-- z-index:auto 不创建层叠上下文
  36. z-index:1/2/3/-1/-2/-3 创建层叠上下文 -->
  37. <!--
  38. 如果 a b 没有设置 z-index 也就是没有创建层叠上下文.
  39. 而 a2 b2 设置了z-index 就会在 container里面的世界相比层级 .
  40. 如果a b 设置了z-index 也就是创建层叠上下文.
  41. 就会在container 小世界里面对比 a b 的层级
  42. a2 b2 不参与container小世界的层级对比
  43. -->
  44. <!-- container是一个小世界 -->
  45. <div class="container">
  46. <!-- a是一个小世界 -->
  47. <div class="a">
  48. <div class="a2">相对于a的层级是10</div>
  49. </div>
  50. <!-- b是一个小世界 -->
  51. <div class="b">
  52. <div class="b2">相对于a的层级是5</div>
  53. </div>
  54. </div>
  55. </body>

CSS动画

详细知识了解 : 点击这里

动画是由许多静止的画面组成的(帧)

动画原理

  1. <style>
  2. .demo {
  3. width: 100px;
  4. height: 100px;
  5. border: 1px solid red;
  6. position: relative;
  7. left: 0;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <div class="demo"></div>
  13. </body>
  14. <script>
  15. // 动画原理
  16. var n = 1;
  17. var demo = document.querySelector('.demo');
  18. var id = setInterval(function() {
  19. if (n <= 200) {
  20. demo.style.left = n + 'px';
  21. n += 1;
  22. } else {
  23. clearInterval(id);
  24. }
  25. }, 1000 / 60)
  26. </script>

浏览器渲染步骤

  1. 根据HTML构建HTML树(DOM)
  2. 根据CSS构建CSS树(CSSOM)
  3. 将两棵树合并成一颗渲染树(render tree)
  4. Layout布局(文档流、盒模型、计算大小和位置)
  5. Paint绘制(把边框颜色、文字颜色、阴影等画出来)
  6. Composite合成(根据层叠关系展示画面)

三种更新方式

02 CSS全解 - 图9

transform属性

详细知识:点击这里

位移translate

  1. <style>
  2. .demo {
  3. width: 100px;
  4. height: 100px;
  5. border: 1px solid red;
  6. transition: all 1s linear;
  7. }
  8. .demo:hover {
  9. /* transform: translateX(200px); */
  10. /* transform: translateY(50px); */
  11. /* transform: translateZ(-200px);*/
  12. /* transform: translate(20px, 50px); */
  13. transform: translateX(50%);
  14. /* 向右位移自生的50% */
  15. }
  16. .w {
  17. perspective: 1000px;
  18. /* 视点 距离中心 1000px*/
  19. border: 1px solid red;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="w">
  25. <div class="demo"></div>
  26. </div>

居中小贴士

  1. { left: 50%;
  2. top: 50%;
  3. transform: translate(-50%, -50%);
  4. }

缩放scale

  1. <style>
  2. #demo {
  3. width: 100px;
  4. height: 100px;
  5. border: 1px solid red;
  6. margin: 50px;
  7. transition: all 1s linear;
  8. }
  9. #demo:hover {
  10. /* transform: scaleX(1.5); */
  11. /* transform: scaleY(1.5); */
  12. transform: scale(1.5, 0.5);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="wrapper">
  18. <div id="demo"></div>
  19. </div>
  20. </body>

旋转rotate

  1. <style>
  2. #demo {
  3. width: 100px;
  4. height: 200px;
  5. border: 1px solid red;
  6. margin: 50px;
  7. transition: all 1s linear;
  8. }
  9. #demo:hover {
  10. transform: rotateZ(45deg);
  11. /* transform: rotate3d(1, 1, 1, 45deg);*/
  12. }
  13. .wrapper {
  14. perspective: 1000px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="wrapper">
  20. <div id="demo"></div>
  21. </div>
  22. </body>

倾斜skew

  1. <head>
  2. <meta charset="utf-8">
  3. <title>JS Bin</title>
  4. <style>
  5. #demo {
  6. width: 100px;
  7. height: 200px;
  8. border: 1px solid red;
  9. margin: 50px;
  10. transition: all 1s linear;
  11. }
  12. #demo:hover {
  13. transform: skewX(15deg);
  14. }
  15. .wrapper {
  16. perspective: 1000px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="wrapper">
  22. <div id="demo"></div>
  23. </div>
  24. </body>

transition过渡

transition:属性名 时长 过渡方式 延迟

transition: left 200ms linear

可以用逗号分隔两个不同属性

transition: left 200ms, top 400ms

可以用all代表所有属性

transition: all 200ms

  1. <style>
  2. #demo {
  3. width: 100px;
  4. height: 100px;
  5. border: 1px solid red;
  6. margin: 50px;
  7. transition: transform 1s linear;
  8. }
  9. #demo.b {
  10. transform: translateX(200px);
  11. }
  12. #demo.c {
  13. transform: translateX(200px) translateY(100px);
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div class="wrapper">
  19. <div id="demo"></div>
  20. <button id=button>开始</button>
  21. </div>
  22. </body>
  23. <script>
  24. button.onclick = () => {
  25. demo.classList.add('b')
  26. setTimeout(() => {
  27. demo.classList.remove('b')
  28. demo.classList.add('c')
  29. }, 1000)
  30. }
  31. </script>

CSS动画优化

详细知识:点击这里

  1. 降低选择器的复杂性;使用以类为中心的方法,例如 BEM。
  2. 减少必须计算其样式的元素数量。
  3. 尽可能避免布局操作
  4. 避免强制同步布局和布局抖动;先读取样式值,然后进行样式更改
  5. 降低绘制的复杂性
  6. 减少绘制区域
  7. 提升移动或淡出的元素
  8. 坚持使用 transform 和 opacity 属性更改来实现动画。
  9. 使用 will-change 或 translateZ 提升移动的元素。
  10. 避免过度使用提升规则;各层都需要内存和管理开销。
  11. 坚持使用 transform 和 opacity 属性更改来实现动画。
  12. 使用 will-change 或 translateZ 提升移动的元素。
  13. 避免过度使用提升规则;各层都需要内存和管理开销。

资料来源:饥人谷、阮一峰

本文为贰贰的原创文章,著作权归本人和饥人谷所有,转载务必注明来源