由于填充,Flexbox无法居中文本

时间:2018-02-24 00:40:19

标签: html css reactjs flexbox

我正在尝试创建一个在内容超出宽度时包装的弹性网格。我正在使用flexbox wrap来实现这一目标。问题是内容没有正确对齐,因为所有单词都不包含相同数量的字符。

我试图实现这个设计乍一看似乎很简单:

我最好的尝试就是:

我的代码是:

.bg {
  width: 990px;
  padding: 124px;
  height: auto;
  border-radius: 6px;
  background-color: #fff;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.5);
}

.flexgrid {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  word-wrap: break-word;
}

.wrap {
  display: flex;
  flex-direction: column;
  text-align: center;
  border-top: 1px solid black;
  padding: 33px;
  flex: 1;
  flex-basis: auto;
}

return (
      <div className={classes.container}>
        <div className={classes.bg}>
          <h1>My Flashcards</h1>
          <br />
          <div className={classes.flashcardcontainer}>
            <div className={classes.flexgrid}>
              {decks.map((deck, index) => {
                return (
                  <FlashcardDeck
                    label={deck.label}
                    lastItem={decks.length === index + 1}
                    key={index}
                  />
                );
              })}
            </div>
          </div>
        </div>
      </div>
    );
  }
}

const flashCardDeck = props => {
  return (
    <div className={classes.wrap}>
      <img src={deckImage} />
      <p>{props.label}</p>
    </div>
  );
};

2 个答案:

答案 0 :(得分:1)

您应该设置flex-basis: 0

.wrap {
  display: flex;
  flex-direction: column;
  text-align: center;
  border-top: 1px solid black;
  padding: 33px;
  flex: 1;
  flex-basis:0;
}

&#13;
&#13;
.flex-contain{
display: flex;
}

.flex{
flex: 1;
flex-basis: auto;
border: 1px solid green;
}
.flex-plus-basis{
flex-basis: 0;
}
&#13;
<h3>Without flex-basis:0</h3>
<div class="flex-contain">
<div class="flex">
<p>Some content in this one</p>
</div>
<div class="flex">
<p>Some content in this one</p>
</div>
<div class="flex">
<p>Some awesome and longer content in this one</p>
</div>
<div class="flex">
<p>Some more content in this one</p>
</div>
<div class="flex">
<p>Some other content in this one</p>
</div>
</div>

<h3>With flex-basis:0</h3>

<div class="flex-contain">
<div class="flex flex-plus-basis">
<p>Some content in this one</p>
</div>
<div class="flex flex-plus-basis">
<p>Some content in this one</p>
</div>
<div class="flex flex-plus-basis">
<p>Some awesome and longer content in this one</p>
</div>
<div class="flex flex-plus-basis">
<p>Some more content in this one</p>
</div>
<div class="flex flex-plus-basis">
<p>Some other content in this one</p>
</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将.wrap选择器的Color of the Text, Email addresses, Logo, Some title text, few checkboxes, few radio buttons. 值更改为flex-basis

flex-basis: 25%