强制弹性项目跨越整个行宽

我试图将前两个子元素保留在同一行中,而第三个元素以全宽度保留在自己的下面,所有这些都使用flex。


我对在前两个元素上使用flex-grow和flex-shrink属性特别感兴趣(这是我不使用百分比的原因之一),但是第三个元素确实必须为全角且在前两个元素以下。


当发生错误并且我无法更改代码时,以label编程方式将元素添加到text元素之后。


如何强制label元素在使用flex定位的其他两个元素下方跨越100%的宽度?


.parent {

  display: flex;

  align-items: center;

  width: 100%;

  background-color: #ececec;

}


.parent * {

  width: 100%;

}


.parent #text {

  min-width: 75px;

  flex-shrink: 2.25;

}

<div class="parent">

  <input type="range" id="range">

  <input type="text" id="text">

  <label class="error">Error message</label>

</div>


不负相思意
浏览 524回答 2
2回答

眼眸繁星

每当您希望弹性项目占据整个行时,请将其设置为width: 100%/ flex-basis: 100%,然后wrap在容器上启用它。现在,该物品会消耗所有可用空间。兄弟姐妹被迫移至其他行。.parent {&nbsp; display: flex;&nbsp; flex-wrap: wrap;&nbsp; align-items: center;&nbsp; background-color: #ececec;}.error {&nbsp; flex: 0 0 100%; /* fg: 0, fs: 0, fb: 100% */&nbsp; border: 1px dashed red;}#range, #text {&nbsp; flex: 1;}<div class="parent">&nbsp; <input type="range" id="range">&nbsp; <input type="text" id="text">&nbsp; <label class="error">Error message</label></div>

潇潇雨雨

您似乎在寻找min-width孩子和flex-wrap:wrap父母:.parent {&nbsp; &nbsp;display: flex;&nbsp; &nbsp;align-items: center;&nbsp; &nbsp;flex-wrap: wrap;}.parent > * {&nbsp; flex-grow: 1;}.parent > .error {&nbsp; min-width: 100%;&nbsp; background-color: rgba(255,0,0,.3);}<div class="parent">&nbsp; <input type="range" id="range">&nbsp; <input type="text" id="text">&nbsp; <label class="error">Error message</label></div>
打开App,查看更多内容
随时随地看视频慕课网APP