5.flex布局,浮动和flex

59 阅读4分钟

day06-Flex布局.png

截屏2024-03-29 17.28.10.png

1.浮动

1.具备行内块的特点。2.浮动的盒子顶对齐跟着父亲。3.脱标的

  <title>Document</title>
    <style>
        .left,.right{
            /* width: 200px; */
            height: 200px;
            background-color: pink;
        }
        
        .left {
            margin-left: 20px;
            float: left;
        }
        .right {
            float: left;
        }
        .bottom {
            width: 500px;
            height: 100px;
            background-color: #ccc;
        }
        /* 1.具备行内块的特点。2.浮动的盒子顶对齐。3.脱标的 */

    </style>
</head>
<body>
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="bottom"></div>
</body>
</html>

2.清除浮动

<!-- 清除浮动 -->
    <!-- 1.如果父亲不给高度,标准流会自动撑大盒子 但是和父亲同级的div会顶部展示-->
    <!-- (1)额外标签法 如果出现这种情况在父标签添加一个块级元素,设置css属性clear:both -->
    <!-- (2)单伪元素法 额外标签的变异 -->
    <!-- (3)双伪元素法 -->
    <!-- (4)overflow:hidden -->

    <!-- 2.如果父亲给了高度,两个子div 浮动,和父亲同级的div正常显示 -->
    <style>
        .parent {
            width: 500px;
            /* height: 500px; */
            background-color: yellow;
            /* 4.overflow:hidden */
            overflow: hidden;
        }
        .son1 {
            width: 200px;
            height: 200px;
            background-color: blue;
            float: left;
        }
        .son2 {
            width: 200px;
            height: 200px;
            background-color: pink;
            float: left;
        }
        .bottom {
            width: 1000px;
            height: 50px;
            background-color: black;
        }
        .clear {
            clear: both;
        }
       
        /* .parent::after {
            content: "";
            clear: both;
            display: block;
        } */


        /* 双伪元素 */

        /* .parent::before,.parent::after {
            content: "";
            display: table;
        }
        .parent::after {
            clear: both;
        } */
    </style>
</head>
<body>
     <div class="parent">
        <div class="son1">
        </div>
        <div class="son2">
        </div>
        <!-- <div class="clear">
        </div> -->
     </div>
     <div class="bottom"></div>
</body>

3.图文混排

 <!-- 图文混排 -->
    <style>
     img{
       float: left;
     }
    </style>
</head>

<body>

        <img src="./images/1.jpg" width="300px" height="300px" alt="">

    2024年3月26日,江苏南通市民政局联合市监局发布《禁止制造、销售封建迷信殡葬用品的通告》,规定全市范围内禁止任何单位和个人制造、销售冥币纸钱、纸扎实物等封建迷信殡葬用品。

    而后,有媒体得知当地多数丧葬用品店已将此前进货的纸钱冥币下架,店家将多进购一些鲜花,来应对下周清明节的祭扫高峰。

    全文如下:

    为进一步推动我市殡葬改革,摒弃丧葬陋俗,倡导文明祭祀,净化城市环境,根据《大气污染防治法》《殡葬管理条例》《江苏省殡葬管理条例》等法律法规,现通告:全市范围内禁止任何单位和个人制造、销售冥币纸钱、纸扎实物等封建迷信殡葬用品。违反本通告规定的,由县级以上民政部门会同同级市场监督管理部门予以没收,可以并处制造、销售金额1倍以上3倍以下的罚款。构成违反治安管理行为的,由公安机关依法给予治安管理处罚;构成犯罪的,依法追究刑事责任。本通告自公布之日起施行。

</body>

4.flex

  <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 弹性布局 不会产生脱标-->
    <!-- flex布局给父亲设置display:flex flex布局必须要有父亲 -->
    <!-- 主轴对齐方式  justify-content: space-between; -->
    <!-- justify-content -->
    <!-- justify-content: flex-start;左边对齐
        justify-content: flex-end;右边对齐
        justify-content: center; 居中对齐
        justify-content: space-between; 两侧对齐,中间平均
        justify-content: space-around; 两侧一样,两个之间是2倍。
        justify-content: space-evenly; 两侧一样,之间也一样。
        -->
    <!-- 侧轴对齐方式 align-item -->
    <style>
        .box {
            display: flex;
            width: 1500px;
            height: 500px;
            background-color: pink;
            /* justify-content: space-between; */
            /* justify-content: space-around; */
            justify-content: space-evenly;
           /* 侧轴居中对齐 */
           /* align-items: center; */
            /* align-items: stretch; 如果不给高这里会被拉伸高度 */
            /* align-items: flex-start; */
            /* align-items: flex-end; */
        }
        .box>div {
            width: 300px;
            height: 200px;
            background-color: green;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>
</html>

5.flex-wrap: wrap;

justify-content //水平对齐的方式 align-content //垂直多行对齐的方式 align-item//垂直一行的对齐方式


  <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        li {
            list-style: none;
        }
        div{
           width: 900px;
           height: 500px;
           background-color: yellow; 
           margin: 0 auto;
        }
        ul {
            height: 500px;
            width: 900px;
            display: flex;
            justify-content: space-between;
            align-content: space-between;
            flex-wrap: wrap;
        }   
        li {
            width: 200px;
            height: 200px;
            background-color: blue;
        }
    </style>


</head>
<body>
    <div>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
        </ul>
    </div>
</body>
</html>

6.align-self修改某一个子div的位置。

 <!-- align-self -->
    <style>
         *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
         }

        div {
            width: 1000px;
            height: 500px;
            background: yellowgreen;
            margin: 0 auto;
        }

        ul {
            display: flex;
            justify-content: space-between;
            width: 1000px;
            height: 500px;
        }
        li {
            width: 200px;
            height: 200px;
            background: blueviolet;
        }
        ul li:nth-child(1) {
            align-self: center;
        }
    </style>
</head>
<body>
    <div>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </div>
</body>
</html>

7.更改主轴 flex-direction: column;

 <!-- 更改主轴方向 -->
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .div {
            width: 500px;
            height: 500px;
            background-color: yellowgreen;
            margin: 0 auto;
            justify-content: center;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .div1 {
            width: 200px;
            height: 200px;
            background-color: blue;
           
        }
        .div2 {
            width: 200px;
            height: 200px;
            background-color: yellow;
           
        }
    </style>
</head>
<body>
     <div class="div">
        <div class="div1">

        </div>
        <div class="div2">

        </div>
     </div>
</body>
</html>

8.伸缩比 flex:数字

 <!-- 伸缩比 -->
    <style>
     *{
        margin: 0;
        padding: 0; 
        box-sizing: border-box;
     } 
    .div {
        width: 80%;
        height: 150px;
        background-color: pink;
        margin: 0 auto;
        display: flex;
    }
    .div1 {
        flex:1;
        height: 150px;
        background-color: yellow;
    }
    .div2 {
        flex:2;
        height: 150px;
        background-color: blue;
    }
    .div3 {
        flex:1;
        height: 150px;
        background-color: green;
    }
    </style>
</head>
<body>
        <div class="div">
            <div class="div1">

            </div>
            <div class="div2">

            </div>
            <div class="div3">

            </div>
        </div>
</body>
</html>