Flex弹性盒子布局实现骰子6点

简介: Flex弹性盒子布局实现骰子6点

概念

Flex 容器(flex container)

Flex 项目(flex item)

水平的主轴(main axis)垂直的交叉轴(cross axis)

主轴的开始位置(与边框的交叉点)main start,结束位置叫做main end;

交叉轴的开始位置叫做cross start,结束位置叫做cross end。

单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

容器的属性

作用

可选参数

默认值

flex-direction

主轴的方向

row|row-reverse | column | column-reverse

row

flex-wrap

换行

nowrap | wrap | wrap-reverse

nowrap

flex-flow

<flex-direction> <flex-wrap>

row nowrap

justify-content

主轴对齐方式

flex-start | flex-end | center | space-between | space-around

flex-start

align-items

交叉轴对齐

flex-start | flex-end | center | baseline | stretch

stretch

align-content

多根轴线的对齐方式

flex-start | flex-end | center | space-between | space-around | stretch

stretch


项目的属性

作用

可选参数

默认值

order

排列顺序

<integer>

0

flex-grow

放大比例

<number>

0

flex-shrink

缩小比例

<number>

1

flex-basis

项目占据的主轴空间

<length>

auto

flex

<flex-grow> <flex-shrink> <flex-basis>

0 1 auto

align-self

项目对齐方式

auto | flex-start | flex-end | center | baseline | stretch

auto

1、基础样式

h2{
    text-align: center;
}

.main{
    display: flex;
    flex-wrap: wrap;
    width: 680px;
    justify-content: space-between;
}

.container{
    display: flex;
    width: 320px;
    height: 320px;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content:space-between;
}

.box{
    width: 90px;
    height: 90px;
    background-color: #EEEEEE;
    padding: 5px;
    border-radius: 5px;
    display: flex;
    flex-wrap: wrap;            
}

.row{
    display: flex;
    flex-basis: 100%;
}

.item{
    width: 24px;
    height: 24px;
    background-color: #000000;
    margin: 3px;
    border-radius: 50%;
}
    

/*排列方向*/
.flex-direction-column{
     flex-direction: column;
}

/*水平排列*/
.justify-content-center{
    justify-content: center;
}

.justify-content-flex-end{
    justify-content: flex-end;
}

.justify-content-space-between{
    justify-content: space-between;
}
    
/*垂直排列*/
 .align-items-center{
     align-items: center;
 }

 .align-items-flex-end{
     align-items: flex-end;
 }

 .align-items-space-between{
     align-items: space-between;
 }

/*多轴对齐*/
 .align-content-space-between{
    align-content: space-between;
 }

 /*项目排列*/
 .align-self-center{
    align-self: center;
 }

 .align-self-flex-end{
    align-self: flex-end;
 }

2、单项目

d25.1.png


代码如下

<div class="container">


<div class="box">
<span class="item"></span>
</div>

<div class="box justify-content-center">
<span class="item"></span>
</div>

<div class="box justify-content-flex-end">
<span class="item"></span>
</div>

<div class="box align-items-center">
<span class="item"></span>
</div>

<div class="box justify-content-center align-items-center">
<span class="item"></span>
</div>

<div class="box justify-content-flex-end align-items-center">
<span class="item"></span>
</div>

<div class="box align-items-flex-end">
<span class="item"></span>
</div>

<div class="box justify-content-center align-items-flex-end">
<span class="item"></span>
</div>

<div class="box justify-content-flex-end align-items-flex-end">
<span class="item"></span>
</div>
</div>

3、双项目

d25.2.png

<div class="container">
<div class="box">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box">
<span class="item"></span>
<span class="item align-self-center"></span>
</div>

<div class="box justify-content-space-between">
<span class="item"></span>
<span class="item align-self-flex-end"></span>
</div>

<div class="box justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box justify-content-space-between align-items-center">
<span class="item"></span>
<span class="item"></span>
</div>
<div class="box justify-content-space-between align-items-flex-end">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box justify-content-space-between flex-direction-column">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box justify-content-space-between align-items-center flex-direction-column">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box justify-content-space-between align-items-flex-end flex-direction-column">
<span class="item"></span>
<span class="item"></span>
</div>

</div>

4、多项目

d25.3.png


<div class="container">
<div class="box">
<span class="item"></span>
<span class="item align-self-center"></span>
<span class="item align-self-flex-end"></span>
</div>

<div class="box justify-content-flex-end align-content-space-between">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box align-content-space-between">
<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>
</div>

<div class="box align-content-space-between">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box align-content-space-between flex-direction-column">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>

<div class="box">
<div class="row">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>

<div class="row justify-content-center">
<span class="item"></span>
</div>

<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>
</div>

<div class="box">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>

</div>

5、骰子6点

d25.4.png


<div class="container">
<div class="box justify-content-center align-items-center">
<span class="item"></span>
</div>

<div class="box justify-content-space-between">
<span class="item"></span>
<span class="item align-self-flex-end"></span>
</div>

<div class="box">
<span class="item"></span>
<span class="item align-self-center"></span>
<span class="item align-self-flex-end"></span>
</div>

<div class="box align-content-space-between">
<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>
</div>

<div class="box">
<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>

<div class="row justify-content-center">
<span class="item"></span>
</div>

<div class="row justify-content-space-between">
<span class="item"></span>
<span class="item"></span>
</div>
</div>

<div class="box align-content-space-between flex-direction-column">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>
</div>

完成代码:

https://github.com/mouday/LearningNote/blob/master/html/flex-demo.html

其他

防止挤压

flex-shrink: 0

参考

  1. Flex 布局教程:语法篇
  2. Flex 布局教程:实例篇
  3. 30分钟彻底弄懂flex布局
  4. CSS3 弹性盒子(Flex Box)
            </div>
目录
相关文章
|
2天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
265 116
|
17天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
12天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
656 220
|
5天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
328 32
Meta SAM3开源:让图像分割,听懂你的话
|
10天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1517 157
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
893 61
|
7天前
|
编解码 Linux 数据安全/隐私保护
教程分享免费视频压缩软件,免费视频压缩,视频压缩免费,附压缩方法及学习教程
教程分享免费视频压缩软件,免费视频压缩,视频压缩免费,附压缩方法及学习教程
290 140