如何向 HTML 中新增預格式化文字?


在 HTML 中,<pre> 標籤用於預格式化文字。處於 <pre> 開頭和閉合 </pre> 標籤之間的文字與在 HTML 文件中編寫的文字內容完全相同。預格式化文字的用法如下所示 -

<pre> ---- </pre>

<pre> 元素以保留空格和換行符的形式顯示文字,並以等寬字體表示。

示例

以下示例嘗試在 HTML 中新增預格式化文字 −

<!DOCTYPE html>
<html>
<body>
   <h2> Different variations on Displaying Paragraph</h2>
   <pre>The <b>pre</b> element displayed the text with preserved space and line breaks and also represents in a fixed-width font.</pre>
</body>
</html>

將 CSS 設定應用於 HTML 中的 <pre> 標籤

處於 <pre> 元素之間的文字以等寬字型顯示;可以透過使用 CSS 來更改它。

示例

以下為另一個示例,我們可以在其中向 HTML 中新增預格式化文字 −

<html>
<head>
   <title>Applying CSS to pre tag in HTML</title>
   <style>
      pre {
         font-family: Lucida Handwriting;
         color: blue;
         margin: 30px;
      }
   </style>
</head>
<body>
   <pre>
        Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.
     </pre>
</body>
</html>

示例

考慮 HTML 中 <pre> 標籤的另一個示例,它使用 CSS 建立具有固定寬度的預格式化文字 -

<!DOCTYPE html>
<html>
<body>
   <h2>Usage of standard pre tag</h2>
   <pre>The standard pre tag uses as much space as it needs.</pre>
   <h2>Usage of Fixed width pre</h2>
   <div style="width:150px;overflow:auto">
      <pre>A pre tag with a fixed width. It uses as much space as specified.</pre>
   </div>
</body>
</html>

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      pre {
         display: block;
         font-family: monospace;
         color: blue;
         white-space: pre;
         margin: 1em 0;
      }

      h2 {
         color: red;
      }
   </style>
</head>
<body>
   <h2>Usage of standard pre tag</h2>
   <pre>The standard pre tag uses as much space as it needs.</pre>
   <h3>Usage of Fixed width pre</h3>
   <div style="width:150px;overflow:auto">
      <pre>A pre tag with a fixed width. It uses as much space as specified.</pre>
   </div>
</body>
</html>

更新時間:2023 年 10 月 10 日

593 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.