HTML - color 屬性



HTML color 屬性用於設定字型元素的文字顏色,可以使用顏色名稱或十六進位制 RGB 格式指定的顏色。

這是一個遺留屬性,您可以使用 CSS 的 'color' 屬性代替。此屬性僅適用於 <font> 和 <hr> 標籤,但是 CSS color 屬性適用於所有元素。

語法

<tag color = 'value'></tag>

其中 value 可以是任何顏色名稱(紅色、綠色等)、任何顏色的十六進位制程式碼或任何顏色的 RGB 程式碼。

應用於

以下列出的元素允許使用 HTML color 屬性。

元素 描述
<font> HTML <font> 標籤用於指定文字的字型。
<hr> HTML <hr> 標籤用於在文件中繪製水平線。

HTML color 屬性示例

以下程式碼演示了在 HTML 中使用 color 屬性。

font 標籤的 color 屬性

在以下示例中,我們在 font 元素中使用 HTML 'color' 屬性來設定此元素的文字顏色。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'color' attribute</title>
</head>

<body>
   <!--HTML 'color' attribute-->
   <p>Example of the HTML 'color' attribute</p>
   <form>
      <input 
         type="text" 
         placeholder="Username" 
         minlength="8">
      <font color='green'>
         Username must have a minimum length of 8
      </font>
      <br>
      <br>
      <input 
         type="password" 
         placeholder="Password">
      <font color='red'>
      Password must contain special characters
      </font>
      <br>
      <br>
      <button>Login</button>
   </form>
</body>

</html>

hr 標籤的 color 屬性

考慮另一種情況,我們將使用 color 屬性與 hr 標籤一起使用。以下程式碼將顯示彩色線條。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'color' attribute</title>
</head>

<body>
   <!--HTML 'color' attribute-->
   <p>Example of HTML 'color' attribute</p>
   <p>Blue line</p>
   <hr color='blue'>
   <p>Red line</p>
   <hr color='red'>
   <p>Green line</p>
   <hr color='green'>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
color
html_attributes_reference.htm
廣告
© . All rights reserved.