面试官让我用 Flex 写色子布局,我直接写了六种

开发 前端
面试中经常会被问道前端布局的实现,最典型的就是使用Flex实现色子的布局,这篇文章会逐步的介绍如何使用Flex实现色子的各个点数的布局。

复习一下Flex布局属性

在实现色子布局之前,我们先来复习一下这几个Flex布局的属性:

justify-content:用于调整元素在主轴的对其方式;

align-items:用于调整元素在侧轴的对其方式;

align-self:设置元素自身在侧轴的对齐方式;

flex-direction:定义主轴是水平还是垂直或者正反方向。

多说无益,我们直接来写代码

实现一点布局

实现一点布局就非常简单了,可以说就是一个水平垂直居中 ,用flex布局实现相当的容易,实现代码如下:

html

<body>
<div class="warp">
<div class="pip"></div>
</div>
</body>
复制代码

css

<style>
.warp {
display: flex;
/* 实现 一点 布局 */
justify-content: center;
align-items: center;
}
</style>
复制代码

这里只贴出核心代码,剩余代码就是一些样式样的调整。

实现效果如下:

图片

这里我们用到了justify-content和align-items,就轻松的实现了色子的一点布局。

实现二点布局

现在我们实现色子的二点布局,实现代码如下:

html

<body>
<div class="warp">
<div class="column"><div class="pip"></div></div>
<div class="column"><div class="pip"></div></div>
</div>
</body>
复制代码

css

<style>
.warp {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column {
display: flex;
}
.column:nth-child(2) {
justify-content: flex-end;
}
</style>
复制代码

这仅仅是实现的一种方案,还有别的写法。

图片

实现三点布局

三点布局与二点布局类似,只需要再添加一行即可,实现代码如下:

html

<body>
<div class="warp">
<div class="column"><div class="pip"></div></div>
<div class="column"><div class="pip"></div></div>
<div class="column"><div class="pip"></div></div>
</div>
</body>
复制代码

css

<style>
.warp {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column {
display: flex;
}
.column:nth-child(2) {
justify-content: center;
}
.column:nth-child(3) {
justify-content: flex-end;
}
</style>
复制代码

运行效果如下:

图片

实现四点布局

四点布局可以说是二点布局的变种,实现代码如下:

html

<body>
<div class="warp">
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
</div>
</body>
复制代码

css

.warp {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column {
display: flex;
justify-content: space-between;
}
复制代码

运行效果如下:

图片

实现五点布局

实现五点布局可以在四点布局的基础上增加一行,示例代码如下:

html

<body>
<div class="warp">
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
<div class="column">
<div class="pip"></div>
</div>
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
</div>
</body>
复制代码

css

.warp {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column {
display: flex;
justify-content: space-between;
}
.column:nth-child(2) {
justify-content: center;
}
复制代码

运行效果如下:

图片

实现六点布局

实现六点布局可以在四点布局的基础上增加一行,示例代码如下:

html

<body>
<div class="warp">
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
<div class="column">
<div class="pip"></div>
<div class="pip"></div>
</div>
</div>
</body>
复制代码

css

.warp {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column {
display: flex;
justify-content: space-around;
}
复制代码

运行效果如下:

图片


责任编辑:武晓燕 来源: 前端YUE
相关推荐

2021-12-02 08:19:06

MVCC面试数据库

2020-05-22 08:11:48

线程池JVM面试

2022-11-15 17:45:46

数据库MySQL

2020-09-09 14:49:19

面试官数据结构

2019-12-02 10:51:11

Redis存储系统

2020-09-17 17:53:12

面试ArrayList数组

2024-04-08 10:35:59

JS代码容量

2020-09-08 06:43:53

B+树面试索引

2023-05-10 13:58:13

服务限流系统

2020-07-02 07:52:11

RedisHash映射

2020-12-01 11:50:49

数据库Redis面试

2021-03-01 18:42:02

缓存LRU算法

2020-02-25 16:56:02

面试官有话想说

2021-02-28 07:52:24

蠕虫数据金丝雀

2023-03-30 07:34:10

Linux性能数据结构

2022-11-16 17:10:25

MySQL数据事务

2021-05-28 11:18:50

MySQLbin logredo log

2022-11-04 08:47:52

底层算法数据

2020-12-10 08:43:17

垃圾回收JVM

2021-03-24 10:25:24

优化VUE性能
点赞
收藏

51CTO技术栈公众号