CSS3新增了许多灵活查找元素的方法,极大的提高了查找元素的效率和精准度。CSS3选择器与jQuery中所提供的绝大部分选择器兼容。
属性选择器:属性是相对于标签而言,所谓属性选择器就是根据指定名称的属性的值来查找元素。
1.E:first-child:查找E元素的父元素的第一个子元素E,不会查找的元素的类型。
2.E:last-child:查找E元素的父元素中最后一个子元素E,不会查找的元素的类型。
3.E:first-of-type:查找E元素的父元素的第一个子元素E,查找的元素的类型,过滤掉其它类型的元素。
4.E:last-of-type:查找E元素的父元素的第一个子元素E,查找的元素的类型。
5.E:nth-child(n):第n个子元素E,不查找类型。
6.E:nth-last-child(n):倒数第n个子元素E,不查找类型。
7.E:nth-child(even):所有偶数,不查找类型。
8.E:nth-child(odd):所有奇数,不查找类型。
9.E:nth-of-type(n):第n个子元素E,类型。
10.E:nth-last-of-type(n):倒数第n个子元素E,类型。
11.E:nth-of-type(even):所有偶数,类型。
12.E:nth-of-type(odd):所有奇数,类型。
注意:n:默认取值范围为0~子元素的长度,但是当n<=0时,选取无效。
n可是多种形式:nth-child(2n)、nth-child(2n+1)、nth-of-type(-n+5)等
eg.为前、后5个li元素添加样式
分析
n=0,-n+5=5;
n=1,-n+5=4;
n=2,-n+5=3;
n=3,-n+5=2;
n=4,-n+5=1;
n=5,-n+5=0;(无效)
所以前、后五个元素应用了样式
13.E:empty:没有任何的内容,连空格都没有的E元素。
14.E:target:可以为锚点目标元素添加样式,当目标元素被触发为当前锚链接的目标时,调用此伪类样式
1.E::before:定义在一个元素的内容之前插入content属性定义的内容与样式
2.E::after:定义在一个元素的内容之后插入content属性定义的内容与样式
注意:
<style>
*{
padding: 0;
margin: 0;
}
body{
padding: 200px;
}
div:nth-of-type(1){
width: 300px;
height: 200px;
background-color: red;
float: left;
}
div:nth-of-type(2){
width: 100px;
height: 200px;
background-color: blue;
float: left;
position: relative;
}
div:nth-of-type(2)::before{
content: "";
position: absolute;
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 10px;
left: -10px;
top: -10px;
}
div:nth-of-type(2)::after{
content: "";
position: absolute;
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 10px;
left: -10px;
bottom: -10px;
}
</style>
<body>
<div></div>
<div></div>
</body>
3.E::first-letter:文本的第一个字母或字(应用:首字下沉)
4.E::first-line:文本第一行
5.E::selection:可改变选中文本的样式
HTML5中添加了一些新的颜色的表示方式
1.RGBA(R,G,B,A);
RGBA在RGB的基础上多了控制alpha透明度的参数
RGBA比元素设置CSS的透明度(opacity:0-1,只能针对整个盒子设置透明度,子盒子及内容会继承父盒子的透明度;transparent 不可调节透明度,始终完全透明)更好,它可以不影响整个元素的透明度,也不会影响到元素其他的属性。
语法:
R:红色值。正整数 | 百分数
G:绿色值。正整数 | 百分数
B:蓝色值。正整数| 百分数
A:透明度。取值0~1之间(0:透明;1:不透明)
正整数值的取值范围为:0 - 255。百分数值的取值范围为:0.0% - 100.0%。超出范围的数值将被截至其最接近的取值极限。并非所有浏览器都支持使用百分数值。
红绿==>黄,绿蓝==>青,红蓝==>紫,红绿蓝==>白
eg.background-color: rgba(255,0,255,0.2);
2. HSLA(H,S,L,A):
此色彩模式与相同,只是在模式上新增了Alpha透明度
语法:
H:Hue(色调,色相)。0(或360)表示红色,120表示绿色,240表示蓝色,也可取其他数值来指定颜色。取值为:0 – 360,过渡为:(红橙黄绿青蓝紫红)
S:Saturation(饱和度)。指色彩的纯度,越高色彩越纯,低则逐渐变灰,取值为:0.0% - 100.0%
L:Lightness(亮度)。取值为:0.0% - 100.0%,50%是平衡值,一般建议保留50%
A:Alpha(透明度)。取值0~1之间(0:透明;1:不透明)
eg.background-color: hsla(300,50%,50%,1);
这个属性可以有两个作用,产生阴影和模糊主体。这样在不使用图片时,能给文字增加质感。
语法:(可以多个阴影叠加)
text-shadow:[Color X Offset Y Offset Blur],[Color X Offset Y Offset Blur]...
或者
text-shadow:[X Offset Y Offset Blur Color],[X Offset Y Offset Blur Color]...
说明:
1.在默认情况下,CSS设置的盒子宽度仅仅是内容区的宽度,而非盒子的宽度。同样,高度类似。真正盒子的宽度和高度(在页面呈现出来的):
很明显,这不直观,很容易出错,造成网页中其它元素的错位。
2. CSS3中可以通过box-sizing来指定盒模型,即可指定为content-box、border-box,这样我们计算盒子大小的方式就发生了改变
3.浏览器的兼容性:IE8及以上版本支持该属性,Firefox 需要加上:-moz-,低版本的IOS和Android浏览器需要加上:-webkit-
border-radius可以通过值来定义样式相同的角,也可以对每个角分别定义
单个圆角的设置:除了同时设置四个圆角以外,还可以单独对每个角进行设置。对应四个角,CSS3提供四个单独的属性:
这四个属性都可以同时设置1到2个值。如果设置1个值,表示水平半径与垂直半径相等;如果设置2个值,第一个值表示水平半径,第二个值表示垂直半径。
另一种方法设置圆角:border-radius:水平x方向/垂直y方向
以“/”进行分隔,遵循“1,2,3,4”规则,“/”前面的1~4个用来设置横轴半径(分别对应横轴1、2、3、4位置 ),“/”后面1~4个参数用来设置纵轴半径(分别对应纵轴1、2、3、4位置 )
1.制作椭圆(四个方向上都是一致的对称圆角)
border-top-right-radius: 100px 50px;
border-bottom-left-radius: 100px 50px;
border-bottom-right-radius: 100px 50px;
border-top-left-radius: 100px 50px;
等价:
2.非对称圆角
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Android机器人</title>
<style>
*{
padding: 0;
margin: 0;
}
body{
background-color: #ccc;
}
.content{
width: 500px;
height: 500px;
border: 1px solid red;
margin:50px auto;
}
.an_header{
width: 250px;
height: 125px;
background-color: darkgreen;
margin: 10px auto;
/*添加圆角*/
border-radius: 125px 125px 0 0;
position: relative;
}
/*使用伪元素添加眼睛*/
.an_header:before,
.an_header:after{
/*1.一定设置content属性*/
content: "";
/*2.如果需要设置宽度就应该转换其为块级元素,它默认是行级元素 float display position*/
position: absolute;
bottom: 40px;
width: 20px;
height: 20px;
border-radius: 10px;
background-color: #fff;
}
.an_header:before{
left: 70px;
}
.an_header:after{
right: 70px;
}
.an_body{
width: 250px;
height: 250px;
background-color: darkgreen;
border-radius: 0 0 20px 20px;
margin:0 auto;
position: relative;
}
/*手臂*/
.an_body:before,
.an_body:after{
content: "";
position: absolute;
top: 20px;
background-color: darkgreen;
width: 30px;
height: 180px;
border-radius: 10px;
}
.an_body:before{
left:-40px;
}
.an_body:after{
right:-40px;
}
.an_footer{
width: 250px;
height: 100px;
position: relative;
margin:0 auto;
/* background: #fff;*/
}
/*脚*/
.an_footer:before,
.an_footer:after{
content: "";
position: absolute;
top: 0;
background-color: darkgreen;
width: 30px;
height: 90px;
border-radius: 0 0 10px 10px;
}
.an_footer:before{
left: 50px;
}
.an_footer:after{
right: 50px;
}
</style>
</head>
<body>
<div class="content">
<div class="an_header"></div>
<div class="an_body"></div>
<div class="an_footer"></div>
</div>
</body>
</html>
box-shadow 属性向边框添加一个或多个阴影(添加多个时用逗号隔开)
语法:box-shadow:h v blur spread color inset;
说明:
width: 200px; height: 200px display: block background-color: hsla(360,100%,50%,0 color:
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务