css - 在横轴上调整内容 - flex

标签 css flexbox

在主轴上,justify-contentspace-betweenspace-around 看起来很直观。

enter image description here

-------------------------------------------- --------------

对于以下元素,

<!doctype html>
<html>
  <head>
  <meta charset="UTF-8">
  <title>Sample document</title>
  <link rel="stylesheet" href="style.css">
  </head>
  <body>
     <div class = "container">
      <div class = "box box1">1</div>
      <div class = "box box2">2</div>
      <div class = "box box3">3</div>
      <div class = "box box4">4</div>
      <div class = "box box5">5</div>
      <div class = "box box6">6</div>
      <div class = "box box7">7</div>
      <div class = "box box8">8</div>
      <div class = "box box9">9</div>
      <div class = "box box10">10</div>
     </div>

  </body>
</html>

在横轴上有样式,

.box{
    color: white;
    font-size: 100px;
    text-align: center;
    text-shadow: 4px 4px 0 rgba(0, 0, 0, 0, 1);
}

.box1{ background: #1abc9c; }
.box2{ background: #3498db; }
.box3{ background: #9b59b6; }
.box4{ background: #34495e; }
.box5{ background: #f1c40f; }
.box6{ background: #e67e22; }
.box7{ background: #e74c3c; }
.box8{ background: #bdc3c7; }
.box9{ background: #2ecc71; }
.box10{ background: #16ae85; }


/* Property of parent container - start */
/* Justify-content works on main axis of the border*/
.container{
    display:flex;
    justify-content: space-between;
    flex-direction: column;
}
/* Property of parent container - start */

下面是横轴上 space-betweenspace-around 的输出:

enter image description here


在上面的输出中,

space-aroundspace-between 是什么意思?

最佳答案

来自 https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content

justify-content: space-between; /* Distribute items evenly
                               The first item is flush with the start,
                               the last is flush with the end */

justify-content: space-around;  /* Distribute items evenly
                               Items have a half-size space
                               on either end */

在第二个示例中,您没有为列设置高度,因为列与内容一样高,所以没有填充空间,因此周围的空间和之间的空间不会真正占用任何额外空间

在您的 flex 为行的第一个示例中,由于容器的宽度为 100% 并且每个元素的宽度加起来不等于该宽度,所以有额外的空间需要填充

关于css - 在横轴上调整内容 - flex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177962/

相关文章:

html - 如何使用 flex 属性阻止文本在每个空间的下一行继续?

html - 使用 CSS 网格或 Flexbox 在垂直列中排序元素?

html - 为什么添加第三个元素会破坏此 CSS 关键帧 slider ?

css - Material Design/Materialize 3列列表

css - contentPlaceHolder 内的 Divs 或 Divs 内的 ContentPlaceHolder?

javascript - 垂直响应背景图片

DIV 容器内的 HTML 图像底部对齐

jquery - 即使页面重新加载,如何使用复选框保持主题颜色?

css - 将 flexbox 元素与 block 中最后一个文本行的基线对齐

javascript - 在 li 上切换下拉菜单,但防止在 ul 上切换点击