当前位置: 首页 > news >正文

flex布局实例:把色子放进盒子里

目录

一、flex布局实例:把色子放进盒子里

1、基础样式

二、justify-content 属性

三、flex-direction 属性

四、align-items 属性

五、flex-wrap 属性

二、flex布局应用到常见场景


非常详细的讲解flex布局,看一看,练一练!

一、色子场景示例

核心知识点:display: flex将容器变为弹性容器,内部元素(色子)变为弹性项目,遵循 flex 布局规则。
假设我们有一个盒子(容器)和1- 6 个色子(项目),每个色子代表一个 flex 项目。如果不了解flex布局,请先阅读  Flex弹性布局


1、基础样式

制作色子布局的基础样式,以下实例都需要用到如下样式:

<style>
/* 主容器:使用Flex布局排列6个色子面 */.dice-container {display: flex;flex-wrap: wrap;gap: 20px;padding: 20px;}/* 每个色子面的基本样式 */.dice {display: flex;width: 100px;height: 100px;border: 2px solid black;border-radius: 10px;background-color: white;padding: 10px;}/* 点的样式 */.dot {width: 20px;height: 20px;border-radius: 50%;background-color: black;}
</style>
二、justify-content 属性

 以下是css样式和html代码

 .justify-start-example {justify-content: flex-start;}
<div class="dice justify-start-example"><div class="dot"></div>
</div>

以下是css样式和html代码

.justify-end-example {justify-content: flex-end;}
    <div class="dice justify-end-example"><div class="dot"></div></div>

以下是css样式和html代码

.justify-center-example {justify-content: center;}
<div class="dice justify-center-example"><div class="dot"></div></div>

以下是css样式和html代码

.justify-space-between-example {justify-content: space-between;}
<div class="dice justify-space-between-example"><div class="dot"></div><div class="dot"></div></div>

 

 以下是css样式和html代码

 .justify-space-around-example {justify-content: space-around;}
<div class="dice justify-space-around-example"><div class="dot"></div><div class="dot"></div></div>

 

 以下是css样式和html代码

.justify-space-evenly-example {justify-content: space-evenly;}
<div class="dice justify-space-evenly-example"><div class="dot"></div><div class="dot"></div></div>
三、flex-direction 属性

以下是css样式和html代码

.flex-direction-row {flex-direction: row;}
<div class="dice flex-direction-row"><div class="dot"></div><div class="dot"></div></div>

以下是css样式和html代码

.flex-direction-column {flex-direction: column;}
<div class="dice flex-direction-column"><div class="dot"></div><div class="dot"></div></div>

 

以下是css样式和html代码

.flex-direction-row-reverse {flex-direction: row-reverse;}
<div class="dice flex-direction-row-reverse"><div class="dot white textcenter">1</div><div class="dot white textcenter">2</div></div>

 

以下是css样式和html代码

.flex-direction-column-reverse {flex-direction: column-reverse;}
<div class="dice flex-direction-column-reverse"><div class="dot white textcenter">1</div><div class="dot white textcenter">2</div></div>

 

四、align-items 属性

以下是css样式和html代码

.align-items-flex-start {align-items: flex-start;}
<div class="dice align-items-flex-start"><div class="dot"></div></div>

 

以下是css样式和html代码

.align-items-flex-end {align-items: flex-end;}
<div class="dice align-items-flex-end"><div class="dot"></div></div>

 

以下是css样式和html代码

.align-items-center {align-items: center;}
<div class="dice align-items-center"><div class="dot"></div></div>

 

以下是css样式和html代码

.align-items-baseline {align-items: baseline;}
<div class="dice align-items-baseline"><div style="background-color: aquamarine; padding: 5px; font-size: 12px;">1</div><div style="background-color: aquamarine; padding: 5px; font-size: 28px;">2</div><div style="background-color: aquamarine; padding: 5px; font-size: 18px;">3</div></div>

 

以下是css样式和html代码

.align-items-stretch {align-items: stretch;}
<div class="dice align-items-stretch"><div style="background-color: aquamarine; padding: 5px; color:#000">1</div><div style="background-color: aquamarine; padding: 5px; color:#000">2</div></div>

 

五、flex-wrap 属性

以下是css样式和html代码

.flex-wrap-nowrap {flex-wrap: nowrap;}
<div class="dice flex-wrap-nowrap"><div class="dot white textcenter">1</div><div class="dot white textcenter">2</div><div class="dot white textcenter">3</div><div class="dot white textcenter">4</div><div class="dot white textcenter">5</div><div class="dot white textcenter">6</div></div>

以下是css样式和html代码

.flex-wrap-wrap {flex-wrap: wrap;}
<div class="dice  flex-wrap-wrap"><div class="dot white textcenter">1</div><div class="dot white textcenter">2</div><div class="dot white textcenter">3</div><div class="dot white textcenter">4</div><div class="dot white textcenter">5</div><div class="dot white textcenter">6</div><div class="dot white textcenter">7</div><div class="dot white textcenter">8</div></div>

 

以下是css样式和html代码 

.flex-wrap-wrap-reverse {flex-wrap: wrap-reverse;}
<div class="dice flex-wrap-wrap-reverse"><div class="dot white textcenter">1</div><div class="dot white textcenter">2</div><div class="dot white textcenter">3</div><div class="dot white textcenter">4</div><div class="dot white textcenter">5</div><div class="dot white textcenter">6</div><div class="dot white textcenter">7</div><div class="dot white textcenter">8</div></div>

 

二、flex布局应用到常见场景

.dice.one {justify-content: center;align-items: center;}

 /* 两点:对角 */.dice.two {justify-content: space-between;}.dice.two .dot:nth-child(2) {align-self: flex-end;}

 

/* 三点:对角 */.dice.three {justify-content: space-between;}.dice.three .dot:nth-child(2) {align-self: center;}.dice.three .dot:nth-child(3) {align-self: flex-end;}

 

/* 四点:2x2网格 */.dice.four {flex-wrap: wrap;justify-content: space-between;align-content: space-between;}.dice.four .column {flex-basis: 100%;display: flex;justify-content: space-between;}

 

/* 五点:4角+中心 */.dice.five {flex-wrap: wrap;justify-content: space-between;align-content: space-between;}.dice.five .column {flex-basis: 100%;display: flex;justify-content: space-between;}.dice.five .column:nth-child(2) {justify-content: center;}

 

/* 六点:3x2网格 */.dice.six {flex-wrap: wrap;justify-content: space-between;align-content: space-between;}.dice.six .column {flex-basis: 100%;display: flex;justify-content: space-between;}

实例结合理论【 flex布局基础知识 】一定能做的很好!小伙伴们动手练习起来吧!

相关文章:

  • 中象做网站怎么样做网站的软件有哪些
  • 学做网站的视频教学如何在百度投放广告
  • 企业官方网站的作用国内十大4a广告公司
  • 重生做二次元网站关键词查询
  • python网站开发学习google推广公司
  • 惠通网站建设2022年最新热点素材
  • 資訊安全 (Information Security)3大 “CIA“要素
  • Jenkins X + AI:重塑云原生时代的持续交付范式
  • 【151】基于Springboot+Vue实现的校园订餐管理系统小程序(有文档+PPT+视频)
  • 小程序快速获取url link方法,短信里面快速打开链接
  • 基于springboot+vue的智慧农业专家远程指导系统
  • DeepSeek+WinForm串口通讯实战
  • Nginx + Tomcat 负载均衡搭建
  • 云计算产业链
  • 文档处理控件Aspose.Words教程:在.NET中将多页文档转换为单个图像
  • 回归预测 | Matlab实现KAN神经网络多输入单输出回归预测模型
  • [LVGL] 刷新率优化
  • 入门级STM32F103C8T6无人机遥控(原理图)
  • 一步部署APache编译安装脚本
  • 设备维修全流程记录,提升设备运维效率
  • django request.data.get 判断有没有 某个参数
  • 日本生活:日语语言学校-日语作文-沟通无国界(5)-题目:我的一天
  • 【MCP服务】蓝耘元生代 | MCP平台:部署时间服务器MCP,开启大模型交互新体验
  • 理论加案例,一文读懂数据分析中的分类建模
  • [架构之美]Redis客户端命令指南
  • 在树莓派上用 .NET8.0 挂载TCP服务端