flex align-items 中心不居中

ttm*_*tmt 5 html css centering flexbox

我有一个代码 - https://codepen.io/anon/pen/MQjpBG

这是一个简单的列表,每个 li 中有一个彩色块和文本

我需要垂直的列表和垂直对齐的文本和块。

我正在尝试使用 flex 和 align-items 来做到这一点,但文本和块永远不会完全居中

我做错了什么,有没有更好的方法来做到这一点。

.element{
  display: flex;
  list-style: none;

  &__item{
    display: flex;
    align-items: center;
    margin-right: 10px;

    &:last-of-type{
      margin-right: 0;
    }

    &-square{
      background: red;
      //display: block;
      display: inline-block;
      height: 20px;
      width: 20px;
    }

    &-text{
      font-weight: bold;
    }

  }

}
Run Code Online (Sandbox Code Playgroud)

我希望块和文本像这样适合。

在此处输入图片说明

align-items: center;似乎做,但它稍微偏离,像

在此处输入图片说明

Mic*_*l_B 7

There's nothing centering them (in your codepen).

Add this to your code:

.element{
    display: flex;
    justify-content: center; /* horizontal alignment */

.element__item {
    display: flex;           /* nested flex container */
    align-items: center;     /* vertical alignment */
}
Run Code Online (Sandbox Code Playgroud)

revised codepen