處理 CSS3 中的文字溢位


text-overflow 屬性用於在 CSS3 中確定如何向用戶表示未顯示的溢位內容。

語法

以下是 text-overflow 屬性的語法 -

text-overflow: value;

值可以是 clip、ellipsis、string 和 initial。你可以使用 string 值設定任意文字。使用 ellipsis 值時,會顯示代表裁剪文字的 ("...") 縮寫。

以下是處理 CSS3 中文字溢位的程式碼 -

裁剪文字

在此示例中,溢位文字被裁剪,且無法使用具有值 clip 的 text-overflow 屬性進行訪問 -

.clip {
   text-overflow: clip;
}

示例

我們來看一個示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         white-space: nowrap;
         width: 100px;
         overflow: hidden;
         border: 1px solid #3008c0;
         margin: 10px;
         padding: 10px;
      }
      .clip {
         text-overflow: clip;
      }
   </style>
</head>
<body>
   <h1>Handling text overflow (clip)</h1>
   <div class="clip">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, beatae.
   </div>
</body>
</html>

使用省略號表示裁剪的文字

在此示例中,溢位文字被裁剪,且無法使用具有值 ellipsistext-overflow 屬性進行訪問 -

.clip {
   text-overflow: ellipsis;
}

示例

我們來看一個示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         white-space: nowrap;
         width: 100px;
         overflow: hidden;
         border: 1px solid #3008c0;
         margin: 10px;
         padding: 10px;
      }
      .ellipsis {
         text-overflow: ellipsis;
      }
   </style>
</head>
<body>
   <h1>Handling text overflow example (ellipsis)</h1>
   <div class="ellipsis">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, beatae.
   </div>
</body>
</html>

使用任意字串表示裁剪的文字

在此示例中,溢位文字被裁剪,且無法使用具有值 ellipsistext-overflow 屬性進行訪問 -

.clip {
   text-overflow: " [..]";
}

示例

我們來看一個示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         white-space: nowrap;
         width: 100px;
         overflow: hidden;
         border: 1px solid #3008c0;
         margin: 10px;
         padding: 10px;
      }
      .myStr {
         text-overflow: " [..]";
      }
   </style>
</head>
<body>
   <h1>Handling text overflow example (string)</h1>
   <div class="myStr">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, beatae.
   </div>
</body>
</html>

更新於:31-10-2023

157 次瀏覽

啟動您的事業

完成課程認證

開始
廣告
© . All rights reserved.