效果图:
先将六个面的结构写出来
<div class="box">
<!--第一面-->
<div>
<span class="dot"></span>
</div>
<!--第二面-->
<div>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第三面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
</div>
<!--第四面-->
<div>
<span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第五面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第六面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
</div>
</div>
设置每个面的样式
.box>div{
width:100px;
height: 100px;
border-radius: 13px;
background-color: #fff;
border: 1px solid gray;
display: flex;
padding: 4px;
box-sizing: border-box; /*表示设置的内边距是包括在width内的*/
}
box-sizing
属性
content-box | 默认属性,标准盒子模型,width 与 height 只包括内容的宽和高,不包括边框(border),内边距(padding),外边距(margin) |
border-box | width 和 height 属性包括内容,内边距和边框,但不包括外边距(margin) |
设置骰子里的红点点的样式
.dot{
width: 30px;
height: 30px;
border-radius: 50%;
background-color: rgb(207, 27, 27);
box-shadow: 0 0 3px 1px rgb(231, 238, 244) inset;
}
box-shadow: h-shadow v-shadow blur spread color inset;
h-shadow | 必需。水平阴影的位置。允许负值。负值表阴影在左边,正为右 |
v-shadow | 必需。垂直阴影的位置。允许负值。负值表阴影在上边,正为下 |
blur | 可选,模糊距离 |
spread | 可选,阴影尺寸 |
color | 可选,阴影的颜色,默认为黑色 |
inset | 可选,将外部阴影变成内部阴影 |
h-shadow v-shadow
正负值的记法:左上为负,右下为正。如果是内部阴影,就会反过来。
开始分别对骰子的点进行分布
/*第一个骰子*/
.box>div:nth-child(1){
justify-content:center; /*在主轴上水平居中*/
align-items:center; /*在交叉轴上垂直居中*/
}
/*第二个骰子*/
.box>div:nth-child(2){
justify-content: space-between; /*两端对齐*/
flex-wrap: wrap;
}
.box>div:nth-child(2)>.dot:nth-child(2){
align-self: flex-end;
}
/*第三个骰子*/
.box>div:nth-child(3)>.dot:nth-child(2){
align-self: center;
}
.box>div:nth-child(3)>.dot:nth-child(3){
align-self: flex-end;
}
/*第四个骰子*/
.box>div:nth-child(4){
justify-content: space-between;
align-content: space-between;
flex-wrap: wrap;
}
.box>div:nth-child(4)>.dot:nth-child(2){
transform: translate(100%,200%);
}
/*第五个骰子*/
.box>div:nth-child(5){
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}
.box>div:nth-child(5)>.dot:nth-child(2){
transform: translateY(100%);
}
/*第六个骰子*/
.box>div:nth-child(6){
justify-content: space-between;
align-content: space-between;
flex-wrap: wrap;
}
第四五个骰子还有其他的方法布局,第六个很好同理,这里就不作说明。如下:
<!-- 第四个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
将左右两边的两个点竖着排列
/* 第四个骰子 */
.box>div:nth-child(7){
justify-content: space-between;
}
.box>div:nth-child(7)>div:nth-child(1),.box>div:nth-child(7)>div:nth-child(2){
display: flex;
flex-direction: column;
/* 这里主轴变成了y轴,所以用justify-content */
justify-content: space-between;
}
<!-- 第五个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
将第三个点居中,剩下四个点(上面两个、下面两个)两端对齐
/* 第五个骰子 */
.box>div:nth-child(8){
align-content: space-between;
flex-wrap: wrap;
}
.box>div:nth-child(8)>div:nth-child(2){
display: flex;
width: 100%;
justify-content: center;
}
.box>div:nth-child(8)>div:nth-child(1),.box>div:nth-child(8)>div:nth-child(3){
display: flex;
width: 100%;
justify-content: space-between;
}
将两边的两点竖着排列,再改变第三个点的自身位置
/* 第五个骰子 */
.box>div:nth-child(9){
justify-content: space-between;
}
.box>div:nth-child(9)>div:nth-child(1),.box>div:nth-child(9)>div:nth-child(3){
display: flex;
flex-direction: column;
justify-content: space-between;
}
.box>div:nth-child(9)>div:nth-child(2){
display: flex;
}
.box>div:nth-child(9)>div:nth-child(2)>.dot{
align-self: center;
}
还有一种方法:
/* 第五个骰子 */
.box>div:nth-child(10){
flex-wrap: wrap;
}
.box>div:nth-child(10)>div{
display: flex;
flex-basis: 100%;
/* 疑似水平铺满 */
justify-content: space-between;
}
.box>div:nth-child(10)>div:nth-child(2){
justify-content: center;
}
全部代码如下:
<!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>Document</title>
<style>
.box{
display: flex;
justify-content: space-around;
}
.box>div{
width:100px;
height: 100px;
border-radius: 13px;
background-color: #fff;
border: 1px solid gray;
display: flex;
padding: 4px;
box-sizing: border-box; /*表示设置的内边距是包括在width内的*/
}
.dot{
width: 30px;
height: 30px;
border-radius: 50%;
background-color: rgb(207, 27, 27);
box-shadow: 2px 1px 6px 3px rgb(225, 134, 134) inset;
}
/*第一个骰子*/
.box>div:nth-child(1){
justify-content:center; /*在主轴上水平居中*/
align-items:center; /*在交叉轴上垂直居中*/
}
/*第二个骰子*/
.box>div:nth-child(2){
justify-content: space-between; /*两端对齐*/
flex-wrap: wrap;
}
.box>div:nth-child(2)>.dot:nth-child(2){
align-self: flex-end;
}
/*第三个骰子*/
.box>div:nth-child(3)>.dot:nth-child(2){
align-self: center;
}
.box>div:nth-child(3)>.dot:nth-child(3){
align-self: flex-end;
}
/*第四个骰子*/
.box>div:nth-child(4){
justify-content: space-between;
align-content: space-between;
flex-wrap: wrap;
}
.box>div:nth-child(4)>.dot:nth-child(2){
transform: translate(100%,200%);
}
/*第五个骰子*/
.box>div:nth-child(5){
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}
.box>div:nth-child(5)>.dot:nth-child(2){
transform: translateY(100%);
}
/*第六个骰子*/
.box>div:nth-child(6){
justify-content: space-between;
align-content: space-between;
flex-wrap: wrap;
}
/* 第四个骰子 */
.box>div:nth-child(7){
justify-content: space-between;
}
.box>div:nth-child(7)>div:nth-child(1),.box>div:nth-child(7)>div:nth-child(2){
display: flex;
flex-direction: column;
/* 这里主轴变成了y轴,所以用justify-content */
justify-content: space-between;
}
/* 第五个骰子 */
.box>div:nth-child(8){
align-content: space-between;
flex-wrap: wrap;
}
.box>div:nth-child(8)>div:nth-child(2){
display: flex;
width: 100%;
justify-content: center;
}
.box>div:nth-child(8)>div:nth-child(1),.box>div:nth-child(8)>div:nth-child(3){
display: flex;
width: 100%;
justify-content: space-between;
}
/* 第五个骰子 */
.box>div:nth-child(9){
justify-content: space-between;
}
.box>div:nth-child(9)>div:nth-child(1),.box>div:nth-child(9)>div:nth-child(3){
display: flex;
flex-direction: column;
justify-content: space-between;
}
.box>div:nth-child(9)>div:nth-child(2){
display: flex;
}
.box>div:nth-child(9)>div:nth-child(2)>.dot{
align-self: center;
}
/* 第五个骰子 */
.box>div:nth-child(10){
flex-wrap: wrap;
}
.box>div:nth-child(10)>div{
display: flex;
flex-basis: 100%;
/* 疑似水平铺满 */
justify-content: space-between;
}
.box>div:nth-child(10)>div:nth-child(2){
justify-content: center;
}
</style>
</head>
<body>
<div class="box">
<!--第一面-->
<div>
<span class="dot"></span>
</div>
<!--第二面-->
<div>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第三面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
</div>
<!--第四面-->
<div>
<span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第五面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span>
</div>
<!--第六面-->
<div>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
</div>
<!-- 第四个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
<!-- 第五个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
<!-- 第五个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
<!-- 第五个骰子 -->
<div>
<div><span class="dot"></span><span class="dot"></span></div>
<div><span class="dot"></span></div>
<div><span class="dot"></span><span class="dot"></span></div>
</div>
</div>
</body>
</html>