CSS - white-space 屬性



white-space 屬性用於控制元素中文字內的空白符流。

可能的值

  • normal: 預設值,其中空白符序列會被摺疊,當文字到達可用寬度時會換行。

  • nowrap: 空白符會被摺疊,文字不會換行。它會繼續在同一行,溢位可用寬度。

  • pre: 完全保留 HTML 程式碼中的空白符。換行符和多個空白符會按原樣顯示。

  • pre-wrap: 保留 HTML 程式碼中的換行符和空白符。

  • pre-line: 摺疊空白符,但保留換行符。當文字到達可用寬度時會換行。

  • break-spaces: 摺疊空白符序列,但保留換行符和換行機會。這是一個實驗性值,可能並非所有瀏覽器都支援。

  • initial: 將值設定為其預設值,即 normal。

  • inherit: 從其父元素繼承值。

應用於

所有塊級元素。

DOM 語法

object.style.whiteSpace = "pre";

示例

這是一個示例

   <html>
<head>
<style>
   div {border:2px solid red; padding: 5px; margin: 2px; width: 300px; height: 100px;}
</style>
</head>
<body>
   <h2>White Space</h2>
   <h4>normal</h4>
   <div>
      <p style="white-space: normal;">white space refers to any empty space or characters that do not display
      a visible symbol or have any effect on the text's meaning</p>
   </div>
   <h4>pre</h4>
   <div>
      <p style="white-space: pre;">white space refers to any empty space or characters that do not display 
      a visible symbol or have any effect on the text's meaning</p>
   </div>
   <h4>nowrap</h4>
   <div>
      <p style="white-space: nowrap;">white space refers to any empty space or characters that do not display 
      a visible symbol or have any effect on the text's meaning</p>
   </div>
   <h4>pre-wrap</h4>
   <div>
      <p style="white-space: pre-wrap;">white space refers to any empty space or characters that do not display 
      a visible symbol or have any effect on the text's meaning</p>
   </div>
   <h4>pre-line</h4>
   <div>
      <p style="white-space: pre-line;">white space refers to any empty space or characters that do not display 
      a visible symbol or have any effect on the text's meaning</p>
   </div>
   <h4>break-spaces</h4>
   <div>
      <p style="white-space: break-spaces;">white space refers to any empty space or characters that do not display 
      a visible symbol or have any effect on the text's meaning</p>
   </div>
</body>
</html> 
廣告