首页 > 解决方案 > 制作

相同的长度并出现在列中

问题描述

我有以下结构

<!DOCTYPE html>
<html>

  <head>
    <title></title>
  </head>

  <body>
    <div id='whatStyling'>
      <div class='container'>
        <div class='element'>
          <p class='text'>Foo</p>
        </div>
      </div>
      <div class='container'>
        <div class='element'>
          <p class='text'>Bar</p>
        </div>
      </div>
      <div class='container'>
        <div class='element'>
          <p class='text'>Fooooooo Baaaaaar</p>
        </div>
      </div>
    </div>
  </body>

</html>

标记

#whatStyling {
  display: flex;
  flex-direction: column,
}

.container {
  display: flex;
}

.element {
  display: flex;
  padding: 0.2rem 2.8rem 0.2rem 0.8rem;
  min-width: 1rem;
  background-color: rgb(255, 0, 0);
}

.text {
  color: rgb(128, 128, 128);
  font-family: Roboto, sans-serif;
  font-weight: 500;
  font-size: 1.5rem;
  background-color: rgb(255, 255, 255);
}

http://jsfiddle.net/g8ansow3/6/

我希望所有元素都相同width,即与最长的“框”一样宽是 ( Fooooooo Baaaaaar) 并显示为列。请注意,我需要display: flex;在每个.container. 我进一步不知道会有多少元素。

如果可能的话,我最外层的 div#whatStyling需要什么样的样式flex box

标签: htmlcssflexboxstylingmarkup

解决方案


使用inline-flex而不是flex然后添加width:100%

#whatStyling {
  display: inline-flex; /*Changed this*/
  flex-direction: column; /* Corrected a typo here*/
}

.container {
  display: flex;
}

.element {
  display: flex;
  padding: 0.2rem 2.8rem 0.2rem 0.8rem;
  min-width: 1rem;
  width:100%; /*Added this */
  background-color: rgb(255, 0, 0);
}

.text {
  color: rgb(128, 128, 128);
  font-family: Roboto, sans-serif;
  font-weight: 500;
  font-size: 1.5rem;
  background-color: rgb(255, 255, 255);
}
<div id='whatStyling'>
  <div class='container'>
    <div class='element'>
      <p class='text'>Foo</p>
    </div>
  </div>
  <div class='container'>
    <div class='element'>
      <p class='text'>Bar</p>
    </div>
  </div>
  <div class='container'>
    <div class='element'>
      <p class='text'>Fooooooo Baaaaaar</p>
    </div>
  </div>
</div>


推荐阅读