flex布局入门实践

最近抽时间阅读了阮一峰大神的《flex布局教程》,初窥门径,小有心得,不过还不是很不熟练,需要多多使用才能熟练掌握。依据实例篇的一些讲解,自己也尝试实现了骰子的布局。这里跟大家分享一下代码和笔记,觉得参照这个代码来初步理解flex布局还是很好的。

<!DOCTYPE html>
<html lang="en">

<head>
  <title>flex布局</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- <link href="css/style.css" rel="stylesheet"> -->
  <style>
    .container {
      background: #000;
      height: 600px;
      width: 800px;
      display: flex;
      flex-wrap: wrap;
      align-content: space-around;
      justify-content: space-around;
      /* padding: 20px; */
    }

    .box {
      display: flex;
      background: #fff;
      width: 200px;
      height: 200px;
      border-radius: 10%;
      padding: 10px;
      /* margin: 20px; */
    }

    .item {
      background: #000;
      color: #fff;
      text-align: center;
      line-height: 60px;
      border-radius: 50%;
      width: 60px;
      height: 60px;
    }

    .column{
      /* 属性定义了在分配多余空间之前,项目占据的主轴空间(main size) */
      /* flex-basis: <length> | auto; */
      flex-basis: 100%;
      display: flex;
    }
    /* 1 */

    .box:nth-child(1) {
      /* 横向:左边|中间|右边 */
      /* justify-content: flex-start|center|flex-end;  */
      /* 纵向:上边|中间|下边 */
      /* align-items: flex-start|center|flex-end; */
      justify-content: center;
      align-items: center;
    }
    /* 2 */

    .box:nth-child(2) {
      /* 排列间隔:两端对齐|每个项目两侧的间隔相等 */
      /* justify-content: space-between|space-around; */
      justify-content: space-between;
    }

    .box:nth-child(2) .item:nth-child(2) {
      /* 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性 */
      /* align-self: auto | flex-start | flex-end | center; */
      align-self: flex-end;
    }
    /* 3 */

    .box:nth-child(3) {
      justify-content: space-between;
    }

    .box:nth-child(3) .item:nth-child(2) {
      align-self: center;
    }

    .box:nth-child(3) .item:nth-child(3) {
      align-self: flex-end;
    }
    /* 4 */

    .box:nth-child(4) {
      /* 属性定义,如果一条轴线排不下,如何换行 */
      /* flex-wrap: no-wrap|wrap|wrap-reverse; */
      /* 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用 */
      /* align-content: flex-start | flex-end | center | space-between | space-around | stretch; */
      flex-wrap: wrap;
      align-content: space-between;
    }

    .box:nth-child(4) .column {
      justify-content: space-between;
    }
    /* 5 */

    .box:nth-child(5) {
      flex-wrap: wrap;
      align-content: space-between;
    }
    .box:nth-child(5) .column{
      justify-content: space-between;
    }
    .box:nth-child(5) .column:nth-child(2){
      justify-content: center;
    }

    /* 6 */
    .box:nth-child(6) {
      /* 排列方向 */
      /* flex-direction: row|row-reverse|column|column-reverse; */
      flex-wrap: wrap;
      flex-direction: column;
      align-content: space-between;
      justify-content: space-between;
    }




  </style>
</head>

<body>
  <div class="container">
    <!-- 1 -->
    <div class="box">
      <span class="item">1</span>
    </div>
    <!-- 2 -->
    <div class="box">
      <span class="item">1</span>
      <span class="item">2</span>
    </div>
    <!-- 3 -->
    <div class="box">
      <span class="item">1</span>
      <span class="item">2</span>
      <span class="item">3</span>
    </div>
    <!-- 4 -->
    <div class="box">
      <div class="column">
        <span class="item">1</span>
        <span class="item">2</span>
      </div>
      <div class="column">
        <span class="item">3</span>
        <span class="item">4</span>
      </div>
    </div>
    <!-- 5 -->
    <div class="box">
      <div class="column">
        <span class="item">1</span>
        <span class="item">2</span>
      </div>
      <div class="column">
        <span class="item">3</span>
      </div>
      <div class="column">
        <span class="item">4</span>
        <span class="item">5</span>
      </div>
    </div>
    <!-- 6 -->
    <div class="box">
      <span class="item">1</span>
      <span class="item">2</span>
      <span class="item">3</span>
      <span class="item">4</span>
      <span class="item">5</span>
      <span class="item">6</span>
    </div>
  </div>

</body>

</html>

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,634评论 25 709
  • 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法。 你会看到,不管是什么布局,Flex往往都可以...
    老夫的天阅读 835评论 0 10
  • 转载自阮一峰博客 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法。你会看到,不管是什么布局,F...
    林初盛阅读 651评论 0 4
  • 建议大家看完阮一峰老师的Flex 布局教程:语法篇再看这篇文章;另:本文中的思维导图使用新建标签页的方式打开才能看...
    该帐号已被查封_才怪阅读 6,037评论 2 51
  • 曾经想过很多次三十岁的自己,一定是事业成功的,再不济也有不可小觑的经济基础,再再不济也是个一身妖娆的知性白领。 然...
    小面豆仔阅读 416评论 0 1