第十三讲_css 伸缩盒模型flex
css 伸缩盒模型flex
- 1. 伸缩盒模型介绍
- 2. 伸缩盒模型的主轴方向
- 3. 伸缩盒模型的主轴换行
- 4. 主轴上的对齐方式
- 5. 侧轴上对齐方式
- 5.1 一行的侧轴上对齐方式
- 5.2 多行的侧轴上对齐方式
- 6. 伸缩项目的伸缩性
- 6.1 伸缩项目在主轴上的基准长度
- 6.2 伸缩项目的放大
- 6.3 伸缩项目的缩小
- 7. flex 复合属性
- 8. 伸缩项目排序
1. 伸缩盒模型介绍
- 伸缩盒模型由伸缩容器和伸缩项目组成。
- 将元素的
display设置为flex,元素就变成了一个伸缩容器,其子元素自动变为伸缩项目(不包括孙子元素)。 - 无论原来是什么元素,变成伸缩项目后,都会变成块元素。
<html><style>.outer {/* 设置为伸缩容器,其子元素自动变成伸缩项目,不包括孙子元素 */display: flex;}.inner {/* 变成伸缩项目后,可以设置宽高 */width: 500px;height: 100px;background-color: red;text-align: center;line-height: 100px;}</style><div class="outer"><span class="inner">我是一个行内元素</span></div>
</html>
2. 伸缩盒模型的主轴方向
伸缩盒模型的主轴,默认是横向的、从左到右,伸缩项目沿着主轴排列。
伸缩盒模型的侧轴,垂直于主轴。
通过flex-direction可以设置主轴的方向:
- row:主轴横向、从左到右(默认值)
- row-reverse:主轴横向、从右到左
- column:主轴纵向、从上到下
- column-reverse:主轴纵向、从下到上
<html><style>.outer1,.outer2,.outer3,.outer4 {border: 1px black solid;margin: 10px;}.outer1 {display: flex;}.outer2 {display: flex;flex-direction: row-reverse;}.outer3 {display: flex;flex-direction: column;}.outer4 {display: flex;flex-direction: column-reverse;}.box {width: 100px;height: 100px;text-align: center;line-height: 100px;}.inner1 {background-color: red;}.inner2 {background-color: green;}.inner3 {background-color: blue;}</style><div class="outer1"><div class="inner1 box">row</div><div class="inner2 box">row</div><div class="inner3 box">row</div></div><div class="outer2"><div class="inner1 box">row-reverse</div><div class="inner2 box">row-reverse</div><div class="inner3 box">row-reverse</div></div><div class="outer3"><div class="inner1 box">column</div><div class="inner2 box">column</div><div class="inner3 box">column</div></div><div class="outer4"><div class="inner1 box">column-reverse</div><div class="inner2 box">column-reverse</div><div class="inner3 box">column-reverse</div></div>
</html>
3. 伸缩盒模型的主轴换行
伸缩项目沿着主轴排列,默认是不换行的。通过flex-wrap可以设置:
- nowrap:不换行(默认值)
- wrap:换行
- wrap-reverse:反向换行
<html><style>.outer1,.outer2,.outer3,.outer4 {width: 200px;border: 1px black solid;margin: 10px;}.outer1 {display: flex;}.outer2 {display: flex;flex-wrap: wrap;}.outer3 {display: flex;flex-wrap: wrap-reverse;}.box {width: 100px;height: 100px;text-align: center;line-height: 100px;}.inner1 {background-color: red;}.inner2 {background-color: green;}.inner3 {background-color: blue;}</style><div class="outer1"><div class="inner1 box">nowrap</div><div class="inner2 box">nowrap</div><div class="inner3 box">nowrap</div></div><div class="outer2"><div class="inner1 box">wrap</div><div class="inner2 box">wrap</div><div class="inner3 box">wrap</div></div><div class="outer3"><div class="inner1 box">wrap-reverse</div><div class="inner2 box">wrap-reverse</div><div class="inner3 box">wrap-reverse</div></div>
</html>
4. 主轴上的对齐方式
伸缩项目沿着主轴排列,在主轴上的对齐方式可以通过justify-content设置:
- flex-start:主轴起点对齐(默认值)
- flex-end:主轴尾部对齐
- center:主轴上居中对齐
- space-between:主轴上均匀分布,两端对齐
- space-around:主轴上均匀分布,两端距离是中间的一半
- space-evenly:主轴上均匀分布,两端距离和中间的一样
<html><style>.outer1,.outer2,.outer3,.outer4,.outer5,.outer6 {border: 1px black solid;margin: 10px;display: flex;}.outer1 {justify-content: flex-start;}.outer2 {justify-content: center;}.outer3 {justify-content: flex-end;}.outer4 {justify-content: space-between;}.outer5 {justify-content: space-around;}.outer6 {justify-content: space-evenly;}.box {width: 100px;height: 100px;text-align: center;line-height: 100px;}.inner1 {background-color: red;}.inner2 {background-color: green;}.inner3 {background-color: blue;}</style><div class="outer1"><div class="inner1 box">flex-start;</div><div class="inner2 box">flex-start;</div><div class="inner3 box">flex-start;</div></div><div class="outer2"><div class="inner1 box">center</div><div class="inner2 box">center</div><div class="inner3 box">center</div></div><div class="outer3"><div class="inner1 box">flex-end</div><div class="inner2 box">flex-end</div><div class="inner3 box">flex-end</div></div><div class="outer4"><div class="inner1 box">space-between</div><div class="inner2 box">space-between</div><div class="inner3 box">space-between</div></div><div class="outer5"><div class="inner1 box">space-around</div><div class="inner2 box">space-around</div><div class="inner3 box">space-around</div></div><div class="outer6"><div class="inner1 box">space-evenly</div><div class="inner2 box">space-evenly</div><div class="inner3 box">space-evenly</div></div>
</html>
5. 侧轴上对齐方式
5.1 一行的侧轴上对齐方式
如果伸缩项目只有一行,在侧轴上的对齐方式可以通过align-items设置:
- flex-start:侧轴起点对齐
- flex-end:侧轴尾部对齐
- center:侧轴上居中对齐
- baseline:侧轴上文本基线对齐
- stretch:侧轴上拉伸到整个父容器(默认值)(ps:如果侧轴是纵向的,伸缩项目不能设置height;如果侧轴是横向,伸缩项目不能设置witch。否则无效)
<html><style>.outer1,.outer2,.outer3,.outer4,.outer5 {border: 1px black solid;margin: 10px;display: flex;height: 200px;}.outer1 {align-items: flex-start;}.outer2 {align-items: center;}.outer3 {align-items: flex-end;}.outer4 {align-items: baseline;}.outer5 {align-items: stretch;}.box {width: 100px;}.inner1 {height: 50px;background-color: red;}.inner2 {height: 100px;background-color: green;}.inner3 {height: 150px;background-color: blue;}.inner4 {background-color: red;}.inner5 {background-color: green;}.inner6{background-color: blue;}</style><div class="outer1"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div><div class="outer2"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div><div class="outer3"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div><div class="outer4"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div><div class="outer5"><div class="inner4 box"></div><div class="inner5 box"></div><div class="inner6 box"></div></div>
</html>
5.2 多行的侧轴上对齐方式
如果伸缩项目有多行,在侧轴上的对齐方式可以通过align-content设置:
- flex-start:侧轴起点对齐
- flex-end:侧轴尾部对齐
- center:侧轴上居中对齐
- space-between:侧轴上均匀分布,两端对齐
- space-around:侧轴上均匀分布,两端距离是中间的一半
- space-evenly:侧轴上均匀分布,两端距离和中间的一致
- stretch:侧轴上平分整个父容器默认值)(ps:如果侧轴是纵向的,伸缩项目不能设置height;如果侧轴是横向,伸缩项目不能设置witch。否则无效)
6. 伸缩项目的伸缩性
6.1 伸缩项目在主轴上的基准长度
flex-basis:设置伸缩项目在主轴上的基准长度。若主轴是横向的,那么伸缩项目的 width 失效;若主轴是纵向的,那么伸缩项目的 height 失效。
浏览器根据这个属性计算主轴上是否有多余的空间,默认值是 auto,即等于伸缩项目的width 或height。
6.2 伸缩项目的放大
flex-grow:设置伸缩项目的放大比例,根据比例瓜分主轴上剩余的空间。
瓜分的计算方式:
- 例如 1:所有的伸缩项目的 flex-grow 都相同,那么所有的伸缩项目平分主轴上剩余的空间。
- 例如 2:一共 3 个伸缩项目,flex-grow 分别为 1、2、3,那么瓜分主轴上剩余的空间比例为 1/6、1/3、1/2。
<html><style>.outer1 {border: 1px black solid;margin: 10px;display: flex;}.box {width: 100px;height: 100px;}.inner1 {flex-grow: 1;background-color: red;}.inner2 {flex-grow: 2;background-color: green;}.inner3 {flex-grow: 3;background-color: blue;}</style><div class="outer1"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div>
</html>
6.3 伸缩项目的缩小
flex-shrink:设置伸缩项目的缩小比例,如果主轴上空间不够。
缩小的计算公式:
例如:一共 3 个伸缩项目,每个伸缩项目的 flex-basis 是 100px、100px、200px,每个伸缩项目的 flex-shrink 是 1、2、1
第一个伸缩项目缩小的比例:100 * 1 / (100 * 1 + 100 * 2 + 200 * 1)
第二个伸缩项目缩小的比例:100 * 2 / (100 * 1 + 100 * 2 + 200 * 1)
第三个伸缩项目缩小的比例:200 * 1 / (100 * 1 + 100 * 2 + 200 * 1)
<html><style>.outer1 {width: 300px;border: 1px black solid;margin: 10px;display: flex;}.box {height: 100px;}.inner1 {width: 100px;flex-shrink: 1;background-color: red;}.inner2 {width: 100px;flex-shrink: 2;background-color: green;}.inner3 {width: 200px;flex-shrink: 1;background-color: blue;}</style><div class="outer1"><div class="inner1 box"></div><div class="inner2 box"></div><div class="inner3 box"></div></div>
</html>
7. flex 复合属性
flex 可以设置 flex-grow、flex-shrink、flex-basis,有严格顺序。默认值为 flex: 0 1 auto
flex: 1 1 auto可简写为flex: autoflex: 1 1 0可简写为flex: 1flex: 0 0 auto可简写为flex: noneflex: 0 1 auto可简写为flex: 0 auto
8. 伸缩项目排序
伸缩项目的顺序,可以通过order 设置,数值越小,顺序越前,默认为 0。如果order 的值相同,按照伸缩项目的先后排序,在前面的排前面。
<html><style>.outer1 {border: 1px black solid;margin: 10px;display: flex;}.box {width: 100px;height: 100px;text-align: center;line-height: 100px;}.inner1 {order: 3;background-color: red;}.inner2 {order: 1;background-color: green;}.inner3 {order: 2;background-color: blue;}</style><div class="outer1"><div class="inner1 box">3</div><div class="inner2 box">1</div><div class="inner3 box">2</div></div>
</html>
相关文章:
第十三讲_css 伸缩盒模型flex
css 伸缩盒模型flex 1. 伸缩盒模型介绍2. 伸缩盒模型的主轴方向3. 伸缩盒模型的主轴换行4. 主轴上的对齐方式5. 侧轴上对齐方式5.1 一行的侧轴上对齐方式5.2 多行的侧轴上对齐方式 6. 伸缩项目的伸缩性6.1 伸缩项目在主轴上的基准长度6.2 伸缩项目的放大6.3 伸缩项目的缩小 7. …...
【C++干货铺】C++11常用新特性 | 列表初始化 | STL中的变化
个人主页点击直达:小白不是程序媛 C系列专栏:C干货铺 代码仓库:Gitee 目录 C11简介 列表初始化 std::initializer_list std::initializer_list使用场景 decltype关键字 STL中的一些变化 新容器 array forward_list 容器中的一些新…...
k8s的对外服务---ingress
service的作用体现在两个方面: 集群内部:不断追踪pod的变化。他会更新endpoint中的pod对象,基于pod的IP地址不断变化的一种服务发现机制。 集群外部:类似负载均衡器,把流量IP端口,不涉及转发url(http、htt…...
element-ui的el-upload组件实现上传拖拽排序图片顺序(sortablejs)
<template><!-- 省略其他配置 --><el-upload ref"upload" :file-list.sync"fileList"></el-upload></template><script>import Sortable from sortablejs;export default {data() {return {fileList: []};},mounted()…...
【PS】PS设计图欣赏、学习、借鉴
【PS】PS设计图欣赏、学习、借鉴 bilibili萌新PS学习计划:PS教程全套零基础教学视频(全套81节全新版本)...
游戏云化好吗?游戏云化会带来什么?
随着云计算技术的飞速发展,游戏云化成为游戏产业中备受关注的话题。这一新兴技术给玩家和游戏行业带来了全新的可能性。本文将深入探讨游戏云化的概念、优势以及可能带来的影响。 1、什么是游戏云化? 游戏云化是指将游戏的各个环节,包括游戏…...
制造业企业数字化转型难点剖析及解决之法
导语 全球正在由工业经济向数字经济转型过渡,制造业正在且并将长期处于数字化转型发展阶段,并沿着数字化、网络化、智能化阶段不断跃升。但如何找准数字化转型的切入点,以低耗能、低成本、高效率的方式加快制造业转型升级的步伐,仍…...
golang 服务端遇到strict-origin-when-cross-origin,解决跨域整理
golang 服务端遇到strict-origin-when-cross-origin,解决跨域整理 以下内容由chatgpt中文网 动态生成,助力开发找我 代码汇总: func Cors() gin.HandlerFunc {return func(c *gin.Context) {method : c.Request.Methodorigin : c.Request.Header.Get(…...
分布式事务Seata实战-AT模式(注册中心为Eureka)
大致记录Seata的AT模式下创建项目过程中需要注意的点和可能遇到的问题。 本项目是以官网的给的示例(即下图)进行创建的,以Eureka为注册中心。 官网:Seata AT 模式 | Apache Seata™ 官方代码示例: 快速启动 | Apac…...
windows vscode jsoncpp cmake c++ 构建项目
jsoncpp的编译和使用推荐文章:jsoncpp的编译和使用 | 爱编程的大丙 (subingwen.cn)https://www.subingwen.cn/cpp/jsoncpp/从这个链接下载jsoncpp-master:https://github.com/open-source-parsers/jsoncpp 可以把这个文件夹名字改成jsoncpp,…...
按照一定规则批量修改文件夹内文件的名称
#一个小朋友问我的问题,写好后,就想着分享出来# #目前只想到这一个普通的方法,应该还有更巧妙的方法,读者可以自己思考# 需求:给定文件夹40001,要求将该文件夹内的图片按照40001_00000001,40002_00000002…...
Git项目分支管理规范
一、分支管理 创建项目时,会针对不同环境创建两个常设分支(也可以算主分支,永久不会删除) master:生产环境的稳定分支,生产环境基于该分支构建。仅用来发布新版本,除了从release测试分支或 hotfix-*Bug修复分支进行m…...
ycsb压测mongodb
下载解压 https://github.com/brianfrankcooper/YCSB/releases/download/0.17.0/ycsb-mongodb-binding-0.17.0.tar.gz tar -zxvf ycsb-mongodb-binding-0.17.0.tar.gzycsb提前已经在workload文件夹下准备好了几个压测场景分别对应workload[a:f] workloads/workloada 样例 …...
【zip密码】Zip压缩包删除密码方法,有哪些?
大家都知道压缩包可以进行加密,但是当我们不需要加密压缩包的时候,该如何删除zip压缩包密码呢?那么zip压缩包密码取消都有什么方法呢?今天将方法总结分享给大家。 最原始的方法,就是通过解压文件,将解压出…...
代码随想录算法训练营day24 || 回溯法原理讲解,77.组合
回溯方法的理论原理与定义 回溯算法是潜藏于递归过程之中一种操作,与递归操作相辅相成;初步理解,有递归必有回溯,使用回溯最好的方式是递归,至于其他的方式有待探索。回溯是一种多重循环的变体,其本质就是…...
RPA与通知机器人的完美结合
写在前面 在现代快节奏的工作环境中,我们经常会面临多个任务同时进行的情况,你还在为时间不够用、忙碌而惆怅吗?你还在为时刻盯着电脑流程而烦恼吗?你还在为及时收不到自己的自动化任务进度而焦躁吗?别担心࿰…...
openssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c
文章目录 openssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c概述笔记END openssl3.2 - 官方demo学习 - signature - rsa_pss_direct.c 概述 用RSA私钥签名 d2i_PrivateKey_ex()可以从内存载入私钥数据, 得到私钥EVP_PKEY* 从私钥产生ctx, 对ctx进行签名初始化, 设置…...
高效批量剪辑技巧:一键按指定时长精准分割视频的方法,轻松制作视频
随着社交媒体和数字内容的快速发展,视频制作的需求也日益增长。在制作视频时,我们经常需要将长视频分割成多个片段,或者将多个片段连接在一起。为了提高效率,我们可以使用一些高效的批量剪辑技巧,一键按指定时长精准分…...
Android基础知识
1. Activity的生命周期 onCreate:Activity在启动时会被创建,后面一般不会在调用该方法(除非例外情况,将Activity回收,例如内存不足);onStart:Activity启动时,会调用该方…...
linux下把动态库变成静态库
1.用nm命令获取动态库中的所有符号列表,假如动态库的文件为lib.so nm -gD lib.so > lib.txt 将把符号列表输出到名为lib.txt的文本文件中 2.创建个新的静态库文件,使用ar命令可以创建一个空的静态库文件 ar -rcs lib.a 3.将动态库中的每个符号提…...
iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘
美国西海岸的夏天,再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至,这不仅是开发者的盛宴,更是全球数亿苹果用户翘首以盼的科技春晚。今年,苹果依旧为我们带来了全家桶式的系统更新,包括 iOS 26、iPadOS 26…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
家政维修平台实战20:权限设计
目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系,主要是分成几个表,用户表我们是记录用户的基础信息,包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题,不同的角色…...
【论文阅读28】-CNN-BiLSTM-Attention-(2024)
本文把滑坡位移序列拆开、筛优质因子,再用 CNN-BiLSTM-Attention 来动态预测每个子序列,最后重构出总位移,预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵(S…...
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...
C++使用 new 来创建动态数组
问题: 不能使用变量定义数组大小 原因: 这是因为数组在内存中是连续存储的,编译器需要在编译阶段就确定数组的大小,以便正确地分配内存空间。如果允许使用变量来定义数组的大小,那么编译器就无法在编译时确定数组的大…...
Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析
Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问(基础概念问题) 1. 请解释Spring框架的核心容器是什么?它在Spring中起到什么作用? Spring框架的核心容器是IoC容器&#…...
Netty从入门到进阶(二)
二、Netty入门 1. 概述 1.1 Netty是什么 Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty是一个异步的、基于事件驱动的网络应用框架,用于…...
从“安全密码”到测试体系:Gitee Test 赋能关键领域软件质量保障
关键领域软件测试的"安全密码":Gitee Test如何破解行业痛点 在数字化浪潮席卷全球的今天,软件系统已成为国家关键领域的"神经中枢"。从国防军工到能源电力,从金融交易到交通管控,这些关乎国计民生的关键领域…...
OD 算法题 B卷【正整数到Excel编号之间的转换】
文章目录 正整数到Excel编号之间的转换 正整数到Excel编号之间的转换 excel的列编号是这样的:a b c … z aa ab ac… az ba bb bc…yz za zb zc …zz aaa aab aac…; 分别代表以下的编号1 2 3 … 26 27 28 29… 52 53 54 55… 676 677 678 679 … 702 703 704 705;…...
