07-CSS浮动

1、结构伪类选择器

1、作用:结构伪类选择器的作用与优势

  1. 作用:根据元素在HTML中的结构关系查找元素
  2. 优势:减少对于HTML的依赖,有利于保持代码整洁
  3. 场景:常用于查找某父级选择器的子元素

2、结构伪类选择器

image.png
结构伪类选择器中的n可以组成常见的公式
image.png
代码

<!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>

展示
image.png

2、伪元素

作用:伪元素一般页面中的非主体内容可以使用伪元素
区别

  1. 元素:HTML设置的标签
  2. 伪元素:由CSS模拟出来的标签效果

种类:
image.png
注意点:

  1. 必须设置content属性才能生效
  2. 伪元素默认是行内元素

3、标准流

1、标准流:又称文档流,是浏览器在渲染显示网页内容时默认采用的一套排版规则,规定了应该以何种方式排列元素
2、常见的标准流排版规则:

  1. 块元素:从上往下,垂直布局,独占一行
  2. 行内元素或行内块元素,从左往右,水平布局,空间不够自动折行

4、浮动

1、浮动的作用

  1. 早期图文环绕

image.png

  1. 现在的作用:网页布局

image.png

2、浮动的代码

属性名效果
float:left左浮动
float:right右浮动

3、浮动的特点

  1. 浮动的元素会脱离标准流,在标准流中不占位置,相当于飘到空中
  2. 浮动元素标准流高半个级别,可以覆盖标准流中的元素
  3. 浮动找浮动,下一个浮动元素会在上一个浮动元素后面左右浮动
  4. 浮动元素有特殊的显示效果
    1. 一行可以显示多个
    2. 可以设置宽高
  5. 注意点
    1. 浮动的元素不能通过text-aligen:center或者margin:0 auto

4、浮动的案例

  1. 需求:使用浮动,完成设计图中布局效果

image.png

  1. 需求:使用浮动,完成设计图中的布局效果
<!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>

image.png

<!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
  1. 网页导航案例

image.png

<!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、为什么要清除浮动

  1. 如果子元素浮动了,父级没有高度,此时子元素不能撑开标准流的块级元素。因为子元素浮动后脱标后不占位置

展示
image.png
2、解决浮动带来的影响

  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>
        .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>
  1. 额外标签法
    1. 在父元素内容的最后添加一个块级元素
    2. 给添加的块级元素设置clear:both
    3. 缺点会在页面添加额外的标签,会让页面的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>
  1. 单伪元素清除法
    1. 补充写法 是为了兼容性来考虑的,把标签进行隐藏

image.png

<!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>
  1. 双伪元素清除法
    1. 把盒子转为表格的显示模式
    2. 最后一行代码才是清除浮动的标签
    3. clerafix::before 为了解决外边距塌陷问题
    4. clerafix::after 中的display:table 也是为了解决外边距塌陷

image.png

<!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>
  1. 清除浮动可以使用overflow:hidden
    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;
            /* 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值