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>
-
-
-
-
-
-
-
-
-
-
-
-
-
- li:nth-last-child(1) {
- background-color: blue;
- }
- </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>
-
- <!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:nth-child(4n) {
- background-color: green;
- }
- </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>
-
1.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>
-
- li:first-child a:nth-child(3) {
- color: red;
- }
- </style>
- </head>
- <body>
- <ul>
- <li>
- <a href="#">这是第1个li里面的a1</a>
- <a href="#">这是第1个li里面的a2</a>
-
- <a href="#">这是第1个li里面的a3</a>
- <a href="#">这是第1个li里面的a4</a>
- <a href="#">这是第1个li里面的a5</a>
- </li>
- <li><a href="#">这是第2个li里面的a</a></li>
- <li><a href="#">这是第3个li里面的a</a></li>
- <li><a href="#">这是第4个li里面的a</a></li>
- <li><a href="#">这是第5个li里面的a</a></li>
- <li><a href="#">这是第6个li里面的a</a></li>
- <li><a href="#">这是第7个li里面的a</a></li>
- <li><a href="#">这是第8个li里面的a</a></li>
- </ul>
- </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>
- .father {
- width: 300px;
- height: 300px;
- background-color: pink;
- }
-
- .father::before {
-
-
- content: '';
- color: green;
- width: 100px;
- height: 100px;
- background-color: blue;
-
-
- display: block;
- }
-
- .father::after {
- content: '大米';
- }
- </style>
- </head>
- <body>
-
-
-
- <div class="father">爱</div>
-
-
- </body>
- </html>
-
3 标准流
4 浮动
4.1 浮动的作用
4.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: 100px;
- height: 100px;
- }
-
- .one {
- background-color: pink;
- float: left;
- }
-
- .two {
- background-color: skyblue;
-
-
- float: left;
- }
- </style>
- </head>
- <body>
-
-
-
-
-
-
- <div class="one">one</div>
- <div class="two">two</div>
- </body>
- </html>
-
4.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>
-
-
- .one {
- width: 100px;
- height: 100px;
- background-color: pink;
-
- float: left;
-
- margin-top: 50px;
- }
-
- .two {
- width: 200px;
- height: 200px;
- background-color: skyblue;
-
- float: left;
-
-
- margin: 0 auto;
- }
-
- .three {
- width: 300px;
- height: 300px;
- background-color: orange;
-
- }
- </style>
- </head>
- <body>
- <div class="one">one</div>
- <div class="two">two</div>
- <div class="three">three</div>
- </body>
- </html>
-
4.4 浮动的案例
4.4.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>
- * {
- margin: 0;
- padding: 0;
- }
-
- .top {
-
- height: 40px;
- background-color: #333;
- }
-
- .header {
- width: 1226px;
- height: 100px;
- background-color: #ffc0cb;
- margin: 0 auto;
- }
-
- .content {
- width: 1226px;
- height: 460px;
- background-color: green;
- margin: 0 auto;
- }
-
- .left {
- float: left;
-
- width: 234px;
- height: 460px;
- background-color: #ffa500;
-
-
- }
-
- .right {
- float: left;
-
-
- width: 992px;
- height: 460px;
- background-color: #87ceeb;
-
-
- }
-
- .li{
- height: 500px;
- background-color: #800080;
- }
-
-
-
- </style>
- </head>
- <body>
-
- <div class="top"></div>
- <div class="header">头部</div>
- <div class="content">
- <div class="left">left</div>
- <div class="right">right</div>
- </div>
- <div class="li">right</div>
- </body>
- </html>
-
4.4.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>
- * {
- margin: 0;
- padding: 0;
- }
-
- .box {
- margin: 0 auto;
-
- width: 1226px;
- height: 614px;
-
- }
-
- .left {
- float: left;
-
- width: 234px;
- height: 614px;
- background-color: #800080;
- }
-
- .right {
- float: right;
-
- width: 978px;
- height: 614px;
-
- }
-
- ul {
-
-
- list-style: none;
- }
-
- .right li {
- float: left;
-
- margin-right: 14px;
- margin-bottom: 14px;
-
- width: 234px;
- height: 300px;
- background-color: #87ceeb;
- }
-
-
-
- .right li:nth-child(4n) {
- margin-right: 0;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <div class="left"></div>
- <div class="right">
- <ul>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- </ul>
- </div>
- </div>
- </body>
- </html>
-
4.4.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>
- * {
- margin: 0;
- padding: 0;
- }
-
- .nav {
- margin: 50px auto;
- width: 640px;
- height: 50px;
- background-color: #ffc0cb;
- }
-
- ul {
- list-style: none;
- }
-
- .nav li {
- float: left;
- }
-
- .nav li a {
-
-
- display: block;
-
-
- width: 80px;
- height: 50px;
-
-
-
- text-align: center;
- line-height: 50px;
- color: #fff;
- text-decoration: none;
- }
-
- .nav li a:hover {
- background-color: green;
- }
- </style>
- </head>
- <body>
-
- <div class="nav">
- <ul>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- <li><a href="#">新闻</a></li>
- </ul>
- </div>
- </body>
- </html>
-
5 清除浮动
5.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>
- .top {
- margin: 0 auto;
- width: 1000px;
-
-
- 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>
-
5.2 清除浮动的方法
5.2.1 直接设置父元素高度
5.2.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;
-
- 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>
-
5.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>
- .top {
- margin: 0 auto;
- width: 1000px;
-
- 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;
-
-
- height: 0;
- visibility: hidden;
- }
- </style>
- </head>
- <body>
-
- <div class="top clearfix">
- <div class="left"></div>
- <div class="right"></div>
-
- </div>
- <div class="bottom"></div>
- </body>
- </html>
-
5.2.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>
- .top {
- margin: 0 auto;
- width: 1000px;
-
- 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>
-
5.2.5 给父元素设置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;
-
- 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>
-