参见英文答案 >
Why don’t flex items shrink past content size?1个
我有2个flexBox和一个flexBox.
如果文本大于它的父级,我希望文本分成多行.出于某种原因,如果我发送一个固定值(例如250px)作为宽度,它就可以工作.如果我将宽度设置为100%,它将不会分成多行.
我有2个flexBox和一个flexBox.
如果文本大于它的父级,我希望文本分成多行.出于某种原因,如果我发送一个固定值(例如250px)作为宽度,它就可以工作.如果我将宽度设置为100%,它将不会分成多行.
这是我的代码:
#flexparent { display: flex } #flexchild1 { flex: 1; background-color: green; } #flexchild2 { flex: 1; background-color: red; display: flex; flex-flow: column } #flexchild3 { background-color: purple; width: 100%; word-wrap: break-word; }
<div id="flexparent"> <div id="flexchild1"> FLEXCHILD1 </div> <div id="flexchild2"> FLEXCHILD2 <div id="flexchild3"> ThisisasuperlongsentenceLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamtstLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamtst </div> </div> </div>
这就是我想要的方式,但没有长句中的空格.
#flexparent { display: flex } #flexchild1 { flex: 1; background-color: green; } #flexchild2 { flex: 1; background-color: red; display: flex; flex-flow: column } #flexchild3 { background-color: purple; word-wrap: break-word; }
<div id="flexparent"> <div id="flexchild1"> FLEXCHILD1 </div> <div id="flexchild2"> FLEXCHILD2 <div id="flexchild3"> Thisisasuperlongsentence Loremipsum dolorsitametcon secteturadipisicingelitAsperi resnecessitatibusneu eodioImpeditistenes ciuntescorru ptiessecumrepudia ndaequidolor umIllumtempori busquoerrorcumqu eeximpeditmagnam Lore mipsumdo lorsitam etconsecteturadipisicing elitAsperioresne cessitatibusneueodi oImpeditisten esciuntescorruptiessecumrepudia ndaequidolorumIll umtemporibusq uoerrorc umqueeximpe ditmagn amtstLoremipsu mdolorsitametcon secteturadipisici ngelitAsperio resnecessitati busn </div> </div> </div>
这是我为此做的代码.
>我的代码:https://codepen.io/tomzz/pen/mpJMow
>我想要它但没有250px宽度:https://codepen.io/tomzz/pen/VyLMaW
>我多么想要,但没有句子中的空格:https://codepen.io/tomzz/pen/vpOeJp
解决方法
这是一个最小宽度问题,其中弹性项目不能小于其内容.
min-width的默认值为auto,在这种情况下,它发生在flex项#flexchild2上
给它min-width:0,它会工作.
此外,宽度:100%是不需要的,因为flex“column”项目的align-items默认为拉伸,因此自动获取其父级的全宽,并且可以删除.
堆栈代码段
#flexparent { display: flex } #flexchild1 { flex: 1; background-color: green; } #flexchild2 { flex: 1; background-color: red; display: flex; flex-flow: column; min-width: 0; /* added */ } #flexchild3 { background-color: purple; /*width: 100%; removed */ word-wrap: break-word; }
<div id="flexparent"> <div id="flexchild1"> FLEXCHILD1 </div> <div id="flexchild2"> FLEXCHILD2 <div id="flexchild3"> ThisisasuperlongsentenceLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamtstLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamLoremipsumdolorsitametconsecteturadipisicingelitAsperioresnecessitatibusneueodioImpeditistenesciuntescorruptiessecumrepudiandaequidolorumIllumtemporibusquoerrorcumqueeximpeditmagnamtst </div> </div> </div>