简体   繁体   English

是否有像 word-wrap 或 overflow-wrap 这样的属性可以强制在单词边界处中断?

[英]Is there a property like word-wrap or overflow-wrap that can force breaks at word boundaries?

For example, If I have a sentence like the following:例如,如果我有如下句子:

I like cute dogs because they're awesome我喜欢可爱的狗,因为它们很棒

doing something like word-break: break-word might give you做类似 word-break 之类的事情:break-word 可能会给你

I like cute dogs because they're awe我喜欢可爱的狗,因为它们令人敬畏

some一些

The behavior I want is for it to break like我想要的行为是让它像

I like cute dogs because they're我喜欢可爱的狗,因为它们

awesome惊人的

I can't seem to find a way to do this.我似乎找不到办法做到这一点。 In most cases, the words do seem to fit efficiently into the container, but there are weird cases with long words that spill out even though I would think it should know how to rearrange them for this not to happen.在大多数情况下,单词似乎确实可以有效地放入容器中,但是有些奇怪的情况下长单词会溢出,即使我认为它应该知道如何重新排列它们以防止这种情况发生。 The words aren't even close in length to the width of the container, so it's not that what I'm trying to achieve is impossible or anything.这些词的长度甚至不接近容器的宽度,所以并不是我想要实现的目标是不可能的。 The CSS I have written is so negligible that it's probably not even worth including, but it's something like:我写的 CSS 是如此微不足道,甚至可能不值得包括在内,但它类似于:

.someClass {
   margin-bottom: 2rem;
   padding: 0.5rem;i 
}

p {
   font-size: 1.1rem;
   margin-bottom: 0.375rem;
}

.someClass's size is a fixed value, and its parent is a flex container. .someClass 的大小是一个固定值,它的父级是一个弹性容器。 I tried adjusting the available space of the flex cell it occupies to exactly the size of the contained element but it made no difference.我尝试将它占据的弹性单元的可用空间调整为所包含元素的大小,但没有任何区别。

  1. Why do words which are only a fraction of the width of the parent overflow sometimes?为什么有时只有父宽度的一小部分的单词会溢出? Like, why aren't they auto arranged to divide the space without overflowing?就像,为什么它们不自动排列来划分空间而不会溢出?
  2. Is there a way to ensure no overflow but without breaking mid-word, and instead breaking at word boundaries有没有办法确保不溢出但不破坏中间词,而是在词边界处破坏

Thanks for the help and cheers.感谢您的帮助和欢呼。

if i understood you correctly word-wrap: break-word;如果我理解正确word-wrap: break-word; does what you need做你需要的

 p { width: 260px; background-color: green; word-wrap: break-word; }
 <p> I like cute dogs because they're awesome </p>

You could use the property overflow-wrap: break-word to start the word in another line.您可以使用属性overflow-wrap: break-word在另一行开始单词。 Code snippet below:下面的代码片段:

 .container { padding: .5rem; overflow-wrap: break-word; background: yellow; width: 100px; }
 <div class="container"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas tellus rutrum tellus pellentesque eu tincidunt. </div>

You could use this你可以用这个

.someClass p {
  word-break: normal;
}

or或者

.someClass p {
   word-break: normal;
   overflow-wrap: anywhere;
}

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

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