CSS3新特性
1、强大的CSS选择器
2、新的颜色制式和透明设定
3、多栏布局的实现
4、多背景图效果
5、文字阴影效果
6、开放的网络字体类型
7、圆角
8、边框背景图片
9、盒子阴影
10 、媒体查询
浏览器支持情况
新增选择器
- 新增选择器(掌握 4种)
- 属性选择器(掌握)
- 伪类选择器(掌握 讲过)
- UI元素伪类选择器(优先级低一些)
- 伪元素选择器(了解,自学)
通配选择器
-
选择所有
E>F 子选择器 (儿子,直接子类 )<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
h1> span{
color: #8A2BE2;
}
</style>
</head>
<body>
<h1>This is a <span>important</span><p><span>p---------important</span></p> heading</h1>
</body>
</html>
效果:
试着将大于号改为空格试试效果
E+F 相邻 紧挨着e的f元素 (第一个兄弟)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#id1+p{
color: red;
}
</style>
</head>
<body>
<p id="id1">11111111</p>
<p>22222</p>
<p>333333</p>
<p>444444</p>
</body>
</html>
E~F 通用选择器 E后面所有的F (所有兄弟)
上面的代码把+修改成~试试效果
属性选择器
根据指定名称的属性的值查找元素
1、li[class=red] 查询class属性名为red的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
li[class=red]{
text-decoration: underline;
color: red;
}
</style>
</head>
<body>
<ul>
<li class="red">red</li>
<li class="blue">red</li>
<li class="red">red</li>
<li class="black">red</li>
<li class="white">red</li>
</ul>
</body>
</html>
2、li[class*=red] 包含即可
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
/* li[class=red]{
text-decoration: underline;
color: red;
} */
li[class*=red]{
text-decoration: underline;
color: red;
}
</style>
</head>
<body>
<ul>
<li class="red">red</li>
<li class="blue">red</li>
<li class="red">red</li>
<li class="black">red</li>
<li class="white">red</li>
</ul>
<ul>
<li class="red1">red</li>
<li class="01red">red</li>
<li class="red2">red</li>
<li class="02red">red</li>
</ul>
</body>
</html>
学生练习
3、li[class^=red] 以red开头的
4、li[class$=red] 以red开头结尾
伪类选择器(前面章节讲过,此处略)
动态伪类选择器(掌握 一般用在链接,指定链接在不同时机下的样式,比如说链接被点击前,鼠标悬停时,被激活时,被点击后等等)
E:link 链接没有被访问过
E:visited 链接被访问过
E:active 激活时 从悬停到释放之间,按往鼠标不释放 (按住的瞬间)
E:hover 悬停
E:focus 获取焦点
正确的书写顺序: a:link、a:visited、a:hover、a:active
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/* a:link、a:visited、a:hover、a:active */
a:link {
color: black;
text-decoration: none
}
a:visited {
color: black
}
a:hover {
color: green
}
a:active {
color: red
}
</style>
</head>
<body>
<a href="#" target="_blank">测试伪类</a>
</body>
</html>
UI元素伪类选择器
E:checked 匹配用户界面上处于选中状态的元素E(input type=radio/ checkbox)
E:enabled 匹配用户界面上处于可用状态的元素
E:disabled 匹配用户界面上处于禁用状态的元素
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>CSS 用户界面(UI)元素状态伪类选择符 checked选择器-CSS教程</title>
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<style>
input:checked+span{background:#f00;}
input:checked+span::after{content:" 我被选中了";}
</style>
</head>
<body>
<form method="post" action="#">
<fieldset>
<legend>选中下面的项试试</legend>
<ul>
<li><label><input type="radio" name="colour-group" value="0" /><span>蓝色</span></label></li>
<li><label><input type="radio" name="colour-group" value="1" /><span>红色</span></label></li>
<li><label><input type="radio" name="colour-group" value="2" /><span>黑色</span></label></li>
</ul>
</fieldset>
<fieldset>
<legend>选中下面的项试试</legend>
<ul>
<li><label><input type="checkbox" name="colour-group2" value="0" /><span>蓝色</span></label></li>
<li><label><input type="checkbox" name="colour-group2" value="1" /><span>红色</span></label></li>
<li><label><input type="checkbox" name="colour-group2" value="2" /><span>黑色</span></label></li>
</ul>
</fieldset>
</form>
</body>
</html>
结构伪类选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
li:first-child{
color:blue;
}
</style>
</head>
<body>
<ul>
<li class="red">red</li>
<li class="blue">red</li>
<li class="red">red</li>
<li class="black">red</li>
<li class="white">red</li>
</ul>
<ul>
<li class="red1">red</li>
<li class="01red">red</li>
<li class="red2">red</li>
<li class="02red">red</li>
</ul>
</body>
</html>
其他的学生练习
伪元素选择器(建议了解-自学)
1、伪元素选择器与content配合使用
<style type="text/css">
p::before {
content: "《";
color: blue;
}
p::after {
content: "》";
color: blue;
}
</style>
<body>
<p>平凡的世界</p>
</body>
2、如果想为伪元素设置宽高,必须把元素变为块级元素display:block 或position或float
例子:制作如下效果
step1-先构建基本div
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.child01{
width:200px;
height: 100px;
float:left;
background-color: red;
}
.child02{
width:100px;
height: 100px;
float:left;
background-color: blue;
}
</style>
</head>
<body>
<div class="child01">
</div>
<div class="child02">
</div>
</body>
</html>
step2:.child02前后加伪元素,调整位置和设定效果
<style type="text/css">
.child01{
width:200px;
height: 100px;
float:left;
background-color: red;
}
.child02{
width:100px;
height: 100px;
float:left;
background-color: blue;
position: relative;
}
.child02::before{
/* 必须添加*/
content:"";
/*转换成块级元素*/
position: absolute;
top:-10px;
left:-10px;
width:20px ;
height: 20px;
background-color: white;
border-radius: 10px;
}
.child02::after{
/* 必须添加*/
content:"";
/*转换成块级元素*/
position: absolute;
bottom:-10px;
left:-10px;
width:20px ;
height: 20px;
background-color: white;
border-radius: 10px;
}
</style>
关键代码:
::first-letter第一个字符
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
p::first-letter{
color:red;
}
</style>
</head>
<body>
<p>UP主自己创建的前端交流群:865712069,有时会分享一些课程资料,for free。</p>
</body>
</html>
::first-line
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
p::first-letter{
color:red;
}
.con:first-line{
color:blue;
}
</style>
</head>
<body>
<p>UP主自己创建的前端交流群:865712069,有时会分享一些课程资料,for free。
</p>
<div class="con">UP主自己创建的前端交流群:865712069,有时会分享一些课程资料,for free。<br>
UP主自己创建的前端交流群:865712069,有时会分享一些课程资料,for free。
</div>
</body>
</html>
效果:
::selection
改变元素被选中时的样式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
p::selection{
background-color: red;
}
</style>
</head>
<body>
<p>UP主自己创建的前端交流群:865712069,有时会分享一些课程资料,for free。</p>
</body>
</html>
用鼠标选中部分文本看样式