css弹性盒子——flex布局

目录

​编辑

一、flex容器的样式属性(父元素属性)

[display:flex 弹性盒子,实现水平排列,在父盒子设置,适用于单行/单列](#display:flex 弹性盒子,实现水平排列,在父盒子设置,适用于单行/单列)

justify-content

二、flex元素的样式属性(子元素属性)

1.flex-grow

2.flex-shrink

3.flex-basis

[4.flex组合属性 flex:flex-grow flex-shrink flex-basis;](#4.flex组合属性 flex:flex-grow flex-shrink flex-basis;)

5.order

[6.align-self 单独控制某个元素](#6.align-self 单独控制某个元素)


一、flex容器的样式属性(父元素属性)

display:flex 弹性盒子,实现水平排列,在父盒子设置,适用于单行/单列

默认子盒子宽高度超过父盒子时,按父盒子宽高度均分,再多则会溢出

flex和上面的block,inline,inline-block并列, display只能设置一个属性,display和float不能出现在同一个盒子的属性里。

flex-direction: column;flex-wrap:wrap; 等于 flex-flow: row wrap;

使用F12可快速调试各属性的页面效果

justify-content

justify-content:space-evenly;

justify-content:space-between;

justify-content: space-around;两侧的空间是中间元素间隔空间的一半

stretch(默认值)

align-items:baseline;

二、flex元素的样式属性(子元素属性)

1.flex-grow

单个盒子范围0~1 延伸至剩余空间的0~1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 300px;
            background-color: skyblue;
            display: flex;
        }
        .box div{
            width: 100px;
            height: 100px;
            background-color: pink;
            flex-grow: 0.6;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
    </div>
</body>
</html>

多个子盒子设置超过1时,剩余空间分成n份

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 300px;
            background-color: skyblue;
            display: flex;
        }
        .box div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background-color: pink;
            flex-grow: 3;
        }
        .box div:nth-of-type(2){
            width: 200px;
            height: 100px;
            background-color: green;
            flex-grow: 2;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
    </div>
</body>
</html>

2.flex-shrink

收缩超出部分的0~1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 300px;
            background-color: skyblue;
            display: flex;
        }
        .box div:nth-of-type(1){
            width: 600px;
            height: 100px;
            background-color: pink;
            flex-shrink: 0.5;
        }
        /* .box div:nth-of-type(2){
            width: 200px;
            height: 100px;
            background-color: green;
        } */
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <!-- <div>2</div> -->
    </div>
</body>
</html>

3.flex-basis

设置主轴初始尺寸,0%(不起作用),n px

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 500px;
            background-color: skyblue;
            display: flex;
        }
        .box div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background-color: pink;
            /* flex-shrink: 0.5; */
            flex-basis: 200px;
        }
        /* .box div:nth-of-type(2){
            width: 200px;
            height: 100px;
            background-color: green;
            flex-grow: 0.5;
        } */
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <!-- <div>2</div> -->
    </div>
</body>
</html>

4.flex组合属性 flex:flex-grow flex-shrink flex-basis;

flex:1; 表示1 1 0%

flex:0.5; 表示0.5 1 0%

5.order

顺序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 500px;
            background-color: skyblue;
            display: flex;
            /* flex-direction: column; */
        }
        .box div{
            width: 100px;
            height: 100px;
            background-color: pink;
            border: 1px solid black;
        }
        .box div:nth-of-type(1){
            order:1;
        }
        .box div:nth-of-type(1){
            order:-1;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>
</html>

6.align-self 单独控制某个元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-grow</title>
    <style>
        .box{
            width: 500px;
            height: 300px;
            background-color: skyblue;
            display: flex;
            /* flex-direction: column; */
        }
        /* .box div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background-color: pink;
            flex-shrink: 0.5;
            flex-basis: 200px;
            flex:1;
        } */
        /* .box div:nth-of-type(2){
            width: 200px;
            height: 100px;
            background-color: green;
            flex-grow: 0.5;
        } */
        .box div{
            width: 100px;
            height: 100px;
            background-color: pink;
            border: 1px solid black;
        }
        /* .box div:nth-of-type(1){
            order:1;
        } */
        .box div:nth-of-type(4){
            align-self: flex-end;
        }
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>
</html>
相关推荐
lzhdim25 分钟前
2、.Net 前端框架:ASP.Net Core - .Net宣传系列文章
后端·前端框架·asp.net·.net
土小帽软件测试1 小时前
F12抓包12:Performance(性能)前端性能分析
前端·抓包·软件测试学习·f12·f12抓包
白总Server1 小时前
pptp解说
前端·javascript·vue.js·物联网·网络协议·数据库架构·idc
Nozomi99671 小时前
CSS—4
前端·css
Commas.KM2 小时前
【CSS|第1期】网页设计的演变:从表格布局到Grid布局
前端·css·网页布局·flex布局·grid布局·表格布局·div+css布局
读心悦2 小时前
CSS的offset属性
前端·css
一 乐3 小时前
点餐|基于java的电子点餐系统小程序(源码+数据库+文档)
java·开发语言·前端·数据库·小程序·论文
布瑞泽的童话3 小时前
Vue 也能这样玩?Vuetify 打造超强大的UI体验
前端·vue.js·ui·开源
加勒比海涛3 小时前
ElementUI 快速入门:使用 Vue 脚手架搭建项目
前端·vue.js·elementui
安冬的码畜日常3 小时前
【CSS in Depth 2 精译_031】5.3 Grid 网格布局的两种替代语法
前端·css·css3·html5·网格布局·网格·css布局