CSS Flexbox学习案例

本文通过两个实例展示了Flex布局的强大。案例一是利用flex-wrap和flex-grow实现CSS多行标签布局,避免了过多的空隙,提升了视觉效果。案例二是通过flex嵌套创建色子布局,利用justify-content属性(如space-between和space-around)展示了不同分布方式,并结合margin自动化布局。这两个案例突显了Flex布局在网页设计中的灵活性和实用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个学习案例,一个是 「精通 CSS 高级 Web 标准解决方案(第 3 版)」 书上的一个多行标签的案例,一个是自己写的色子的布局案例。

色子的布局案例用的是 flex 的嵌套,用 flex 的嵌套的功能真的挺强大的,搭配上合理的 CSS 的层叠特性已经能够完成很多事情了。

  • 案例1,多行标签案例

    在宽度不同的窗口上的效果图:

    multiple-row1

    multiple-row2

    这里主要用到的特性有:

    • 使用 flex-wrap: wrap; 完成了自动这行功能
    • 使用 flex-grow 实现标签的自动增长
    • 使用了 max-width 限制了自增长的最大范围

    float 相比最大的优势在于其自增长,这样的话看起来屏幕上留下来的空隙不会特别多,视觉呈现效果会好一些。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Row wrapping with flexbox - aligning content to stretch</title>
    
      <style>
        body {
          font-family: 'Avenir Next', Avenir, Corbel, 'Franklin Gothic', 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;
        }
        .tags {
          border: 1px solid #c9e1f4;
          display: -webkit-flex;
          display: -ms-flexbox;
          display: flex;
          -webkit-flex-wrap: wrap;
              -ms-flex-wrap: wrap;
                  flex-wrap: wrap;
          /* align-content: stretch; is the default */
          margin: 0;
          padding: 1em;
          list-style: none;
          min-height: 300px;
        }
        .tags li {
          display: inline-block;
          margin: .5em;
          -webkit-flex: 1 0 auto;
              -ms-flex: 1 0 auto;
                  flex: 1 0 auto;
          max-width: 14em;
        }
        .tags a {
          display: block;
          padding: .25em .5em .25em .25em;
          background-color: #c9e1f4;
          border-radius: 0 .25em .25em 0;
          line-height: 1.5;
          text-decoration: none;
          color: #28448f;
          position: relative;
          text-align: center;
        }
        .tags a:before {
          position: absolute;
          content: '';
          width: 0;
          height: 0;
          border: 1em solid transparent;
          border-right-width: .5em;
          border-right-color: #c9e1f4;
          left: -1.5em;
          top: 0;
        }
      </style>
    </head>
    <body>
      <ul class="tags">
        <li><a href="/Binary_planet">Binary planet</a></li>
        <li><a href="/Carbon_planet">Carbon planet</a></li>
        <li><a href="/Coreless_planet">Coreless planet</a></li>
        <li><a href="/Desert_planet">Desert planet</a></li>
        <li><a href="/Dwarf_planet">Dwarf planet</a></li>
        <li><a href="/Earth_analog">Earth analog</a></li>
        <li><a href="/Exoplanet">Exoplanet</a></li>
        <li><a href="/Gas_giant">Gas giant</a></li>
        <li><a href="/Helium_planet">Helium planet</a></li>
        <li><a href="/Ice_giant">Ice giant</a></li>
        <li><a href="/Lava_planet">Lava planet</a></li>
        <li><a href="/Mesoplanet">Mesoplanet</a></li>
        <li><a href="/Ocean_planet">Ocean planet</a></li>
        <li><a href="/Pulsar_planet">Pulsar planet</a></li>
        <li><a href="/Rogue_planet">Rogue planet</a></li>
        <li><a href="/Terrestrial_planet">Terrestrial planet</a></li>
        <li><a href="/Sub-brown_dwarf">Sub-brown dwarf</a></li>
      </ul>
    </body>
    </html>
    
  • 案例2,色子的实现

    效果图:

    dice
    只有2点的布局选择了比较好做的水平布局,4点和5点分别用了 space-aroundspace-between 进行布局,这也能看出两个值之间的差别: space-around 中,首尾两个元素的两边也会被分配空间;space-between 只有元素之间会被分配空间。

    因为将很多的 CSS 都按 类(class) 分类了,所以CSS的代码倒不是很多。居中的方式使用了 margin: auto;,对 dice ——一定要在直接父元素上——添加 justify-content: center; align-items: center; 一样能够完成居中对齐,不过怎么方便怎么来嘛……用 margin: auto; 代码更少一些,结构也清晰一些。

    代码如下:

    <!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>
          /* general css */
          body,
          div {
            display: flex;
          }
          .dice {
            background-color: lightgray;
            border-radius: 5%;
            margin: 10px;
            width: 100px;
            height: 100px;
          }
          .dot {
            height: 20px;
            width: 20px;
            margin: 5px;
            background-color: black;
            border-radius: 50%;
          }
    
          /* flex property */
          .center-align {
            margin: auto;
          }
          .bottom-align {
            align-self: flex-end;
          }
          .flex-col {
            flex-direction: column;
          }
          .space-between {
            justify-content: space-between;
          }
          .space-around {
            justify-content: space-around;
          }
    
          /* special css for dice 1 */
          .dotLarge {
            height: 30px;
            width: 30px;
          }
          /* override dice for dice 1 and 4 */
          .dotRed {
            background-color: red;
          }
        </style>
      </head>
      <body>
        <div class="dice">
          <div class="dot center-align dotLarge dotRed"></div>
        </div>
        <div class="dice">
          <div class="dot center-align"></div>
          <div class="dot center-align"></div>
        </div>
        <div class="dice">
          <div class="dot"></div>
          <div class="dot center-align"></div>
          <div class="dot bottom-align"></div>
        </div>
        <div class="dice space-around">
          <div class="flex-col space-around">
            <div class="dot dotRed"></div>
            <div class="dot dotRed"></div>
          </div>
          <div class="flex-col space-around">
            <div class="dot dotRed"></div>
            <div class="dot dotRed"></div>
          </div>
        </div>
        <div class="dice space-around">
          <div class="flex-col space-between">
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
          <div class="flex-col space-between">
            <div class="dot center-align"></div>
          </div>
          <div class="flex-col space-between">
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
        </div>
        <div class="dice space-around">
          <div class="flex-col space-around">
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
          <div class="flex-col space-around">
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
          <div class="flex-col space-around">
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
        </div>
        <div class="dice space-around">
          <div class="flex-col space-around">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
          <div class="flex-col space-around">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
        </div>
      </body>
    </html>
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值