CSS弹性盒模型flex在布局中的应用_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:17:11
原创
1521人浏览过

× 目录 [1]元素居中 [2]两端对齐 [3]底端对齐 [4]输入框按钮 [5]等分布局 [6]自适应布局 [7]悬挂布局 [8]全屏布局

前面的话

  前面已经详细介绍过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>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/2007">
                            <img src="https://img.php.cn/upload/ai_manual/001/246/273/68b6cf21129f9332.png" alt="无阶未来模型擂台/AI 应用平台">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/2007">无阶未来模型擂台/AI 应用平台</a>
                            <p>无阶未来模型擂台/AI 应用平台,一站式模型+应用平台</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="无阶未来模型擂台/AI 应用平台">
                                <span>35</span>
                            </div>
                        </div>
                        <a href="/ai/2007" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="无阶未来模型擂台/AI 应用平台">
                        </a>
                    </div>
                        <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>
登录后复制

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号