使用 CSS 的紅綠藍模型 (RGB) 定義顏色


在網站設計中,顏色至關重要。它會影響使用者點選的連結、他們閱讀資訊的方式以及他們在瀏覽您的網站時的舒適度。在整合顏色時需要練習,而當您使用 CSS 的 color 和 background-color 屬性時,將其新增到您的網站非常簡單。

這些屬性可以透過多種方式定義。可以使用 HTML 顏色名稱、十六進位制顏色程式碼、RGB 顏色程式碼或 HSL 顏色值來更改網頁的文字或背景顏色。在本文中,我們將學習有關 RGB 顏色的知識。

RGB(紅綠藍)

可以透過定義 R、G 和 B 值範圍從 0 到 255,使用RGB(紅、綠、藍)格式來定義 HTML 元素的顏色。例如,黑色(0、0、0)、白色(255、255、255)、黃色(255、255、0)等的顏色 RGB 值。

語法

以下是 RGB 顏色格式的語法:

rgb(red, green, blue)

示例

讓我們看一下以下示例,我們將使用 RGB 顏色

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: verdana;
         text-align: center;
         background-color: rgb(213, 245, 227);
      }
      h1 {
         color: rgba(142, 68, 173);
      }
      P {
         color: rgb(222, 49, 99);
      }
   </style>
</head>
<body>
   <h1> TUTORIALSPOINT </h1>
   <P>Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.</P>
</body>undefined
</html>

當我們執行以上程式碼時,它將生成一個輸出,其中包含應用了 CSS 的文字顯示在網頁上。

示例

考慮另一種情況,我們將看到 RGB 顏色的用法。

<!DOCTYPE html>
<html>
<head>
   <style>
      .tp {
         position: absolute;
         inset: 0;
         margin: auto;
         width: 200px;
         height: 200px;
         border-radius: 10px;
         transform: rotate(69deg);
      }
      div {
         height: 250px;
         margin-top: 250px;
         background: rgb(249, 231, 159);
      }
      .tp {
         background: rgb(30 132 73/0.8);
      }
   </style>
</head>
<body>
   <div>
      <div class="tp"></div>
   </div>
</body>
</html>

執行以上程式碼後,輸出視窗將彈出,在網頁上顯示應用了 CSS 的 div 框

更新於:2024年1月8日

152 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.