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>
目录
相关文章
|
3月前
|
前端开发 容器
实现弹性盒子(Flexbox)中一行多个 div 平摊排列
这篇文章介绍了如何使用CSS Flexbox布局来实现一行内多个div的自动平摊排列,包括水平均匀分布和非均匀分布排列的方法,并提供了相应的HTML和CSS代码示例。
实现弹性盒子(Flexbox)中一行多个 div 平摊排列
|
3月前
|
前端开发 容器
掌握弹性盒子布局(Flex):打造灵活的网页布局
掌握弹性盒子布局(Flex):打造灵活的网页布局
|
4月前
|
容器
flex-grow 自适配宽度避免内容超出挤压两侧的最佳实践
flex-grow 自适配宽度避免内容超出挤压两侧的最佳实践
83 0
教你快速上手Flex弹性盒布局(项目属性)
教你快速上手Flex弹性盒布局(项目属性)
|
6月前
|
容器 前端开发
弹性盒子(flexbox)
弹性盒子(Flexbox)是CSS3中引入的一种强大且兼容性好的布局方法,它可以让你轻松地处理页面上的元素布局,并创建可以自动缩小和放大的动态用户界面。使用弹性盒子布局模型,可以告别浮动,完美实现垂直水平居中,提高页面排版的灵活性和效率。
183 1
flex 布局子元素被挤压的问题
flex 布局子元素被挤压的问题
|
11月前
|
前端开发
|
容器
Flex弹性盒子布局实现骰子6点
Flex弹性盒子布局实现骰子6点
131 0
Flex弹性盒子布局实现骰子6点
|
容器
超Q的弹性盒子——flex✨
还记得小时候的QQ糖吗,超Q的口感。 而在布局中所使用的弹性盒子也会让你在开发中QQ弹弹 Come on baby
137 3
超Q的弹性盒子——flex✨