(1) 在html中使用样式#
- 内联样式
- 内部样式
- 外部样式
1-2内联样式和内部样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 内部样式 */
.box {
color: green;
font-size: 30px;
}
</style>
</head>
<body>
<!-- 内联样式 -->
<div style="border:1px solid;color:red;">
内联样式
</div>
<div class="box">
内部样式
</div>
</body>
</html>
3 外部样式
<!-- demo.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="./demo.css">
</head>
<body>
<!-- 内联样式 -->
<div style="border:1px solid;color:red;">
内联样式
</div>
<div class="box">
内部样式
</div>
</body>
</html>
<!-- demo.css -->
.box {
border:1px solid red;
color: red;
}
(2) 几个简单的css样式#
- border 边框
- width, height 宽高
- Background-color 背景颜色
- font-size 字体大小
<!DOCTYPE html>
<html lang="en">
<style>
div {
border: 1px solid;
font-size: 30px;
width: 300px;
height: 300px;
background-color: grey;
}
</style>
<body>
<div>
我爱web
</div>
</body>
</html>
(3) 元素选择器#
常用选择器
- 元素选择器
- id选择器
- 类选择器
- 群组选择器
- 后代选择器
- 子代选择器
- 兄弟选择器
- nth(n) 选择器
- :first-child (伪类选择器)
- :last-child (伪类选择器css3新增)
- :hover (伪类选择器)
- :visited (伪类选择器)
- :after (伪类选择器)
- css3新增选择器
1.元素选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div {
border: 1px solid;
}
p {
background-color: green;
}
</style>
</head>
<body>
<div>
div1
</div>
<div>
div2
</div>
<p>
pppppppp
</p>
<p>2342342</p>
</body>
</html>
2.id选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div1 {
color: red;
}
#div2 {
color: green;
}
</style>
</head>
<body>
<div id="div1">
div1
</div>
<div id="div2">
div2
</div>
</body>
</html>
3.class选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.div1 {
color: red;
}
.div2 {
color: blue;
}
</style>
</head>
<body>
<div class="div1">
div1
</div>
<div class="div2">
div2
</div>
<p class="div1">
pppppppp
</p>
</body>
</html>
4.群组选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* .box {
color: red;
}
#aa {
color: red;
}
p {
color: red;
} */
/* 以上可以使用群组选择器 */
.box,#aa,p {
color: red;
}
</style>
</head>
<body>
<div class="box">
div1
</div>
<div id="aa">
div2
</div>
<p>
pppp
</p>
</body>
</html>
5.后代选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 后代选择器 */
.box1 .aa {color: red;}
.box2 li {color: green;}
</style>
</head>
<body>
<ul class="box1">
<li class="aa">111</li>
<li class="aa">111</li>
<li class="aa">111</li>
</ul>
<ul class="box2">
<li class="aa">2222</li>
<li class="aa">2222</li>
<li class="aa">2222</li>
</ul>
</body>
</html>
6. 子代选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.box>.aa {
border: 1px solid;
width: 300px;
}
.box>p.aa {
color: red;
}
</style>
</head>
<body>
<div class="box">
<div class="aa">
<i>iiiii</i>
<span>span</span>
</div>
<p class="aa">
这是p标签
</p>
</div>
</body>
</html>
7. 兄弟选择器#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.pp+.span {
color: red;
}
</style>
</head>
<body>
<h3>把.pp标签的下一个兄弟元素字体变成红色</h3>
<div class="box">
<div class="div">div标签</div>
<p class="pp">p标签</p>
<span class="span">span标签</span>
<i>i标签</i>
</div>
</body>
</html>
<!-- 一个小技巧:除了第一个li,其它li加上边框 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li+li {
border-bottom:1px solid #999;
width: 100px;
}
</style>
</head>
<body>
<ul>
<li>1xxx</li>
<li>2xxx</li>
<li>3xxx</li>
<li>4xxx</li>
<li>5xxx</li>
</ul>
</body>
</html>
伪类选择器
8. :nth(n) 选择器#
<!DOCTYPE html>
<html lang="en">
<style>
.item:nth-child(1) {
color:red;
}
.item:nth-child(2) {
color:gold;
}
.item:nth-child(3) {
color:greenyellow;
}
</style>
<body>
<div class="box">
<p class="item">xxxx</p>
<p class="item">xxxx</p>
<p class="item">xxxx</p>
</div>
</body>
</html>
<!-- 奇数和偶数 -->
<!DOCTYPE html>
<html lang="en">
<style>
/* 奇数 */
.item:nth-child(odd) {
color: blue;
}
/* 偶数 */
.item:nth-child(even) {
color: red;
}
</style>
<body>
<div class="box">
<p class="item">xxxx</p>
<p class="item">xxxx</p>
<p class="item">xxxx</p>
<p class="item">xxxx</p>
</div>
</body>
</html>
9. :first-child (第一个)#
10. :last-child (最后一个 css3新增)#
<!DOCTYPE html>
<html lang="en">
<style>
li.item:first-child{
color: red;
}
li.item:last-child {
color: blue;
}
</style>
<body>
<ul class="list">
<li class="item">xxxx</li>
<li class="item">xxxx</li>
<li class="item">xxxx</li>
<li class="item">xxxx</li>
<li class="item">xxxx</li>
</ul>
</body>
</html>
11. :hover (移动到元素上)#
12. :visited (访问过)#
<!DOCTYPE html>
<html lang="en">
<style type="text/css">
/* 未访问的链接 */
a:link {
color: #000;
}
/* 已访问的链接 */
a:visited {
color: #F00;
}
/* 鼠标在链接上 */
a:hover {
color: #0F0;
}
/* 点击元素 */
a:active {
color: #00F;
}
</style>
<body>
<a href="http://www.baidu.com" target="_blank">百度</a>
<a href="http://www.sina.com" target="_blank">新浪</a>
</body>
</html>
13. :after (伪类选择器)#
<!DOCTYPE html>
<html>
<head>
<style>
p {
width: 200px;
height: 200px;
border: 1px solid;
}
p:after {
content: "疯狂编码中...";
}
</style>
</head>
<body>
<p></p>
</body>
</html>
(4) 选择器优先级#
样式可以继承, 相同的设置会覆盖
- 内联样式 > 内部样式 > 外部样式 (就近原则)
- !important > id选择器 > class选择器 > 元素选择器 > 通配符(*)选择器
- 都是class的情况下, 一般选择器越"长", 优先级越高
- !important能不用就不用, 因为用不好对维护会产生影响
<!-- 内联样式 > 内部样式 > 外部样式 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="./demo6.css">
<style>
/* 内部样式 */
.box {
font-size: 30px;
border: 1px solid red;
}
</style>
</head>
<body>
<!-- 内联样式 -->
<div class="box" style="color: red;font-size: 10px;">
box
</div>
</body>
</html>
<!-- !important > id选择器 > class选择器 > 元素选择器 > 通配符选择器 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 将!important放在样式的后面, 不管它是什么选择器都会优先使用 */
* {
color: white!important;
}
#box {
color: blue;
}
.box {
color: green;
}
/* 元素选择器 */
div {
color: red;
}
</style>
</head>
<body>
<div id="box" class="box">
div
</div>
</body>
</html>