简体   繁体   English

Flexbox div文本包装

[英]Flexbox div text wrapping

I want to make text in my flexbox layout wrap. 我想在我的flexbox布局包中制作文本。

Code: https://jsfiddle.net/u2oswnth/2/ 代码: https //jsfiddle.net/u2oswnth/2/

 .child { border: solid 1px; display: flex; } .left { width: 100px; } .container { display: flex; flex-wrap: wrap; } 
 <div class="container"> <div class="child left"> Left! </div> <div class="child right"> I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! </div> </div> 

What I have: When exceeding frame width, .right div falls to next row. 我拥有:超过帧宽时, .right div下降到下一行。

What I want: When exceeding frame width, words in .right div start wrapping, two columns stay side-by-side, forming single row. 我想要的:当超过框架宽度时, .right div单词开始包装,两列并排,形成单行。

Change flex-wrap: wrap; 改变flex-wrap: wrap; to flex-wrap: nowrap; flex-wrap: nowrap; . By using flex-wrap: wrap; 通过使用flex-wrap: wrap; you are telling the .right div to wrap onto a new line if it runs out of space. 你告诉.right div如果空间.right新的一行。 As you only want the text itself to wrap you need to use flex-wrap: nowrap; 因为你只想要文本本身包装你需要使用flex-wrap: nowrap; to keep .right on the same line. 保持.right在同一条线上。 The text will automatically wrap when there is not enough space. 当空间不足时,文本将自动换行。

  • Change flex-wrap: wrap; 改变flex-wrap: wrap; to flex-wrap: nowrap; flex-wrap: nowrap; on .container .container

 .child { border: solid 1px; display: flex; } .left { width: 100px; } .container { display: flex; flex-wrap: nowrap; } 
 <div class="container"> <div class="child left"> Left! </div> <div class="child right"> I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! </div> </div> 

NOTE: There is no need to apply "display: flex" on child class and flex-wrap: nowrap; 注意:不需要在子类和flex-wrap: nowrap;上应用"display: flex" flex-wrap: nowrap; on container class because bydefault it is nowrap . 在容器类上,因为默认它是nowrap Below is updated answer and WORKS!!. 下面是更新的答案和WORKS !!

.child {
  border: solid 1px;
}
.left {
  width: 100px;
}
.container {
  display: flex;
} 

<div class="container">
  <div class="child left">
    Left!
  </div>
  <div class="child right">
    I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!!
  </div>
</div>

You should change flex-wrap: wrap; 你应该改变flex-wrap: wrap; to flex-wrap: nowrap; flex-wrap: nowrap;

Please note that flex-wrap: no-wrap; 请注意, flex-wrap: no-wrap; ( no-wrap with a dash ) is NOT a valid value. 没有用破折号包裹 )不是有效值。 Here's the doc reference about it: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap 这是关于它的文档参考: https//developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM