CSS - direction 屬性



CSS 的 direction 屬性用於控制元素的文字方向,這會影響該元素內文字的佈局和閱讀順序。它確保文字和其他內聯內容以與預期閱讀方向一致的方式顯示。

語法

direction: ltr | rtl | initial | inherit;

屬性值

描述
ltr 將文字方向設定為從左到右。預設值。
rtl 將文字方向設定為從右到左。
initial 將屬性設定為其預設值。
inherit 從父元素繼承屬性。

CSS Direction 屬性示例

以下示例說明了使用不同值的 direction 屬性。

從左到右的文字方向

要使文字方向從左到右,我們使用 ltr 值。這是預設值。以下示例中顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      p {
         font-size: larger;
         direction: ltr;
      }
   </style>
</head>

<body>
   <h2>
      CSS direction property
   </h2>
   <h4>
      left-to-right direction
   </h4>
   <p>
      This is the left-to-right direction of the text.
   </p>
</body>

</html>

從右到左的文字方向

要使文字方向從右到左,我們使用 rtl 值。這是預設值。以下示例中顯示了這一點。

示例

<!DOCTYPE html>
<html>

<head>
   <style>
      p {
         font-size: larger;
         direction: rtl;
      }
   </style>
</head>

<body>
   <h2>
      CSS direction property
   </h2>
   <h4>
      right-to-left direction
   </h4>
   <p>
      This is the right-to-left direction of the text.
   </p>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
direction 2.0 5.5 1.0 1.3 9.2
css_properties_reference.htm
廣告