overflow-wrap: anywhere 用法演示

overflow-wrap: anywhere 是CSS中较新的值,它允许在任意字符间换行,即使在没有传统换行机会的地方也是如此。

1. overflow-wrap: normal (默认值)
这里有一个超长的英文单词:Supercalifragilisticexpialidocious,它会超出容器边界而不是换行。

长单词不会在内部换行,可能会溢出容器。

2. overflow-wrap: break-word
这里有一个超长的英文单词:Supercalifragilisticexpialidocious,它会在容器边界处换行。

长单词会在任意字符间换行以适应容器宽度。

3. overflow-wrap: anywhere
这里有一个超长的英文单词:Supercalifragilisticexpialidocious,它会在容器边界处换行。

允许在任意字符间换行,甚至在没有传统换行机会的地方。

4. 三种值的对比
normal
https://www.example.com/very/long/url/that/might/cause/overflow/issues
break-word
https://www.example.com/very/long/url/that/might/cause/overflow/issues
anywhere
https://www.example.com/very/long/url/that/might/cause/overflow/issues
5. anywhere 与 break-word 的区别
break-word
这是一个句子withaverylongunbreakableword that might cause overflow
anywhere
这是一个句子withaverylongunbreakableword that might cause overflow

break-word 通常只在内容溢出时才强制换行,而 anywhere 允许在任何可能的地方提前换行。

CSS 代码:


.normal-wrap { overflow-wrap: normal; }
.break-word { overflow-wrap: break-word; }
.anywhere { overflow-wrap: anywhere; }
            
实际应用场景
浏览器兼容性注意

overflow-wrap: anywhere 是较新的CSS特性,可能在一些旧版浏览器中不被支持。在使用时需要注意浏览器兼容性。