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

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

myzbx 2025-03-06 17:35 6 浏览

进入我的主页,查看更多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;
}

示例:


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

有补充请在评论区留言。

相关推荐

Django零基础速成指南:快速打造带用户系统的博客平台

#python##服务器##API##编程##学习#不是所有教程都值得你花时间!这篇实战指南将用5分钟带你解锁Django核心技能,手把手教你从零搭建一个具备用户注册登录、文章管理功能的完整...

iOS 17.0 Bootstrap 1.2.9 半越狱来啦!更新两点

这款Bootstrap半越狱工具终于更新,离上一次更新已相隔很久,现在推出1.2.9版本,主要为内置两点功能进行更新,也是提升半越狱的稳定性。如果你正在使用这款半越狱工具的,建议你更新。注意!...

iOS 16.x Bootstrap 1.2.3 发布,支持运行清理工具

本文主要讲Bootstrap半越狱工具更新相关内容。如果你是iOS16.0至16.6.1和17.0系统的,想体验半越狱的果粉,请继续往下看。--知识点科普--Bootstrap...

SpringBoot整合工作流引擎Acticiti系统,适用于ERP、OA系统

今日推荐:SpringBoot整合工作流引擎Acticiti的源码推荐理由:1、SpringBoot整合工作流引擎Acticiti系统2、实现了三级权限结构3、持久层使用了mybatis框架4、流程包...

SpringCloud自定义Bootstrap配置指南

在SpringCloud中自定义Bootstrap配置需要以下步骤,以确保在应用启动的早期阶段加载自定义配置:1.添加依赖(针对新版本SpringCloud)从SpringCloud2020...

Python使用Dash开发网页应用(三)(python网页开发教程)

PlotlyDash开发Web应用示例一个好的网页设计通常都需要编写css甚至js来定制前端内容,例如非常流行的bootstrap框架。我们既然想使用Dash来搭建web应用,很大的一个原因是不熟悉...

Oxygen XML Editor 27.1 中的新功能

OxygenXMLEditor27.1版是面向内容作者、开发者、合作者和出版商的行业领先工具包的增量版本。在27.1版本中,AIPositronAssistant得到了增强,包括用于...

【LLM-多模态】Mini-Gemini:挖掘多模态视觉语言模型的潜力

一、结论写在前面论文提出了Mini-Gemini,一个精简而强大的多模态VLM框架。Mini-Gemini的本质在于通过战略性框架设计、丰富的数据质量和扩展的功能范围,发掘VLM的潜在能力。其核心是补...

谐云课堂 | 一文详解分布式改造理论与实战

01微服务与分布式什么是分布式?首先,我们对上图提到的部分关键词进行讲解。单体,是指一个进程完成全部的后端处理;水平拆分,是同一个后端多环境部署,他们都处理相同的内容,使用反向代理来均衡负载,这种也叫...

基于Abaqus的手动挡换挡机构可靠性仿真

手动挡,也称手动变速器,英文全称为Manualtransmission,简称MT,即用手拨动换挡操纵总成才能改变变速器内的齿轮啮合位置,改变传动比,从而达到变速的目的。家用轿车主要采用软轴连接的换挡...

【pytorch】目标检测:彻底搞懂YOLOv5详解

YOLOv5是GlennJocher等人研发,它是Ultralytics公司的开源项目。YOLOv5根据参数量分为了n、s、m、l、x五种类型,其参数量依次上升,当然了其效果也是越来越好。从2020...

超实用!50个非常实用的PS快捷键命令大全分享

今天,给大家介绍50个非常实用的快捷键命令大全,大家伙都是设计师,关于软件使用那是越快越好啊。一、常用的热键组合1、图层混合模式快捷键:正常(Shift+Option+N),正片叠底(Shif...

Pohtoshop中深藏不露的小技巧(科目一考试技巧记忆口诀看完必过)

邢帅教育ps教程为大家总结了一些Pohtoshop中深藏不露的小技巧,可以帮助到大家在设计时减少不必要的麻烦,提高工作效率哦~~~1.设置网格线保持像素完美不在1:1分辨率下也能保持像素完美,可以...

Ganglia监控安装总结(监控安装工作总结)

一、ganglia简介:Ganglia是一个跨平台可扩展的,高性能计算系统下的分布式监控系统,如集群和网格。它是基于分层设计,它使用广泛的技术,如XML数据代表,便携数据传输,RRDtool用于数据...

谁说Adobe XD做不出好看的设计?那是你没搞懂这些功能

AdobeXD的美化栏具有将设计视图美化的功能,它能使界面设计和原型设计更漂亮、更吸引眼球。美化栏的7个功能包括竖线布局设计、横线布局设计、重复网格、图形大小和位置设置、响应式调整大小、文字美化以及...