HTMLday0619

1. 浮动(float)

CSS 布局的三种机制

CSS 提供了 3 种机制来设置盒子的摆放位置,分别是普通流(标准流)、浮动定位

普通流(标准流)

        块级元素会独占一行,从上向下顺序排

        行内元素会按照顺序,从左到右顺序排列,碰到父元素边缘则自动换行;浮动

让盒子从普通流中浮起来,主要作用让多个块级盒子一行显示。定位将盒子定在浏览器的某一个位置——CSS 离不开定位,特别是后面的 js 特效清除浮动的影响。

2.清除浮动


     <!-- 在浮动元素的末尾添加一个div标签设置clear:both -->

可以给父级添加: overflow为hidden|auto|scroll 都可以实现,只要不是visible

:after 方式为空元素的升级版,好处是不用单独加标签了

<!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>
        .father {
            width: 300px;
            border: 1px solid rgb(18, 7, 236);
            /* box-sizing: border-box; */
        }
        
        .son {
            width: 100px;
            height: 50px;
            background-color: brown;
            float: left;
        }
        
        .s {
            width: 100px;
            height: 50px;
            background-color: black;
            /* float: left; */
        }
        
        .q {
            width: 100px;
            height: 50px;
            background-color: rgb(171, 21, 164);
            float: right;
        }
        
        .father_bro {
            width: 300px;
            height: 100px;
            background-color: aquamarine;
        }
        
        .father::after {
            content: '.........';
            clear: both;
            display: block;
            height: 0;
            visibility: hidden;
        }
        /* 隐藏,
            虽然隐藏但仍然站位  */
        /* 如果子元素都浮动,父元素没有设置高度,产生了浮动的影响,可以使用伪元素的方法来清除浮动 */
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
        <div class="s"></div>
        <div class="q"></div>
        <div style="clear: both;"></div>
    </div>
    <div class="father_bro"></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>
        .father {
            width: 300px;
            border: 1px solid rgb(23, 145, 82);
            /* 给父元素设置overflow: hidden; */
            overflow: hidden;
        }
        
        .son {
            width: 100px;
            height: 50px;
            background-color: brown;
            float: left;
        }
        
        .q {
            width: 300px;
            height: 100px;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <div class="q"></div>
</body>

</html>

3.伪元素

<!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>
        什么时候使用伪元素
        /* content: 内容属性是伪元素必备的元素; */
        /* .x::before {
            content: '小马抢了';
        }
         */
        
        .x::after {
            content: '小华也来了';
        }
        /* before作为父元素的第一个孩子存在
        after作为父元素的最后一个孩子存在 */
    </style>
</head>

<body>
    <!-- 元素标签html 伪元素:假的标签 -->
    <div class="x">
        <span>小明爱</span>小红
    </div>
</body>

</html>

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>
        .f {
            width: 500px;
            height: 300px;
            border: 1px solid red;
            margin: 50px auto;
        }
        
        .s1 {
            width: 100px;
            height: 100px;
            background-color: #f00;
        }
        
        .s2 {
            width: 100px;
            height: 100px;
            background-color: #0f0;
            /* 相对定位没有脱离自己原来的位置进行定位 没有脱离标准流 */
            position: relative;
            left: 100px;
            top: 80px;
        }
        
        .s3 {
            width: 100px;
            height: 100px;
            background-color: #00f;
        }
    </style>
</head>

<body>
    <div class="f">
        <div class="s1">son1</div>
        <div class="s2">son2</div>
        <div class="s3">son3</div>
    </div>
</body>

</html>

5.绝对定位

<!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>
        .f {
            width: 500px;
            height: 300px;
            border: 1px solid red;
            margin: 50px auto;
        }
        
        .s1 {
            width: 100px;
            height: 100px;
            background-color: #f00;
        }
        
        .s2 {
            width: 100px;
            height: 100px;
            background-color: #0f0;
            position: absolute;
            left: 100px;
            top: 80px;
        }
        /* 绝对定位
        父亲没有设置定位,那么相对于body(文档进行定位) */
        
        .s3 {
            width: 100px;
            height: 100px;
            background-color: #00f;
        }
    </style>
</head>

<body>
    <div class="f">
        <div class="s1">son1</div>
        <div class="s2">son2</div>
        <div class="s3">son3</div>
    </div>
</body>

</html>

6.子绝父相

<!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>
        .f {
            width: 500px;
            height: 300px;
            border: 1px solid red;
            margin: 50px auto;
            position: relative;
        }
        
        .s1 {
            width: 100px;
            height: 100px;
            background-color: #f00;
        }
        
        .s2 {
            width: 100px;
            height: 100px;
            background-color: #0f0;
            position: absolute;
            left: 100px;
            top: 80px;
        }
        
        .s3 {
            width: 100px;
            height: 100px;
            background-color: #00f;
        }
    </style>
</head>

<body>
    <div class="f">
        <div class="s1">son1</div>
        <div class="s2">son2</div>
        <div class="s3">son3</div>
    </div>
</body>

</html>

7.固定位置

<!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>
        img {
            position: fixed;
            right: 50px;
            bottom: 100px;
        }
        
        .sc {
            width: 100px;
            height: 5000px;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <!-- fixed position: 固定定位,相对于浏览器进行定位 -->
    <img src="../huaweiguanwang/img/logo.svg" alt="" width="1000px">
    <div class="sc"></div>
</body>

</html>

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值