1、结构伪类选择器
1、作用:结构伪类选择器的作用与优势
- 作用:根据元素在HTML中的结构关系查找元素
- 优势:减少对于HTML的依赖,有利于保持代码整洁
- 场景:常用于查找某父级选择器的子元素
2、结构伪类选择器
结构伪类选择器中的n可以组成常见的公式
代码
<!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:first-child{
/* 选中第一个 */
color: red;
}
li:last-child{
/* 选中最后一个 */
color: green;
}
li:nth-child(3){
/* 选中第三个 */
color: aqua;
}
li:nth-last-child(1){
/* 选中倒数第一个 */
color: antiquewhite;
}
</style>
</head>
<body>
<ul>
<li>这是第1个li</li>
<li>这是第2个li</li>
<li>这是第3个li</li>
<li>这是第4个li</li>
<li>这是第5个li</li>
<li>这是第6个li</li>
<li>这是第7个li</li>
<li>这是第8个li</li>
</ul>
</body>
</html>
展示
2、伪元素
作用:伪元素一般页面中的非主体内容可以使用伪元素
区别
- 元素:HTML设置的标签
- 伪元素:由CSS模拟出来的标签效果
种类:
注意点:
- 必须设置content属性才能生效
- 伪元素默认是行内元素
3、标准流
1、标准流:又称文档流,是浏览器在渲染显示网页内容时默认采用的一套排版规则,规定了应该以何种方式排列元素
2、常见的标准流排版规则:
- 块元素:从上往下,垂直布局,独占一行
- 行内元素或行内块元素,从左往右,水平布局,空间不够自动折行
4、浮动
1、浮动的作用
- 早期图文环绕
- 现在的作用:网页布局
2、浮动的代码
属性名 | 效果 |
---|---|
float:left | 左浮动 |
float:right | 右浮动 |
3、浮动的特点
- 浮动的元素会脱离标准流,在标准流中不占位置,相当于飘到空中
- 浮动元素标准流高半个级别,可以覆盖标准流中的元素
- 浮动找浮动,下一个浮动元素会在上一个浮动元素后面左右浮动
- 浮动元素有特殊的显示效果
- 一行可以显示多个
- 可以设置宽高
- 注意点
- 浮动的元素不能通过text-aligen:center或者margin:0 auto
4、浮动的案例
- 需求:使用浮动,完成设计图中布局效果
- 需求:使用浮动,完成设计图中的布局效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.head{
width: 100%;
height: 40px;
background-color: #333333;
}
.box{
width: 1226px;
height: 560px;
background-color: yellow;
margin: 0 auto;
}
.box ul{
list-style: none;
}
li:first-child{
display: block;
width: 1226px;
height: 100px;
background-color: #ffc0cb;
}
li:nth-child(2){
display: inline-block;
height: 460px;
width: 234px;
background-color: #ffa500;
}
li:nth-child(3){
display: inline-block;
height: 460px;
width: 992px;
background-color: #87ceeb;
float: right;
}
</style>
</head>
<body>
<div class="head"></div>
<div class="box">
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 1235px;
height: 614px;
margin: 0 auto;
}
ul li{
list-style: none;
}
.box{
display:inline-block;
width: 234px;
height: 614px;
background-color: #800080;
overflow: hidden;
float: left;
margin-top: 14px;
}
li:nth-child(n){
display:inline-block;
width: 234px;
height: 300px;
float: right;
background-color: #87ceeb;
margin-top: 14px;
margin-left: 14px;
}
li:nth-child(4n) {
margin-right: 0;
}
</style>
</head>
<body>
<div>
<ul>
<div class="box"></div>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>32
- 网页导航案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
color: aliceblue;
display: inline-block;
width: 80px;
height: 50px;
background-color: #ffc0cb;
font-size: 16;
float: left;
line-height: 50px;
text-indent: 1em;
}
a:hover{
background-color: #008000;
}
</style>
</head>
<body>
<a href="">新闻1</a>
<a href="">新闻2</a>
<a href="">新闻3</a>
<a href="">新闻4</a>
<a href="">新闻5</a>
<a href="">新闻6</a>
<a href="">新闻7</a>
<a href="">新闻8</a>
<a href="">新闻9</a>
</body>
</html>
4、清除浮动
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>
.top {
margin: 0 auto;
width: 1000px;
height: 300px;
background-color: pink;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
- 额外标签法
- 在父元素内容的最后添加一个块级元素
- 给添加的块级元素设置clear:both
- 缺点会在页面添加额外的标签,会让页面的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>
.top {
margin: 0 auto;
width: 1000px;
/* height: 300px; */
background-color: pink;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix{
clear: both;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="bottom"></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>
.top {
margin: 0 auto;
width: 1000px;
/* height: 300px; */
background-color: pink;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix::after{
content: '';
display: block;
clear: both;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
- 双伪元素清除法
- 把盒子转为表格的显示模式
- 最后一行代码才是清除浮动的标签
- clerafix::before 为了解决外边距塌陷问题
- clerafix::after 中的display:table 也是为了解决外边距塌陷
<!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>
.top {
margin: 0 auto;
width: 1000px;
/* height: 300px; */
background-color: pink;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix::before,
.clearfix::after{
content: '';
display: table;
}
.clearfix::after{
clear: both;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
- 清除浮动可以使用overflow:hidden
- 优点:给父元素添加即可
<!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>
.top {
margin: 0 auto;
width: 1000px;
/* height: 300px; */
background-color: pink;
overflow: hidden;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top ">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>