爲了實現骰子的實例,先建一個骰子的模型css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> [class$="face"] { margin: 16px; padding: 4px; background-color: #e7e7e7; width: 104px; height: 104px; object-fit: contain; box-shadow: inset 0 5px white, inset 0 -5px #bbb, inset 5px 0 #d7d7d7, inset -5px 0 #d7d7d7; border-radius: 10%; } .item { display: block; width: 24px; height: 24px; border-radius: 50%; margin: 4px; background-color: #333; box-shadow: inset 0 3px #111, inset 0 -3px #555; } </style> </head> <body> </body> </html>
骰子的一面,最多能夠放置9個點。html
<div class="box-face"> <span class="item"></span> </div>
上面代碼中,div元素(表明骰子的一個面)是Flex容器,span元素(表明一個點)是Flex項目。若是有多個項目,就要添加多個span元素,以此類推。
佈局
.box-face { display: flex; justify-content: center; align-items: center; }
<div class="box2-face"> <span class="item"></span> <span class="item"></span> </div>
cssflex
.box2-face { display: flex; justify-content: space-between; /*space-between:兩端對齊,項目之間的間隔都相等。 */ } .box2-face .item:nth-child(2) { align-self: flex-end; }
<div class="box3-face"> <span class="item"></span> <span class="item"></span> <span class="item"></span> </div>
cssui
.box3-face { display: flex; } .box3-face .item:nth-child(2) { align-self: center; } .box3-face .item:nth-child(3) { align-self: flex-end; }
<div class="box4-face"> <div class="column"> <span class="item"></span> <span class="item"></span> </div> <div class="column"> <span class="item"></span> <span class="item"></span> </div> </div>
cssspa
.box4-face { display: flex; justify-content: space-between; } .box4-face .column { display: flex; flex-direction: column; justify-content: space-between; }