选择器
基本选择器机器扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| *{ margin:0; padding:0; }
#id{ background-color:#123456; }
.item{ width:100px; height:100px; }
input{ outline:none; }
.item ul li{ color:blue; }
.father > .son{ position:absolute; }
.first ~ .third{ color:green; }
.first + .second{ color:red; }
h1,h2,h3{ color:black; }
div[attr]{ background-color:pink; } div[attr=val]{ background-color:pink; } div[attr~=val]{ background-color:pink; } div[attr|=val]{ background-color:pink; } div[attr^=val]{ background-color:pink; }div[attr$=val]{ background-color:pink; } div[attr*=val]{ background-color:pink; }
a:link{ color: blue; } a:visited{ color:green; } #container:target .item{ display:block; } .box:hover{ color:red; } .box:active{ color:blue; }
input:enabled{ background-color:green; } input:disabled{ background-color:red; } input:checked{ width:100px; height:100px; } input:focus{ opacity:0.6; }
.item:nth-child(1){
color: red; } .container div:nth-of-type(2){
color:blue; } .box:empty{ background-color:red; } a:not(:last-of-type){ border-right:1px solid red; }
div:before{ /*由css生成的在元素之前的结构*/ content""; display:block; } div:after{ /*由css生成的在元素之后的结构*/ content:""; display:block; } p::first-letter{ font-size:20px; } p::first-line{ font-size:20px; } div::selection{ background-color:red; color:pink; }
:root{ border:1px solid black; }
|
声明的优先级
选择器特殊性
特殊性表述:0 0 0 0
ID选择器:0 1 0 0
类选择器、属性选择器、伪类:0 0 1 0
元素选择器、伪元素:0 0 0 1
通配符:0 0 0 0
内联样式:1 0 0 0
继承而来的属性无特殊性(即比通配符还低)
结合符对选择器特殊性无贡献
不进位(即1 0 0 0大于所有 0 x x x)
重要声明
声明的结束分号之前可插入!important来标志,表示最重要的声明,覆盖其他所有声明
css样式的来源大致有三种:
- 创作人员(程序员)
- 读者(用户)
- 用户代理(浏览器)
权重:
- 读者的重要声明
- 创作人员的重要声明
- 创作人员的正常声明
- 读者的正常声明
- 用户代理的声明
层叠:
找出所有相关的规则,这些规则都包含一个选择器
计算声明的优先级:
- 先按来源排序
- 再按选择器的特殊性排序
- 最终排序
自定义字体
1 2 3 4 5 6 7 8 9 10
| <style type="text/css"> @font-face{ font-family:"Damu"; src:url(./xx.ttf); }
div.test{ font:50px "Damu"; } </style>
|
新增UI方案
文本新增样式
- 透明度:opacity
- 颜色模式:rgba()
- 换行:word-break、word-wrap
1 2 3 4 5 6
| .test{ word-break: keep-all; } .test1{ word-wrap: break-word; }
|
1 2 3 4 5 6
| .test{ opacity:0.5; color:rgba(255,255,255,.5); text-shadow: 2px 3px 4px pink, 3px 4px 5px black; }
|
- 文字描边(只有webkit内核支持):-webkit-text-stroke
1 2 3
| .test{ -webkit-text-stroke: 4px pink; }
|
1 2 3 4
| .test{ direction:rtl; unicode-bidi:bidi-override; }
|
1 2 3 4 5
| .test{ white-space:nowrap; overflow:hidden; text-overflow:ellipse; }
|
盒模型新增样式
1 2 3 4
| .test{ box-shadow: 5px 5px 20px 10px gray, /*第四个可选为阴影缩放,大于0放大,小于0缩小*/ inset -5px -5px 20px gray; }
|
- 盒模型倒影(-webkit内核):-webkit-box-reflect
1 2 3
| .test{ -webkit-box-reflect:left 10px; }
|
- 拖拽大小:resize(需配合overflow:auto;)
1 2 3 4
| .test{ overflow:auto; resize:horizontal; }
|
1 2 3
| .test{ box-sizing:border-box; }
|
新增UI样式
- 圆角:border-radius(尽量使用px值)
1 2 3
| .test{ border-radius:10px/20px 20px/10px 10px/20px 20px/10px; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| .test{ border:30px solid; border-image-source: url(xxx.png); border-image-slice: 33.33333% fill; border-image-repeat: round; border-image-width: 20px; border-image-outset:10px; }
|
- 背景
- background-color:默认绘制时,平铺border-box
- background-image:默认从padding-box开始绘制,从border-box开始裁剪 || css3中有多背景,默认绘制时尺寸是自己的位图像素
- background-repeat:控制平铺与否
- background-position:控制背景图片在背景区域中的位置
- background-attachment:scroll(背景图不会随着滚动条的滚动而滚动) || fixed(相对视口固定定位)
- background-origin:背景图片起始位置
- background-clip:裁剪图片
- background-size:控制图片自适应方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| .test{ background-color:pink; background-image:url(xx.png); background-repeat:no-repeat; background-position: -10px 10px; background-attachment:fixed; background-origin: content-box; background-clip:content-box; -webkit-background-clip:text; background-size: 100% 100%; background-image:linear-gradient(to right,red 10%,green 50%); background-image:repeating-linear-gradient(45deg,red,rgba(255,0,0,0) 50%); background-image:radial-gradient(circle,yellow 10%, pink 70%); background-image:radial-gradient(closest-side,yellow 10%, pink 70%); }
|
定位与浮动
滚动条问题
- 如果body内部长度超过body长宽,滚动条会出现在document上,只有当html和body都设置了overflow属性时,才会出现在body身上
1 2 3
| html,body{ overflow:auto; }
|
- 所以开发时一般使用一个div.wrap来模拟视口,代替body(将html和body的滚动条禁用)
过渡
1 2 3 4 5 6 7 8 9 10 11 12
| .test{ transition-property: width, height, font; transition-duration: 3s, 2s; transition-timing-function: linear, cubic-bezier(.28,1.64,.76.-0.5); transition-delay: 3s; transition: 3s width, 2s height linear 4s; }
|
1 2 3 4 5 6 7 8 9 10
| window.onload = function(){ var testNode = document.querySelector("#test"); document.body.onmouseover = function(){ testNode.style.width = "300px"; testNode.style.height = "300px"; }; testNode.addEventListener("transitionend",function(){ alert("transitionend!"); }); }
|
transition在元素首次渲染还没有结束的情况下是不会被触发的
2D变形
- transform属性只对block元素生效
- transform: rotate():旋转,还有rotateX(),rotateY(),rotateZ()
- transform: translate():平移,还有translateX(),translateY(),translateZ()
- transform: skew():倾斜,还有skewX(),skewY()
- transform: scale():缩放,还有scaleX(),scaleY()
transform-orgin: top left;
改变基点
- 矩阵变换:transform: matrix();
1 2 3 4 5 6
| .test{ transform: translate(50%,50%) rotate(90deg) scale(1.1) skew(30deg); } .test1{ transform: matrix(1,0,0,1,0,0); }
|
3D变形
- rotateX(),rotateY,rotateZ(), rotate3d(x,y,z,angle)
- translateX(),translateY(),translateZ(),translate3d()
- scaleX(),scaleY(),scaleZ() ,scale3d()
景深-perspective
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| .wrap{ width: 400px; height: 400px; border: 1px solid black; perspective: 200px; perspective-orgin: left top; transform:perspective(200px) rotateY(30deg); } .inner{ width:200px; height: 200px; border-radius:50%; transition: 2s; } .inner:hover{ transform: rotateX(360deg); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| .wrap{ width: 400px; height: 400px; border: 1px solid black; perspective: 200px; transform-style: preserve-3d; } .inner{ width:200px; height: 200px; border-radius:50%; transition: 2s; } .inner:hover{ transform: rotateX(360deg); }
|
背面 backface-visibility
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| .wrap{ width: 400px; height: 400px; border: 1px solid black; perspective: 200px; transform-style: preserve-3d; } .inner{ width:200px; height: 200px; border-radius:50%; transition: 2s; backface-visibility: hidden; } .inner:hover{ transform: rotateX(360deg); }
|
动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| .test{ width: 100px; height: 100px; background-color: pink; animation-name: test; animation-duration: 3s; animation-timing-function: linear; animation-delay: 1s; animation-iteration-count: 3; animation-direction: reverse; animation-fill-mode: backwards; animation-play-state: running; } @keyframes test{ from{ background-color: orange; }to{ background-color: skyblue; } }
|
布局
flex布局-弹性盒子布局
- flex容器
- flex项目
- 主轴
- 侧轴
- 项目永远在主轴的正方向上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| .test{ width: 100px; height: 100px; display: -webkit-box; -webkit-box-orient: vertical; -webkit-box-direction: reverse; -webkit-box-pack: center; }
.new{ width: 100px; height: 100px; display: flex; justify-content: space-around; align-items: center; flex-direction: column-reverse; flex-wrap: wrap; flex-flow: column-reverse wrap; }
.item{ flex-grow: 2; flex-shrink: 1; flex-basis: 300px; flex: 2 1 300px; order:2; }
|
多列布局
1 2 3 4 5 6 7
| .test{ column-count: 3; column-width: 100px; column-gap: 10px; column-rule: 2px solid red; column-span: all; }
|
滤镜
1 2 3 4 5 6 7 8 9 10 11 12
| .test{ filter: brightness(80%) /* 亮度:0%为黑色,0-100为减弱图片亮度,往上为增大亮度 */ grayscale(10%) /* 灰度:0%表示不做修改,100%为完全黑度 */ sepia(0%) /* 复古:即褐色滤镜,100%为完全褐色滤镜 */ invert() /* 反转:颜色反转,100%为反转所有颜色 */ hue-rotate(120deg) /* 旋转:色相旋转,0-360deg */ drop-shadow(5px 5px 5px red) /* 阴影:前两个为偏移量,第三个为模糊程度,最后为阴影颜色 */ opacity(60%) /* 透明度:0为完全透明 */ blur(2px) /* 模糊:像素值越大,模糊程度越大 */ contrast(100%) /* 对比度:100%向下为减弱对比度,向上为增强对比度 */ saturate(100%); }
|
其他
1 2 3 4 5 6 7 8 9
| .test{ outline: 1px solid red; xx: initial; width: cacl(100% - 30px); pointer-events: none; }
|