CSS3 - flexbox


参考:Flex 布局教程:实例篇#

为了实现骰子的实例,先建一个骰子的模型

<!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>

一、骰子的布局#

1.一点(单项目)#

骰子的一面,最多可以放置9个点。

<div class="box-face">
        <span class="item"></span>
</div>

上面代码中,div元素(代表骰子的一个面)是Flex容器,span元素(代表一个点)是Flex项目。如果有多个项目,就要添加多个span元素,以此类推。

.box-face {
            display: flex;
            justify-content: center;    
            align-items: center;
        }

2.两点(双项目)#

<div class="box2-face">
        <span class="item"></span>
        <span class="item"></span>
    </div>

css

	.box2-face {
            display: flex;
            justify-content: space-between; /*space-between:两端对齐,项目之间的间隔都相等。 */
        }

        .box2-face .item:nth-child(2) {
            align-self: flex-end;
            }

3.三点(三项目)#

<div class="box3-face">
        <span class="item"></span>
        <span class="item"></span>
        <span class="item"></span>
    </div>

css

      .box3-face {
            display: flex;
        }

        .box3-face .item:nth-child(2) {
            align-self: center;
        }

        .box3-face .item:nth-child(3) {
            align-self: flex-end;
        }

4.四点(四项目)#

    <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>

css

        .box4-face {
            display: flex;
            justify-content: space-between;
        }

        .box4-face .column {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

作者:【唐】三三

出处:https://www.cnblogs.com/tangge/p/11222987.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   【唐】三三  阅读(212)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-07-21 MVC - 12.HtmlHelper
2013-07-21 WebService – 2.动态调用WebService
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示