一、结构伪类选择器
伪类:id+class结合
练习1.1ul的第一个元素,ul的最后一个元素
<style>
/*ul的第一个元素*/
ul li:first-child{
background: #c907ff;
}
/*ul的最后一个元素*/
ul li:last-child{
background: brown;
}
</style>
总体代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*ul的第一个元素*/
ul li:first-child{
background: #c907ff;
}
/*ul的最后一个元素*/
ul li:last-child{
background: brown;
}
</style>
</head>
<body>
<p>p0</p>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
练习1.2 选中p1:定位到父元素,选择当前的第一个元素,并且是当前元素才能生效
选择当前p元素的父级元素,选中父级元素的第一个
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个*/
p:nth-child(1){
background: aqua;
}
总体代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*选中p1:定位到父元素,选择当前的第一个元素,并且是当前元素才能生效
选择当前p元素的父级元素,选中父级元素的第一个*/
p:nth-child(1){
background: aqua;
}
</style>
</head>
<body>
<p>p0</p>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
扩展
同样是选择第一个元素,当加了一个h1标签,就没有变化,因此必须是当前元素才能生效。
p:nth-child(1){
background: aqua;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*选中p1:定位到父元素,选择当前的第一个元素,并且是当前元素才能生效
选择当前p元素的父级元素,选中父级元素的第一个 顺序*/
p:nth-child(1){
background: aqua;
}
</style>
</head>
<body>
<h1>h1</h1>
<p>p0</p>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
p:nth-child()和p:nth-of-type()的使用
p:nth-child(2){
background: aqua;
}
因为第二个元素是p标签,所以是有变化的,以下这样就可以
p:nth-of-type这个不受其他标签的影响,括号里是1就是这个标签的第一个元素
/*选中父元素,下的p元素的第一个 类型*/
p:nth-of-type(1){
background: yellow;
}
二、属性选择器(推荐使用)
Id+class结合
总结
= 绝对等于
*= 包含这个元素
^= 以什么什么开头
$= 以什么什么结尾
1、存在Id属性的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left; /*悬浮*/
display: block; /*有框*/
height: 50px;
width: 50px;
border-radius: 10px; /*圆角*/
background: aqua;
text-align: center; /*文本居中*/
color: black; /*字体颜色*/
text-decoration: none; /*去下划线*/
margin-right: 5px; /*外边距向右移动*/
/*line-height: 50px;*/ /*上下居中*/
font: bold 20px/50px Arial;/*上下居中 bold加粗*/
}
/*两种写法:1、属性名 2、属性名 = 属性值*/
/*1、存在Id属性的元素 a[]{}*/
a[id]{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.hao.com" class="item links first" id="first">1</a>
<a href="http://www.baidu.com" class="item links active" target="_blank title=test">2</a>
<a href="images/123.html" class="item links">3</a>
<a href="images/123.png" class="item links">4</a>
<a href="images/123.png" class="item links">5</a>
<a href="abc" class="item links">6</a>
<a href="/a.pdf" class="item links">7</a>
<a href="/abc.pdf" class="item links">8</a>
<a href="abc.doc" class="item links">9</a>
<a href="abcd.doc" class="item links last">10</a>
</p>
</body>
</html>
Id等于first的元素
a[id=first]{
background: antiquewhite;
}
3、class中有link的元素
a[class*="link"]{
background: chartreuse;
}
4、选中href中以http开头的元素
a[href^=http]{
background: cornflowerblue;
}
5、选中href中以doc开头的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left; /*悬浮*/
display: block; /*有框*/
height: 50px;
width: 50px;
border-radius: 10px; /*圆角*/
background: aqua;
text-align: center; /*文本居中*/
color: black; /*字体颜色*/
text-decoration: none; /*去下划线*/
margin-right: 5px; /*外边距向右移动*/
/*line-height: 50px;*/ /*上下居中*/
font: bold 20px/50px Arial;/*上下居中 bold加粗*/
}
/*两种写法:1、属性名 2、属性名 = 属性值*/
/*a[]{}*/
/*5、选中href中以doc开头的元素*/
a[href$=doc]{
background: blueviolet;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.hao.com" class="item links first" id="first">1</a>
<a href="http://www.baidu.com" class="item links active" target="_blank title=test">2</a>
<a href="images/123.html" class="item links">3</a>
<a href="images/123.png" class="item links">4</a>
<a href="images/123.png" class="item links">5</a>
<a href="abc" class="item links">6</a>
<a href="/a.pdf" class="item links">7</a>
<a href="/abc.pdf" class="item links">8</a>
<a href="abc.doc" class="item links">9</a>
<a href="abcd.doc" class="item links last">10</a>
</p>
</body>
</html>
三、CSS的作用及字体样式
3.1美化网页元素
为什么要美化网页?
- 有效的传递页面信息
- 美化网页,页面漂亮才能吸引用户
- 凸显页面的主题
- 提高用户的体验
3.2 span标签:重点要突出的字,使用span 套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习 <span id="title">Java</span>
</body>
</html>
3.3字体样式
font-family字体样式,可以同时存在两种以上字体
font-size字体大小
font-weight字体粗细,bold为粗体,lighter为细体
color 字体颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--font-family字体样式,可以同时存在两种以上字体
font-size字体大小
font-weight字体粗细,bold为粗体,lighter为细体
color 字体颜色
-->
<style>
body{
font-family: "Adobe Devanagari", 华文楷体;
color: green;
}
h1{
font-size: 50px;/*这个后面的单位也可以写em ,表示是缩进字符*/
}
.p1{
font-weight: bold;/*这个后面也可以直接跟数字*/
}
</style>
</head>
<body>
<h1>故事介绍</h1>
<p class="p1">
熊大和熊二到森林中采摘水果,可到家才发现,辛辛苦苦摘到的水果都不见了。
“熊二不是让你拿好吗!?”熊大愤怒的说。“为什么所有的错都怪我...” 熊二很委屈。
</p>
<p>
找不回全部的水果不能回家哦!太可怜了。快来帮帮他们吧!熊大喜欢苹果,熊二喜欢鸭梨,
收集全部的水果回家,即可成功过关。吃了神奇药水,熊变大后可以踩死野猪,普通状态碰到野猪会死哦,小心!
</p>
<p>
If you were a teardrop;In my eye,
For fear of losing you,I would never cry.
And if the golden sun,Should cease to shine its light,
Just one smile from you,Would make my whole world bright.
</p>
</body>
</html>
斜体的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*oblique斜体 bolder粗体 字体大小 "楷体"字体样式*/
p{
font: oblique bolder 24px "楷体";
}
</style>
</head>
<body>
<p>
熊大和熊二到森林中采摘水果,可到家才发现,辛辛苦苦摘到的水果都不见了。
“熊二不是让你拿好吗!?”熊大愤怒的说。“为什么所有的错都怪我...” 熊二很委屈。
</p>
</body>
</html>