CSS弹性布局问题:空间分配不均匀问题

huangapple go评论55阅读模式
英文:

css flex layout problem:space allocation not even problem

问题

这是我的代码:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.cell {
  border: 1px solid red;
  display: flex;
  flex-grow: 1;
  margin: 0px;
  padding: 3px;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: 0px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-grow: 1;
  margin: 0px;
}

html {
  height: 100%;
  margin: 0px;
  padding: 0px;
}

body {
  /*border:1px solid black;*/
  height: calc(100% - 20px);
  margin: 10px;
  padding: 0px;
}

<!-- 语言: lang-html -->
<html>
  <body onload="">
    <div class="container">
      <div class="row">
        <div class="cell">Caller</div>
        <div class="cell">Callee</div>
      </div>
      <div class="row">
        <div class="cell">
          sdsdfddd
        </div>
        <div class="cell">sdfdsf</div>
      </div>
    </div>
  </body>
</html>

<!-- 结束代码片段 -->

为什么第一行单元格的空间分配是均匀的,而第二行单元格不均匀呢?如何使第二行的空间分配与第一行相同?

英文:

Here is my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.cell {
  border: 1px solid red;
  display: flex;
  flex-grow: 1;
  margin: 0px;
  padding: 3px;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: 0px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-grow: 1;
  margin: 0px;
}

html {
  height: 100%;
  margin: 0px;
  padding: 0px;
}

body {
  /*border:1px solid black;*/
  height: calc(100% - 20px);
  margin: 10px;
  padding: 0px;
}

<!-- language: lang-html -->

&lt;html&gt;
  &lt;body onload=&quot;&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;cell&quot;&gt;Caller&lt;/div&gt;
        &lt;div class=&quot;cell&quot;&gt;Callee&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;cell&quot;&gt;
          sdsdfddd
        &lt;/div&gt;
        &lt;div class=&quot;cell&quot;&gt;sdfdsf&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

The space allocation for the first-row cells is even, why the second-row cells are not even?
How can I make the space allocation as the first row?

答案1

得分: 1

添加 flex-basis: 0;(或使用 flex: 1 简写)

.cell {
  border: 1px solid red;
  display: flex;
  flex-grow: 1;
  flex-basis: 0;
  margin: 0px;
  padding: 3px;
}
英文:

Add flex-basis: 0; (or use flex:1 shorthand)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.cell {
  border: 1px solid red;
  display: flex;
  flex-grow: 1;
  flex-basis: 0;
  margin: 0px;
  padding: 3px;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: 0px;
}

.row {
  display: flex;
  flex-direction: row;
  flex-grow: 1;
  margin: 0px;
}

html {
  height: 100%;
  margin: 0px;
  padding: 0px;
}

body {
  /*border:1px solid black;*/
  height: calc(100% - 20px);
  margin: 10px;
  padding: 0px;
}

<!-- language: lang-html -->

&lt;html&gt;
  &lt;body onload=&quot;&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;cell&quot;&gt;Caller&lt;/div&gt;
        &lt;div class=&quot;cell&quot;&gt;Callee&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;cell&quot;&gt;
          sdsdfddd
        &lt;/div&gt;
        &lt;div class=&quot;cell&quot;&gt;sdfdsf&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月18日 21:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定