CSS - word-break 属性



CSS 中的word-break 属性用于指定当单词超过元素可用宽度时如何换行或换字。它决定浏览器是否允许单词在任何位置换行,或者是否应将它们保持在一起。

可能的值

  • normal:使用默认换行规则。

  • break-all:应用断字,以防止溢出。

  • keep-all:不为中文、日文和韩文使用断字;对于其他语言,应为 normal。

  • break-word:与 anywhere 相同,即应用任何单词处的断字。但此值已弃用。

应用于

所有 HTML 元素。

DOM 语法

object.style.wordBreak = "break-all";

示例

这是一个例子

<html>
<head>
<style>
   p {
      border: 2px solid green;
      width: 200px;
   }
   .normal {
      word-break: normal;
   }
   .all {
      word-break: break-all;
   }
   .keep {
      word-break: keep-all;
   }
   .wordbreak {
      word-break: break-word;
   }
</style>
</head>
<body>
   <h2>Word Break</h2>
   <p class="normal">normal - CSS provides property <b>word-break</b> that is useful in determining how to break words in a block of text</p>
   <p class="all">break-all - CSS provides property <b>word-break</b> that is useful in determining how to break words in a block of text</p>
   <p class="keep">keep-all - CSS provides property <b>word-break</b> that is useful in determining how to break words in a block of text</p>
   <p class="wordbreak">break-word - CSS provides property <b>word-break</b> that is useful in determining how to break words in a block of text</p>
</body>
</html> 
广告