html – 使用Flexbox,扩展元素以填充行之间的间隙

html – 使用Flexbox,扩展元素以填充行之间的间隙,第1张

概述参见英文答案 > Flexbox row: How to stretch children to fill cross-axis?                                    2个 我觉得这有点傻,但是我对Flexboxes的了解已经筋疲力尽了,所以我希望别人可以进来帮助我. 我的总体目标是让中间行中的两个项目伸展以填充标题和项目之间的空间,我已经四处搜索并且老实说无法弄 参见英文答案 > Flexbox row: How to stretch children to fill cross-axis?2个
我觉得这有点傻,但是我对FlexBoxes的了解已经筋疲力尽了,所以我希望别人可以进来帮助我.

我的总体目标是让中间行中的两个项目伸展以填充标题和项目之间的空间,我已经四处搜索并且老实说无法弄清楚我应该做什么.我分叉了CSS Tricks Guide的代码,最底层的代码,我做了一些改动.我目前拥有的代码是(以全屏模式打开它以使其更清晰):

body,HTML {  height: 100%;}.wrapper {  display: -webkit-Box;  display: -moz-Box;  display: -ms-flexBox;  display: -webkit-flex;  display: flex;  justify-content: flex-start;  -webkit-flex-flow: row wrap;  flex-flow: row wrap;  height: 100%;  Font-weight: bold;  text-align: center;}.wrapper > * {  padding: 10px;  flex: 1 1 100%;}.header {  background: tomato;  height: 50px;  flex: 1 1 100%;}.footer {  background: lightgreen;  height: 50px;}.main {  text-align: left;  align-self: stretch;  background: deepskyblue;}.asIDe-1 {  background: gold;}.asIDe-2 {  background: hotpink;}@media all and (min-wIDth: 600px) {  .asIDe {    flex: 1 auto;  }}@media all and (min-wIDth: 800px) {  .main {    flex: 3 0px;  }  .asIDe-1 {    order: 1;  }  .main {    order: 2;  }  .asIDe-2 {    order: 3;  }  .footer {    order: 4;  }}body {  padding: 2em;}
<div >  <header >header</header>  <article >    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,feugiat vitae,ultricIEs eget,tempor sit amet,ante. Donec eu libero sit amet quam egestas semper. Aenean ultricIEs mi vitae est.      Mauris placerat eleifend leo.</p>  </article>  <asIDe >AsIDe 1</asIDe>  <footer >Footer</footer></div>

FlexBox是否有可能在不改变HTML的情况下实现这一目标,或者我应该寻找另一种方法来实现这一目标?

解决方法 当人们告诉你如何解决问题时,他们并没有告诉你为什么你没有预期的结果.我认为这部分是因为他们中的大多数都错过了实际的问题.我觉得这很有趣.

让我先解决一些问题:

Flex-direction ::出于实际目的,它表示项目的显示方向.然而,它并不准确.

现在让我们说如果方向设置为行,则意味着每个项目必须具有容器的高度,并且它们应该彼此相邻.换句话说,容器必须被视为一行,而项目是列.

.c{  display: flex;  wIDth: 400px;  height:100px;}.c1{  flex-grow: 1;  background:gold;}.c2{  flex-grow: 1;  background:red;}
<div >    <div ></div>    <div ></div></div>

我没有指定高度,项目填充行的高度,并像列一样堆叠在一起.

指定高度时,项目将采用您定义的高度,但不会更改行的高度:

.c{  display: flex;  wIDth: 400px;  height: 100px;}.c1{  flex-grow: 1;  height: 40px;  background:gold;}.c2{  flex-grow: 1;  background:red;}
<div >    <div ></div>    <div ></div></div>

红色立方体仍然会产生垂直空间,因为行的高度没有改变.

flex grow:分配给不同项目的可用空间量.

.c{    display: flex;    wIDth: 400px;  }.c1{  flex-grow: 1;  background:gold;  }.c2{  flex-grow: 1;  background:red;  }
<div >        <div >AAAAAAAAAAAAA</div>        <div ></div>    </div>

尽管具有相同的flex-grow值,但这两个项目的大小不同,这是因为自由空间分布在它们之间,但黄色矩形开始时更大.

首先让我们使用flex-wrap:wrap:

.c{  display: flex;  flex-wrap: wrap;  border: 1px solID black;  wIDth: 400px;  height:100px;}.c1{  wIDth:200px;  background:gold;}.c2{  wIDth:200px;  background:red;}.c3{  wIDth:100px;  background:orange;}.c4{  wIDth:300px;  background:green;}
<div >    <div ></div>    <div ></div>    <div ></div>    <div ></div></div>

我们可以看到,当我们超出可用宽度的数量时,项目开始,有效地创建另一行.

现在来解决你的问题:

如果我们采用上面的例子并设置第一项的高度怎么办?让我们来看看:

.c{      display: flex;      flex-wrap: wrap;      border: 1px solID black;      wIDth: 400px;      height:100px;    }    .c1{      wIDth:200px;      height: 30px;      background:gold;    }    .c2{      wIDth:200px;      background:red;    }    .c3{      wIDth:200px;      background:orange;    }    .c4{      wIDth:200px;      background:green;    }
<div >  <div ></div>  <div ></div>  <div ></div>  <div ></div></div>

就像你的片段一样.

让我们看另一个例子:

.c{          display: flex;          flex-wrap: wrap;          border: 1px solID black;          wIDth: 600px;          height:100px;        }        .c1{          wIDth:400px;          height: 35px;          background:gold;        }        .c2{          wIDth:200px;          background:red;        }        .c3{          wIDth:200px;          background:orange;        }        .c4{          wIDth:200px;          background:green;        }        .c5{          wIDth:200px;          background:purple;        }
<div >      <div ></div>      <div ></div>      <div ></div>      <div ></div>      <div ></div>    </div>

>放置400px X 35px的黄色立方体并跨越2列,然后放置200px的红色立方体并跨越1列.
>此时所有矩形的高度均为0,除了第一个具有35px的矩形.
>剩余的垂直空间在行之间划分,以产生整个垂直空间.因此剩余的垂直空间是100-35 = 65px.
分为2行= 32.5.第一行得到35 32.5,第二行得到32.5px高度.

另一个让事情更清晰的例子:

.c,.d{              display: flex;              flex-wrap: wrap;              border: 1px solID black;              wIDth: 600px;              height:100px;            }            .c1{              flex-shrink: 0;              wIDth:400px;              height: 0px;              background:gold;            }            .c2{              wIDth:200px;              background:red;            }            .c3{              wIDth:200px;              background:orange;            }            .c4{              wIDth:200px;              background:green;            }            .c5{              wIDth:200px;              background:purple;            }            .d1{              wIDth:400px;              height: 50px;              background:gold;            }            .d2{              wIDth:200px;              background:red;            }            .d3{              wIDth:200px;              background:orange;            }            .d4{              wIDth:200px;              background:green;            }            .d5{              wIDth:200px;              background:purple;            }
First item has 0px height,the vertical space remaining (100px) is divIDed between the 2 rows. Both row have 50px height        <div >          <div ></div>          <div ></div>          <div ></div>          <div ></div>          <div ></div>        </div>First item has 35px height,the vertical space remaining (65px) is divIDed between the 2 rows.        <div >          <div ></div>          <div ></div>          <div ></div>          <div ></div>          <div ></div>        </div>

要解决此问题,您可以使用calc()来计算其他行的高度,就像其他人建议的那样.原因是没有更多的自由垂直空间可供共享.

.c{              display: flex;              flex-wrap: wrap;              border: 1px solID black;              wIDth: 600px;              height:100px;            }            .c1{              wIDth:400px;              height: 35px;              background:gold;            }            .c2{              wIDth:200px;              background:red;            }            .c3{              height:calc(100% - 35px);              wIDth:600px;              background:green;            }
<div >          <div ></div>          <div ></div>          <div ></div>        </div>
总结

以上是内存溢出为你收集整理的html – 使用Flexbox,扩展元素以填充行之间的间隙全部内容,希望文章能够帮你解决html – 使用Flexbox,扩展元素以填充行之间的间隙所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1102713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存