CSS弹性盒模型flex在布局中的应用

前面的话

  前面已经详细介绍过flex弹性盒模型的基本语法兼容写法,本文将介绍flex在布局中的应用

 

元素居中

【1】伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items

<style>
.parent{
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
<div class="parent"  style="background-color: lightgrey; height: 100px; width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>      
</div>

【2】在伸缩项目上使用margin:auto

<style>
.parent{
    display: flex;
}
.in{
    margin: auto;
}
</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>      
</div>

 

两端对齐

<style>
.parent{
    display: flex;
    justify-content:space-between
}
</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>
    <div class="in" style="background-color: lightgreen;">DEMO</div>
    <div class="in" style="background-color: lightcyan;">DEMO</div>
    <div class="in" style="background-color: lightseagreen;">DEMO</div>      
</div>

 

底端对齐

<style>
.parent{
    display: flex;
    align-items: flex-end;
}
</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue; height:20px;">DEMO</div>
    <div class="in" style="background-color: lightgreen; height:30px;">DEMO</div>
    <div class="in" style="background-color: lightcyan; height:40px;">DEMO</div>
    <div class="in" style="background-color: lightseagreen; height:50px;">DEMO</div>      
</div>

 

输入框按钮

复制代码
<style>
.inputBox{
    display: flex;
    width: 250px;
}
.inputBox-ipt{
    flex: 1;
}
</style>
复制代码
<div class="inputBox">
  <input class="inputBox-ipt">
  <button class="inputBox-btn">按钮</button>
</div>

 

等分布局

复制代码
<style>
body,p{margin: 0;}
.parent{
    display: flex;
}
.child{
    flex:1;
    height: 100px;
}
.child + .child{
    margin-left: 20px;
}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="child" style="background-color: lightblue;">1</div>
    <div class="child" style="background-color: lightgreen;">2</div>
    <div class="child" style="background-color: lightsalmon;">3</div>
    <div class="child" style="background-color: pink;">4</div>                
</div>    

 

多列自适应布局

<style>
p{margin: 0;}
.parent{display: flex;}
.left,.center{margin-right: 20px;}
.right{flex: 1;}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

 

悬挂布局

复制代码
<style>        
.box{
    display: flex;
    background-color: lightgrey;
    width: 300px;
}
.left{
    margin-right: 20px;
    background-color: lightblue;
    height: 30px;
}
.main{
    flex:1;
}
</style>
复制代码
<div class="box">
    <div class="left">左侧悬挂</div>
    <div class="main">主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容</div>    
</div>

 

全屏布局

复制代码
<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.parent{
    display: flex;
    flex-direction: column;
}
.top,.bottom{
    height: 50px;
}
.middle{
    display: flex;
    flex: 1;
}
.left{
    width: 100px;
    margin-right: 20px;
}
.right{
    flex: 1;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
</style>
复制代码
复制代码
<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>     
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>            
        </div>                    
    </div>              
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>
复制代码

posted @   小火柴的蓝色理想  阅读(3613)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
显示目录
目录
[1]元素居中[2]两端对齐[3]底端对齐[4]输入框按钮[5]等分布局[6]多列自适应布局[7]悬挂布局[8]全屏布局