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

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

zhezhongyun 2025-06-13 18:08 20 浏览

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

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

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

一、row

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

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

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

示例:

<div class="row-ll" style="width: 660px; height: 230px; border: 1px solid red;">
  <div style="width: 110px; height: 110px;">我是div</div>
  <img src="" alt="" style="width: 110px; height: 110px;" />
  <span>text</span>
</div>

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;
}

示例:

<link rel="stylesheet" href="./flex.css" />
<div class="d-flex flex-row align-center justify-center">
    垂直居中,水平居中
</div>
<div class="d-flex flex-row align-center justify-space-between">
    垂直居中,水平两端对齐
</div>

有补充请在评论区留言。

相关推荐

3 分钟!AI 从零开发五子棋全过程曝光,网友:这效率我服了

<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8...

一行代码实现display&quot;过渡动画&quot;原理

作者:Peter谭老师转发链接:https://mp.weixin.qq.com/s/XhwPOv62gypzq5MhhP-5vg写本文的起因上篇文章,提到如何让display出现过渡动画,却没有仔...

脑洞:琼恩·雪诺、蝙蝠侠和魔形女的灵魂宠物了解一下

AlekseiVinogradovisaRussianfreelancedigitalartistwhoshareshisskillsandtalentwith120k...

浏览器的渲染机制、重绘、重排

1、什么是重排和重绘网页生成过程:HTML被HTML解析器解析成DOM树css则被css解析器解析成CSSOM树结合DOM树和CSSOM树,生成一棵渲染树(RenderTree)生成布局(flo...

托福写作高频考题写作思路&amp;词汇丨考虫独家

科技话题与媒体话题是托福写作的常考话题很多考生对这两类话题里的专有词汇表达也许很不了解所以今天就跟随考虫托福写作老师刘云龙老师一起来学习在这些话题的写作里你可以使用哪些有用的表达。希望大家有收获!记得...

在优麒麟上使用 Electron 开发桌面应用

使用Web标准来创建桌面GUI,上手快、成本低、跨平台、自适应分辨率,这些都是Electron的优势。作者/来源:优麒麟Electron是由Github开发,用HTML、CSS和...

php手把手教你做网站(三十八)jquery 转轮盘抽奖,开盲盒

抽奖和开盲盒性质一样的都是通过ajax读取后台的随机数据。1、转轮盘本来是想直接绘图实现轮盘,但是没有找到怎么填充文字,只好把轮盘弄成了背景图,通常用于游戏抽道具,商城积分抽奖,公司年末员工抽奖点击抽...

用 CSS 整活!3D 轮播图手把手教学,快乐代码敲出来

兄弟们,今天咱来搞点好玩的——用CSS整一个3D轮播图!咱野生程序员就是要在代码里找乐子,技术和快乐咱都得要!代码是写不完的,但咱能自己敲出快乐来,走起!一、先整个容器,搭个舞台咋先写一个...

实现一个超酷的 3D 立体卡片效 #前端开发

今天我们来实现一个超酷的3D立体卡片效果。正常情况下就是一个普通的图片展示卡片,鼠标悬停的时候图片会跳出卡片,并将影子投射到背景卡片上,在视觉上有一个3D立体感。html主要分成3个部分:容器→背景层...

Vue 3 Teleport与Suspense:解决UI难题的两个&quot;隐藏大招&quot;

模态框的"层级噩梦"与Teleport的救赎"这个模态框怎么又被父容器截断了?"团队协作开发后台系统时,小张第N次遇到这个问题。多层嵌套的组件结构里,弹窗被overfl...

让交互更加生动!有意思的鼠标跟随 3D 旋转动效

今天,群友问了这样一个问题,如下所示的鼠标跟随交互效果,如何实现:简单分析一下,这个交互效果主要有两个核心:借助了CSS3D的能力元素的旋转需要和鼠标的移动相结合本文,就将讲述如何使用纯CSS...

填坑:transform元素导致zindex失效终极方法

今天遇到了使用css3动画的元素层级被放大置顶的问题,ios浏览器上没问题,安卓原生浏览器和安卓微信上有问题。使用了css3动画的元素z-index失效,兄弟元素设置多高的z-index都盖不住解决办...

诡异的层级错乱:一个被transform隐藏的CSS陷阱

周五下午三点十七分,设计部突然发来紧急截图——原本应该悬浮在顶部的导航菜单,此刻正诡异地被下方的轮播图遮挡。我盯着屏幕上错乱的层级关系,手指下意识地敲下z-index:9999,心里清楚这不过是程序...

动画篇--碎片动画

本文授权转载,作者:Sindri的小巢(简书)前言从最开始动笔动画篇的博客,至今已经过去了四个多月。这段时间回头看了看自己之前的动画文章,发现用来讲解动画的例子确实不那么的赏心悦目。于是这段时间总是想...

Nature:大洋转换断层处的拉张构造与两阶段地壳增生

Nature:大洋转换断层处的拉张构造与两阶段地壳增生转换断层是三种基本的板块边界之一,全球总长度超过48000km(Bird,2003),它们的发现为板块构造理论的建立奠定了重要的基础(Wil...