简体   繁体   English

自动换行在CSS网格项中不起作用

[英]word-wrap not working in a CSS Grid item

I have a paragraph text without line breaks. 我的段落文字没有换行符。 It is supposed to be wrapped to a new line if the width exceeds the width of the grid container. 如果宽度超过网格容器的宽度,则应该将其换行。 It works if the container is not a grid. 如果容器不是网格,则可以使用。

I tried the solution from Prevent content from expanding grid items but not working. 我尝试了“ 阻止内容扩展网格项目”中的解决方案,但无法正常工作。 A similar question word-wrap in a CSS grid seems to be using tables which is outdated and not recommended in HMTL5 CSS网格中的类似问题自动换行似乎使用的表格已经过时,HMTL5中不建议使用

JSFiddle: https://jsfiddle.net/bvtsan8a/23/ JSFiddle: https ://jsfiddle.net/bvtsan8a/23/

 .container { display: grid; grid-template-rows: auto; width: 500px; background: lightsalmon; min-width: 0; min-height: 0; background:peru; } .child{ padding:30px; margin:30px; width:100%; background:indigo; } .item{ background:indianred; padding:30px; margin:30px; width:100%; } .item p { min-width: 0; min-height: 0; color:gold ; word-wrap: break-word; max-width: 100%; } 
 <div class="container"> <div class="child"> <div class="item"> <p>lolololololololololololololololololololololololololololololololololololololololololo lolololololololololololololo</p> </div> </div> </div> 

Use style word-breaK:break-all as mentioned below: 使用样式word-breaK:break-all ,如下所述:

.item p {
  min-width: 0;
  min-height: 0;
  color:gold ;
  word-wrap: break-word;
  word-break: break-all;
  max-width: 100%;
}

JSFiddle JSFiddle

The reason is that word-wrap: break-word will only break the words. 原因是word-wrap: break-word只会打断单词。 So if for an example, if your paragraph has multiple words and you want to break them then this style will be used. 因此,例如,如果您的段落中有多个单词并且您想打断它们,那么将使用此样式。 And here your text inside p itself is one word. 在这里,您在p内的文本本身就是一个单词。

While word-break: break-all; word-break: break-all; is used when you want to break the word. 当您想打断单词时使用。

try using word-break: break-word; 尝试使用word-break: break-word;

 .container { display: grid; grid-template-rows: auto; width: 500px; background: lightsalmon; min-width: 0; min-height: 0; background:peru; } .child{ padding:30px; margin:30px; background:indigo; } .item{ background:indianred; padding:30px; margin:30px; } .item p { min-width: 0; min-height: 0; color:gold ; word-break: break-word; max-width: 100%; } 
 <div class="container"> <div class="child"> <div class="item"> <p> lolololololololololololololololololololololololololololololololololololololololololololololololololololololololo </p> </div> </div> </div> 

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

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