CSS学习笔记二
1. 选择器进阶
复合选择器
后代选择器(注意空格)
1.相关介绍
2. 演示
<!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>
div p{
color:brown;
}
</style>
</head>
<body>
<p>这是一个p标签</p>
<div>
<p>这是div的儿子p</p>
</div>
</body>
</html>
显示效果:
子代选择器(大于号)
1.相关知识
2. 演示
<!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>
div>a{
color: red;
}
</style>
</head>
<body>
<div>
父级
<a href="#">这是div里面的a</a>
<p>
<a href="#">这是div里面的p里面的a</a>
</p>
</div>
</body>
</html>
显示效果:
并集选择器(,)
1.相关知识
2.演示
<!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>
p,
div,
span,
h1{
color: red;
}
</style>
</head>
<body>
<p>ppp</p>
<div>div</div>
<span>span</span>
<h1>h1</h1>
<h1>h1=h1</h1>
</body>
</html>
显示效果:
交集选择器
1.相关知识
2.演示
<!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{
font-size: 14px;
}
p.box{
color: red;
}
</style>
</head>
<body>
<p class="box">这是p标签:box</p>
<p>pppp</p>
<div class="box">这是div标签:box</div>
</body>
</html>
显示效果:
hover伪类选择器(:)
1.相关知识
注意:任何标签都可以添加伪类,任何一个标签都可以鼠标悬停。
2. 演示
<!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>
a:hover{
color:red;
background-color: blue;
}
</style>
</head>
<body>
<a href="#">超链接</a>
</body>
</html>
显示效果:
Emment语法
1.相关知识
(1) div+p
同级。
(2)提示css属性,单词的首字母;如fw700=font-width:700
。
2.演示
ul>li{$}*3
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
w300+h200+bgc
width: 300px;
height: 200px;
background-color: #fff;
2. 背景相关属性
背景颜色
1.background
(bgc),其取值和color一样可以采用四种方式(关键字,rgb表示法,rgba表示法,十六进制)
2.背景颜色默认值是透明:rgba(0,0,0,.5)
。(0省略)
3.背景颜色不会影响盒子大小,并且还能看清盒子的大小和位置,一般布局中习惯先给盒子设置背景颜色。
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>
div{
background-color: pink;
}
</style>
</head>
<body>
<div>div</div>
</body>
</html>
显示效果:
背景图片
1.background-image: url(./images/cat.png)
,./images/cat.png
为背景图片路径。
2.背景图片默认是在水平和垂直方向平铺的。
3.演示
<!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>
div{
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./images/cat.png);
}
</style>
</head>
<body>
<div>嘤嘤嘤</div>
</body>
</html>
显示效果:
背景平铺
例子:
<!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>
div{
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./images/cat.png);
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div>嘤嘤嘤</div>
</body>
</html>
背景位置
1.background-position:center center;
可以省略为background-position:center;
2.例子(若x给负值,往左走;若y给负值,往上走;)
<!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>
div{
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./images/cat.png);
background-repeat: no-repeat;
background-position:-50px -100px;
}
</style>
</head>
<body>
<div>嘤嘤嘤</div>
</body>
</html>
显示效果:
背景相关属性
- 推荐书写顺序(也可以不按这个顺序来):
background:color image repeat position
。 - 背景图位置如果是英文单词可以颠倒顺序,数字不能颠倒顺序。
- 可以按需省略。
- 例子
<!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>
div{
width: 400px;
height: 400px;
/*推荐顺序*/
/*background: pink url(./images/cat.png) no-repeat center bottom ;*/
background:center pink url(./images/car.jpg) no-repeat ; /*不是推荐顺序也可以*/
}
</style>
</head>
<body>
<div>嘤嘤嘤</div>
</body>
</html>
显示效果:
注意:img
用来显示网页中比较重要的图片,不重要的背景图片(如:修饰作用的)。
3. 元素显示模式 <>
块级元素
1.独占一行(一行只显示一个),可以设置宽高。
2.包括:div、p、h系列,ul、li 、dl、dt、dd、 form、 header、 nav、 footer...
行内元素
1.一行可以显示多个。宽度高度默认由内容撑开,不可设置宽高,但背景可以设置。
2.包括:a 、span、 b 、u、i、 s、 striong、 ins、 em、 del...
。
行内块元素
1.一行可以显示多个,加宽高可以生效。
2.包括:input 、textarea 、button 、select...
。
特殊情况:img标签有行内块元素的特点,但是Chrome调试工具中显示结果是inline
元素显示模式转换
1.相关知识
2.例子:
<!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>
div{
width: 200px;
height: 100px;
display: inline-block;
background: pink;
}
</style>
</head>
<body>
<div>pp</div>
<div>ppp</div>
</body>
</html>
显示效果:
3.嵌套规范
(1)块级元素一般作为大容器,可以嵌套:文本、块级元素、行内元素、行内块元素等等…。(但是p标签中不要嵌套div、p、h等块级元素)
(2)a标签内部可以嵌套任何元素,但是不能嵌套a标签。
4. CSS特性
继承性
1.子元素有默认夫元素样式的特点。
2.文字控制属性都可以继承,控制标签本身的一律不能继承。
3.如果本来就有颜色,就不会继承父亲的颜色,比如超链接。如果本来就有字号大小,也不会基础父亲的,比如h系列。
层叠性
1.相关知识
优先级
- 谁的范围广,谁的优先级低。
- 例子
<!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>
/*id 选择器*/
#ox{
color: orange;
}
/*类选择器*/
.box{
color: blue;
}
/*标签选择器*/
div{
color: green !important;
}
/*继承*/
body{
color: red;
}
</style>
</head>
<body>
<div class="box" id="ox" style="color: brown; font-size:32px ">你好</div>
</body>
</html>
显示结果如下:
3. 权重叠加计算
例子:
<!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>
/*(0,1,0,1)*/
div #one{
color: orange;
}
/*(0,0,2,0)*/
.father .son{
color: blue;
}
/*(0,0,1,1)*/
.father p{
color: green;
}
/*(0,0,0,2)*/
div p{
color: red;
}
</style>
</head>
<body>
<div class="father">
<p class="son" id="one"> 你好</p>
</div>
</body>
</html>
显示如下:
5. 综合案例
alt+shift
可以选多行添加或者删除内容。
导航1
<!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>
a{
text-decoration: none;
width: 100px;
height: 50px;
background-color: red;
display: inline-block; /*转行内块*/
color:white;
text-align: center;
line-height: 50px;
}
a:hover{
background-color: orange;
}
</style>
</head>
<body>
<a href="#">导航1</a>
<a href="#">导航2</a>
<a href="#">导航3</a>
<a href="#">导航4</a>
<a href="#">导航5</a>
</body>
</html>
导航2
<!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>
a{
text-decoration: none;
width: 120px;
height: 58px;
background-color: pink;
display: inline-block;
text-align: center;
line-height: 50px;
color: #fff;
}
.one{
background-image: url(./images/bg1.png);
}
.two{
background-image: url(./images/bg2.png);
}
.three{
background-image: url(./images/bg3.png);
}
.four{
background-image: url(./images/bg4.png);
}
.one:hover{
background-image: url(./images/bg5.png);
}
.two:hover{
background-image: url(./images/bg6.png);
}
.three:hover{
background-image: url(./images/bg7.png);
}
.four:hover{
background-image: url(./images/bg8.png);
}
</style>
</head>
<body>
<a href="" class="one">五彩导航</a>
<a href="" class="two">五彩导航</a>
<a href="" class="three">五彩导航</a>
<a href="" class="four">五彩导航</a>
</body>
</html>