注:新手练习
基本思路
HtML部分:
一个大 ul 包裹6个 li,也就是6个骰子,每个 li 中的 span 对应一个骰子点数
<ul>
<li>
<span></span>
</li>
<li>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
</ul>
CSS部分:
先设置统一的样式,六个筛子的大小、边框、颜色,骰子内部点数的大小、颜色保持一致。给所有 li 设置 flex 属性
ul {
width: 400px;
height: 400px;
background-color: beige;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
align-items: center;
background-color: green;
}
/* 骰子统一样式 */
li {
width: 100px;
height: 100px;
border: 1px solid black;
background-color: white;
border-radius: 20%;
display: flex;
}
/* 点数统一样式 */
span {
width: 30px;
height: 30px;
background-color: blue;
border-radius: 50%;
}
第1个骰子:
只需设置主轴和侧轴居中即可
ul :nth-child(1) {
justify-content: center;
align-items: center;
}
第2个骰子:
改变主轴,两边环绕对齐,再居中
ul :nth-child(2) {
flex-direction: column;
justify-content: space-around;
align-items: center;
}
第3个骰子:
这个骰子比较特殊,是斜着对齐的,所以要使用align-self属性。先设置一个环绕,再分别对第二个和第三个点数控制。(这里由于筛子的圆角设置了成了20%,避免斜着对齐离边框太近,就设置了一下内边距。)
ul :nth-child(3) {
justify-content: space-around;
box-sizing: border-box;
padding: 5px;
}
ul :nth-child(3) :nth-child(2) {
align-self: center;
background-color: red;
}
ul :nth-child(3) :nth-child(3) {
align-self: flex-end;
}
第4个骰子:
flex-wrap: wrap;属性让点数位置不够就能自动换下一行。设置点数的左右外边距,用于撑开第三个点数去第二行。
ul :nth-child(4) span {
margin: 0 10px;
}
ul :nth-child(4) {
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
第5个骰子:
这里可以先看一下设置完环绕之后的效果(红色为第三个点数),要解决就是让第三个居中,并且让第四个点数下去。
ul :nth-child(5) {
justify-content: space-around;
flex-wrap: wrap;
box-sizing: border-box;
padding: 5px;
}
在做到这个地方的时候,我的想法是给第三个点数加外右边距margin-right,第四个不就下去了!?!,也确实下去了,然后成了这样。但第三个不居中,那怎么办?
那就再来一个左外边距,只要保证中间一行没有点数,配合flex-wrap: wrap;,第三个点数自然就会居中。
ul :nth-child(5) {
justify-content: space-around;
flex-wrap: wrap;
box-sizing: border-box;
padding: 5px;
}
ul :nth-child(5) :nth-child(3) {
background-color: red;
margin-left: 20px;
margin-right: 20px;
}
第6个骰子:
这个比较简单,不做描述
ul :nth-child(6) {
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
}
梳理:
练习完这个flex布局,不仅对flex加深了理解,也对子代选择器更加的熟练了。在进行第三个筛子设置的时候,选择第二个点数的效果迟迟不出现( ul :nth-child(3)),原因是标签要与子代选择器一个间距,怪我太粗心!想要选择骰子内具体的点数,可以在其后再接一个nth-child( ul :nth-child(3) :nth-child(2) )。
大部分情况,flex + margin 就能解决很多布局,其余部分就是细微的调整了。
完整代码:
<!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>
* {
padding: 0;
margin: 0;
list-style: none;
}
ul {
width: 400px;
height: 400px;
background-color: beige;
margin: 20px auto;
border-radius: 25%;
padding: 10px;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
align-items: center;
background-color: green;
}
/* 骰子统一样式 */
li {
width: 100px;
height: 100px;
border: 1px solid black;
background-color: white;
border-radius: 20%;
display: flex;
}
/* 点数统一样式 */
span {
width: 30px;
height: 30px;
background-color: blue;
border-radius: 50%;
}
/* 标签要与:nth-child 隔开一点,不然不生效*/
/* 第 1 个骰子 */
ul :nth-child(1) {
justify-content: center;
align-items: center;
}
/* 第 2 个骰子 */
ul :nth-child(2) {
flex-direction: column;
justify-content: space-around;
align-items: center;
}
/* 第 3 个骰子 */
ul :nth-child(3) {
justify-content: space-around;
box-sizing: border-box;
padding: 5px;
}
ul :nth-child(3) :nth-child(1) {
/* align-self: flex-start; */
}
ul :nth-child(3) :nth-child(2) {
align-self: center;
background-color: red;
}
ul :nth-child(3) :nth-child(3) {
align-self: flex-end;
}
/* 第 4 个骰子 */
ul :nth-child(4) span {
margin: 0 10px;
}
ul :nth-child(4) {
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
/* 第 5 个骰子 */
ul :nth-child(5) {
justify-content: space-around;
flex-wrap: wrap;
box-sizing: border-box;
padding: 5px;
}
ul :nth-child(5) :nth-child(3) {
background-color: red;
margin-left: 20px;
margin-right: 20px;
}
/* 第 6 个骰子 */
ul :nth-child(6) {
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
}
</style>
</head>
<body>
<ul>
<li>
<span></span>
</li>
<li>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
</ul>
</body>
</html>