CSS3【display: flex;】与【justify-content: 主轴对齐方式;】的使用

317 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    body {
      background-color: #eee;
      font-size: 22px;
    }
    h3 {
      margin: 10px;
      font-weight: normal;
    }
    section {
      width: 1000px;
      margin: 0 auto;
    }
    ul {
      background-color: #fff;
      border: 1px solid #ccc;
    }
    ul li {
      width: 200px;
      height: 200px;
      background-color: pink;
      margin: 10px;
    }

    section:nth-child(1) ul {
      display: flex;
      /* 主轴对齐方式:左对齐 */
      justify-content: flex-start;
    }
    section:nth-child(2) ul {
      display: flex;
      /* 主轴对齐方式:右对齐 */
      justify-content: flex-end;
    }
    section:nth-child(3) ul {
      display: flex;
      /* 主轴对齐方式:居中 */
      justify-content: center;
    }
    section:nth-child(4) ul {
      display: flex;
      /* 主轴对齐方式:平均分配居中 */
      justify-content: space-around;
    }
    section:nth-child(5) ul {
      display: flex;
      /* 主轴对齐方式:两端对齐,中间的进行平分展示 */
      justify-content: space-between;
    }
  </style>
</head>
<body>
  <section>
    <h3>主轴的对齐方式 justify-content: flex-start;</h3>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </section>
  <section>
    <h3>主轴的对齐方式 justify-content: flex-end;</h3>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </section>
  <section>
    <h3>主轴的对齐方式 justify-content: center;</h3>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </section>
  <section>
    <h3>主轴的对齐方式 justify-content: space-around;</h3>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </section>
  <section>
    <h3>主轴的对齐方式 justify-content: space-between;</h3>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </section>
</body>
</html>
  • demo 效果: