如何使包裹的 flex 盒子填充高度

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

how to make a wrapped flex box fill the height

问题

Sure, here's the translated code:

#con {
    height: 400px;
    width: 300px;
    border: solid 1px red;
    display: flex;
    flex-wrap: wrap;
    align-content: stretch;
}

#one {
    border: solid 1px green;
    flex-grow: 1;
}

#two {
    border: solid 1px magenta;
    width: 40px;
}

#tre {
    width: 100%;
    height: 50px;
    border: solid 1px blue;
}
<div id="con">
    <div id="one">1</div>
    <div id="two">2</div>
    <div id="tre">3</div>
</div>

The provided code sets the CSS properties for a container and three inner div elements. The comments and HTML structure are retained as they are.

英文:

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

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

#con {
height: 400px;
width: 300px;
border: solid 1px red;
display: flex;
flex-wrap: wrap;
align-content: stretch;
}

#one {
border: solid 1px green;
flex-grow: 1;
}

#two {
border: solid 1px magenta;
width: 40px;
}

#tre {
width: 100%;
height: 50px;
border: solid 1px blue;
}

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

&lt;div id=&quot;con&quot;&gt;
&lt;div id=&quot;one&quot;&gt;1&lt;/div&gt;
&lt;div id=&quot;two&quot;&gt;2&lt;/div&gt;
&lt;div id=&quot;tre&quot;&gt;3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

if i dont set the height then they autofill, but i dont want both rows to be 50% high

答案1

得分: 0

请使用类来进行样式设置,而不要使用id。如果您不受限于仅使用flexbox,我建议使用grid(如果您必须只有3个div)。

#con {
  height: 400px;
  width: 300px;
  border: solid 1px red;
  /* display: flex;
  flex-wrap: wrap;
  align-content: stretch; */
  display: grid; /* 添加 */
  grid-template-columns: auto 40px; /* 添加 */
  grid-template-rows: auto 50px; /* 添加 */
}

#one {
  border: solid 1px green;
  /* flex-grow: 1; */
}

#two {
  border: solid 1px magenta;
  /* width: 40px; */
}

#tre {
  /* width: 100%;
  height: 50px; */
  border: solid 1px blue;
  grid-column: 1 / 3; /* 添加 */
}
<div id="con">
  <div id="one">1</div>
  <div id="two">2</div>
  <div id="tre">3</div>
</div>
英文:

Please use classes for styling not id's. If you are not limited by using only flexbox, i would use grid (if you must to have only 3 divs)

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

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

      #con {
        height: 400px;
        width: 300px;
        border: solid 1px red;
        /* display: flex;
        flex-wrap: wrap;
        align-content: stretch; */
        display: grid; /* added */
        grid-template-columns: auto 40px; /* added */
        grid-template-rows: auto 50px; /* added */
      }

      #one {
        border: solid 1px green;
        /* flex-grow: 1; */
      }

      #two {
        border: solid 1px magenta;
        /* width: 40px; */
      }

      #tre {
        /* width: 100%;
        height: 50px; */
        border: solid 1px blue;
        grid-column: 1 / 3; /* added */
      }

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

    &lt;div id=&quot;con&quot;&gt;
      &lt;div id=&quot;one&quot;&gt;1&lt;/div&gt;
      &lt;div id=&quot;two&quot;&gt;2&lt;/div&gt;
      &lt;div id=&quot;tre&quot;&gt;3&lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

答案2

得分: -2

只需更改这部分:

#two {
border: solid 1px magenta;
width: 40px;
}

为:

#two {
border: solid 1px magenta;
width: 50px;
}

谢谢

英文:

Juste change this :

#two {
border: solid 1px magenta;
width: 40px;
}

by :

#two {
border: solid 1px magenta;
width: 50px;
}

Thanks

huangapple
  • 本文由 发表于 2023年6月15日 00:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475619.html
匿名

发表评论

匿名网友

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

确定