文章目录
- 效果演示
- 分析思路
- 代码实现
效果演示
分析思路
5点需要使用margin进行移动点数。而6点的话,使用align-content: space-between;
和 justify-content: space-between;
就能实现,不过需要注意的是主轴为侧轴,dot的第二个要给padding才能实现每一列3个点。
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>骰子点数</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
margin: 100px 100px;
width: 800px;
height: 500px;
border-radius: 10px;
background-color: #232323;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-around;
}
.dot {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #000;
}
.box-common {
width: 100px;
height: 100px;
display: flex;
background-color: #fff;
padding: 4px;
margin: 60px;
border-radius: 8px;
}
/* 1点的写法 主轴与侧轴垂直 */
.box1 {
align-items: center;
justify-content: center;
}
/* 2点的写法 主轴为侧轴 两端对齐 首尾对齐 */
.box2 {
flex-direction: column;
justify-content: space-between;
align-items: center;
}
/* align-self子元素属性,控制自身的排列 */
.dot2-1 {
align-self: flex-start;
}
/* align-self子元素属性,控制自身的排列 */
.dot2-2 {
align-self: flex-end;
}
/* 3点的写法 */
.box3 {
justify-content: space-between;
}
.dot3-1 {
align-self: flex-start;
}
.dot3-2 {
align-self: center;
}
.dot3-3 {
align-self: flex-end;
}
/* 4点写法 */
.box4 {
flex-wrap: wrap;
}
.dot4 {
width: 50%;
height: 50%;
display: flex;
}
.dot4:nth-child(2) {
justify-content: flex-end;
}
.dot4:nth-child(3) {
align-items: flex-end;
}
.dot4:nth-child(4) {
justify-content: flex-end;
align-items: flex-end;
}
/* 5点写法 */
.box5 {
flex-wrap: wrap;
justify-content: center;
align-content: space-between;
}
.dot5:nth-child(1) {
margin-right: 40px;
}
.dot5:nth-child(3) {
margin-left: 40px;
margin-right: 40px;
align-self: center;
}
.dot5:nth-child(4) {
margin-right: 40px;
}
/* 6点写法 */
.box6 {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: space-between;
justify-content: space-between;
}
.dot6:nth-child(2) {
padding: 10px 0;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- 一点 父容器,主轴居中,侧轴居中-->
<div class="box1 box-common">
<div class="dot"></div>
</div>
<!-- 二点 主容器主轴为侧轴,居中,每一个子项单独控制,第一个flex-start,第二个flex-end-->
<div class="box2 box-common">
<div class="dot dot2-1"></div>
<div class="dot dot2-2"></div>
</div>
<!-- 三点 同2点写法一样,只不过中间的点是center-->
<div class="box3 box-common">
<div class="dot dot3-1"></div>
<div class="dot dot3-2"></div>
<div class="dot dot3-3"></div>
</div>
<!-- 四点 分为四个部分,嵌套两层flex盒子-->
<div class="box4 box-common">
<div class="dot4">
<div class="dot"></div>
</div>
<div class="dot4">
<div class="dot"></div>
</div>
<div class="dot4">
<div class="dot"></div>
</div>
<div class="dot4">
<div class="dot"></div>
</div>
</div>
<!-- 5点写法 -->
<div class="box5 box-common">
<div class="dot5">
<div class="dot"></div>
</div>
<div class="dot5">
<div class="dot"></div>
</div>
<div class="dot5">
<div class="dot"></div>
</div>
<div class="dot5">
<div class="dot"></div>
</div>
<div class="dot5">
<div class="dot"></div>
</div>
</div>
<!-- 六点写法 -->
<div class="box6 box-common">
<div class="dot6">
<div class="dot"></div>
</div>
<div class="dot6">
<div class="dot"></div>
</div>
<div class="dot6">
<div class="dot"></div>
</div>
<div class="dot6">
<div class="dot"></div>
</div>
<div class="dot6">
<div class="dot"></div>
</div>
<div class="dot6">
<div class="dot"></div>
</div>
</div>
</div>
</body>
</html>