【问题标题】:Flexbox cannot center text because of padding由于填充,Flexbox 无法使文本居中
【发布时间】:2018-08-04 02:50:24
【问题描述】:

我正在尝试创建一个 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>
  );
};

【问题讨论】:

    标签: html css reactjs flexbox


    【解决方案1】:

    将 .wrap 选择器的 flex-basis 值更改为 flex-basis: 25%

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

    【讨论】:

    • 谢谢,我试试看。
    • 它起作用了,但我不知道它为什么起作用。它反应灵敏,应有尽有。如果你有时间,你能向我解释一下它为什么有效吗?
    • 您的布局是 4 列,这就是我们添加“flex-basis: 25%;”的原因.总弯曲宽度为 100%。我认为您最好阅读此链接,它将对您有很大帮助css-tricks.com/snippets/css/a-guide-to-flexbox
    • 非常感谢。
    【解决方案2】:

    你应该设置flex-basis: 0

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

    .flex-contain{
    display: flex;
    }
    
    .flex{
    flex: 1;
    flex-basis: auto;
    border: 1px solid green;
    }
    .flex-plus-basis{
    flex-basis: 0;
    }
    <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>

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2019-08-19
    • 2017-09-06
    相关资源
    最近更新 更多