弹性项目未居中



我有以下布局,它填充了一个包含div的包含div,其中包含许多代表团队成员的圆圈。目前,我有一个奇怪的警告,即由>= 3名团队成员组成的团队实现了预期的结果,但是对于仅由1 || 2名成员组成的团队,圆圈与容器的最左侧对齐。

为什么会这样呢?

.fireteam {
  padding: 0 5px;
}
.fireteam>p {
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  margin: 0 0 0.1rem;
}
.fireteam #fireteamMembers {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  height: 4.2rem;
  list-style: none;
  padding: 0;
  border: 1px solid #000;
  border-radius: 5px;
  overflow: hidden;
}
.fireteam .fireteamMember {
  width: 1.5rem;
  height: 1.5rem;
  background: #000;
  color: white;
  border-radius: 50%;
  margin: 0.25rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>

您的弹性容器缺少align-items: center

将此添加到您的代码中:

.fireteam #fireteamMembers {
  align-items: center; /* NEW */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  height: 4.2rem;
  list-style: none;
  padding: 0;
  border: 1px solid #000;
  border-radius: 5px;
  overflow: hidden;
}

.fireteam {
  padding: 0 5px;
}
.fireteam>p {
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  margin: 0 0 0.1rem;
}
.fireteam #fireteamMembers {
  align-items: center; /* NEW */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  height: 4.2rem;
  list-style: none;
  padding: 0;
  border: 1px solid #000;
  border-radius: 5px;
  overflow: hidden;
}
.fireteam .fireteamMember {
  width: 1.5rem;
  height: 1.5rem;
  background: #000;
  color: white;
  border-radius: 50%;
  margin: 0.25rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>
<div class="fireteam col-xs-4">
  <p>name</p>
  <label>X members</label>
  <ul id="fireteamMembers">
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
    <li class="fireteamMember"></li>
  </ul>
</div>

在列方向容器中,一列只能容纳两个圆圈。下一个包装。

一旦有包装,容器就会有多个柔性线,需要align-content来将它们居中。你已经有了这个。这就是为什么>=3 有效。

但是当只有一个或两个圆时,只有一条弹性线,align-content没有效果。这就是为什么1和2圆圈没有居中。您需要align-items将项目居中到单行柔性容器中。

这里有一个更完整的解释:

  • 弹性换行如何与对齐、对齐项目和对齐内容配合使用?

相关内容

  • 没有找到相关文章

最新更新