百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

CSS:前端必会的flex布局,我把布局代码全部展示出来了

ztj100 2025-03-01 16:13 43 浏览 0 评论

进入我的主页,查看更多CSS的分享!

首先呢,先去看文档,了解flex是什么,这里不做赘述。

当然,可以看下面的代码示例,辅助你理解。

一、row

子元素在水平方向进行布局:

1. 垂直方向靠顶部,水平方向靠左侧

.row-ll {
  display: flex;/* 定义flex */
  flex-direction: row;/* 默认值*/
  align-items: flex-start;/* 默认值*/
  justify-content: flex-start;/* 默认值*/
}

示例:

我是div
text

2. 垂直方向靠顶部,水平方向居中

.row-lc {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: center;
}

3.垂直方向靠顶部,水平方向两端对齐

.row-lsb {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
}

4. 垂直方向靠顶部,水平方向平均分隔(中间间隔的宽度为两边间隔宽度的2倍)

.row-lsa {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-around;
}

5. 垂直方向靠顶部,水平方向平均分隔(间隔距离相等)

.row-lse {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-evenly;
}

6. 垂直方向靠顶部,水平方向靠右侧

.row-le {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: flex-end;
}

?7. 垂直方向可以设置为:靠顶部、居中、靠底部

前面6个是(垂直方向)靠顶部的效果,且(垂直方向)居中、靠底部的代码类似,如下:

/* 垂直方向居中,水平方向靠左侧 */
.row-cl {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: flex-start;
}
/* 垂直方向居中,水平方向居中 */
.row-cc {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: cenetr;
}
/* 垂直方向居中,水平方向平均间隔(中间间隔的宽度为两边间隔宽度的2倍) */
.row-csa {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: space-around;
}
/* 垂直方向居中,水平方向两端对齐 */
.row-csb {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: space-between;
}
/* 垂直方向居中,水平方向平均间隔(间隔距离相等) */
.row-cse {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: space-evenly;
}
/* 垂直方向居中,水平方向靠右侧 */
.row-ce {
  display: flex;
  flex-direction: row;
  align-items: cenetr;
  justify-content: flex-end;
}
/* 垂直方向居底部,水平方向靠左侧 */
.row-el {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-start;
}
/* 垂直方向居底部,水平方向居中 */
.row-ec {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: cenetr;
}
/* 垂直方向居底部,水平方向平均间隔 */
.row-esa {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: space-around;
}
/* 垂直方向居底部,水平方向两端对齐 */
.row-esb {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: space-between;
}
/* 垂直方向居底部,水平方向平均间隔 */
.row-ese {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: space-evenly;
}
/* 垂直方向居底部,水平方向靠右侧 */
.row-ee {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-end;
}

二、column

子元素在垂直方向进行布局:

1. 垂直方向靠顶部,水平方向靠左侧

.col-ll {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
}

2. 垂直方向靠居中,水平方向靠左侧

.col-lc {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}

3. 垂直方向两端对齐,水平方向靠左侧

.col-lsb {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
}

4. 垂直方向平均间隔(中间间隔的宽度为两边间隔宽度的2倍),水平方向靠左侧

.col-lsa {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-around;
}

5. 垂直方向平均间隔(间隔距离相等),水平方向靠左侧

.col-lse {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-evenly;
}

6. 垂直方向靠底部,水平方向靠左侧

.col-le {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-end;
}

7. 水平方向可以设置为:靠顶部、居中、靠底部

前面6个是(水平方向)靠顶部的效果,且(水平方向)居中、靠底部的代码类似,如下:

/* 垂直方向靠顶部,水平方向居中 */
.col-cl {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}
/* 垂直方向居中,水平方向居中 */
.col-cc {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
/* 垂直方向平均间隔,水平方向居中 */
.col-csa {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}
/* 垂直方向两端对齐,水平方向居中 */
.col-csb {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
}
/* 垂直方向平均间隔,水平方向居中 */
.col-cse {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
}
/* 垂直方向靠底部,水平方向居中 */
.col-ce {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
}
/* 垂直方向靠顶部,水平方向靠底部 */
.col-cl {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-start;
}
/* 垂直方向居中,水平方向靠底部 */
.col-cc {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
}
/* 垂直方向平均间隔,水平方向靠底部 */
.col-csa {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-around;
}
/* 垂直方向两端对齐,水平方向靠底部 */
.col-csb {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
}
/* 垂直方向平均间隔,水平方向靠底部 */
.col-cse {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-evenly;
}
/* 垂直方向靠底部,水平方向靠底部 */
.col-ce {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
}

三、更多属性

菜鸟教程(
https://www.runoob.com/css3/css3-flexbox.html)

四、代码太多了也有重复,好乱啊

我参考了vuetify的预置css,flex.css可以这么写:

.d-flex {
  display: flex;
}
.flex-row {
  flex-direction: row;
}
.flex-wrap {
  flex-wrap: wrap;
}
.flex-wrap-reverse {
  flex-wrap: wrap-reverse;
}
.flex-row-reverse {
  flex-direction: row-reverse;
}
.flex-column {
  flex-direction: column;
}
.flex-column-reverse {
  flex-direction: column-reverse;
}
.align-start {
  align-items: flex-start;
}
.align-center {
  align-items: center;
}
.align-end {
  align-items: flex-end;
}
.justify-start {
  justify-content: flex-start;
}
.justify-center {
  justify-content: center;
}
.justify-space-around {
  justify-content: space-around;
}
.justify-space-between {
  justify-content: space-between;
}
.justify-space-evenly {
  justify-content: space-evenly;
}

示例:


垂直居中,水平居中
垂直居中,水平两端对齐

有补充请在评论区留言。

相关推荐

其实TensorFlow真的很水无非就这30篇熬夜练

好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...

交叉验证和超参数调整:如何优化你的机器学习模型

准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...

机器学习交叉验证全指南:原理、类型与实战技巧

机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...

深度学习中的类别激活热图可视化

作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...

超强,必会的机器学习评估指标

大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...

机器学习入门教程-第六课:监督学习与非监督学习

1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...

Python教程(三十八):机器学习基础

...

Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置

你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...

超全面讲透一个算法模型,高斯核!!

...

神经网络与传统统计方法的简单对比

传统的统计方法如...

AI 基础知识从0.1到0.2——用“房价预测”入门机器学习全流程

...

自回归滞后模型进行多变量时间序列预测

下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...

苹果AI策略:慢哲学——科技行业的“长期主义”试金石

苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...

时间序列预测全攻略,6大模型代码实操

如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...

AI 基础知识从 0.4 到 0.5—— 计算机视觉之光 CNN

...

取消回复欢迎 发表评论: