CSS 弹性盒子模型

它主要是在一个大的容器当中里面子元素的一个设置。一个大的盒子里面里面的子元素如何摆放由我们的弹性盒子说的算。

之前的盒子模型是一个元素,内边距外边距,边框来调整在页面所显示的位置。

弹性盒子,在大容器里面,里面有很多的小盒子,这些小盒子是如何摆放的。

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>首页</title>

    <style>
       .container{
            width: 500px;
            height: 500px;
            background-color: blueviolet;
       }
       .box1{
            width: 100px;
            height: 100px;
            background-color: blue;
       }
       .box2{
            width: 100px;
            height: 100px;
            background-color: yellow;
       }
       .box3{
            width: 100px;
            height: 100px;
            background-color: greenyellow;
       }
    </style>
</head>

<body>
        
<div class="container">
   <div class="box1">box1</div>
   <div class="box2">flex item 2</div>
   <div class="box3">box3</div>
</div>

</body>

</html>

盒子是自上而下进行摆放的。默认div是块级元素,div和div之间是垂直摆放的。现在可以为盒子的布局设置一个弹性盒子模型,给外层容器的div加上弹性盒子设置,那么就可以管理里面三个盒子。

这样需要给容器设置display属性值为flex。

默认弹性盒子里面内容是横向摆放

html 复制代码
       .container{
            display: flex;
            width: 500px;
            height: 500px;
            background-color: blueviolet;
       }

这里只需要记住一个属性,就是垂直的属性。

justify-content 这个父级属性是应用在垂直上面,垂直方向上面的摆放

html 复制代码
       .container{
            display: flex;
            justify-content: center;
            width: 500px;
            height: 500px;
            background-color: blueviolet;
       }

align-items 父亲属性是水平方向 居左还是居右边还是中

html 复制代码
       .container{
            display: flex;
            justify-content: center;
            align-items: center;
            width: 500px;
            height: 500px;
            background-color: blueviolet;
       }

上面就实现了将小盒子放在大盒子里面居中,垂直和水平方向都居中。设置一个子盒子在一个父亲盒子里面居中非常简单。只需要设置justify-content: center align-items: center这两个属性即可。

有多个盒子,可以设置盒子所占空间的权重。因为大盒子可能会发生变化,所以小盒子使用权重的方式来设置。一旦设置的flex属性,小盒子的宽高就不在生效了。

Go 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>首页</title>

    <style>
       .container{
            display: flex;
            justify-content: center;
            align-items: center;
            width: 500px;
            height: 500px;
            background-color: blueviolet;
       }
       .box1{
            text-align: center;
            background-color: blue;
            flex: 2;
       }
       .box2{
            text-align: center;
            background-color: yellow;
            flex: 2;
       }
       .box3{
            text-align: center;
            background-color: greenyellow;
            flex: 1;
       }
    </style>
</head>

<body>
        
<div class="container">
   <div class="box1">box1</div>
   <div class="box2">flex item 2</div>
   <div class="box3">box3</div>
</div>

</body>

</html>

总结 在父亲元素属性比较多,开启弹性盒子模型通过display:flex属性来开启,子盒子方向可以通过flex-direction进行调整,之后就是子盒子上下左右位置,最后就是子盒子在父盒子当中的平均分配。

相关推荐
会编程的土豆16 小时前
新手前端小细节
前端·css·html·项目
珹洺17 小时前
Bootstrap-HTML(二)深入探索容器,网格系统和排版
前端·css·bootstrap·html·dubbo
BillKu17 小时前
VS Code HTML CSS Support 插件详解
前端·css·html
1024小神1 天前
用css的clip-path裁剪不规则形状的图片展示
前端·css
GGGG寄了1 天前
CSS——文字控制属性
前端·javascript·css·html
HWL56791 天前
在网页中实现WebM格式视频自动循环播放
前端·css·html·excel·音视频
HWL56791 天前
防止移动设备自动全屏播放视频,让视频在页面内嵌位置正常播放
前端·css·音视频
小小测试开发2 天前
UI自动化测试:CSS定位方式超详细解析(附实战示例)
css·ui·tensorflow
RFCEO2 天前
前端编程 课程十五、:CSS核心基础3:文字+段落样式
前端·css·文字+段落样式·css文本样式·美化页面文本内容·演示动画说明·单行文字垂直居中技
2601_949857432 天前
Flutter for OpenHarmony Web开发助手App实战:CSS参考
前端·css·flutter