Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

การกำหนดสีข้อความด้วย CSS color Property

คุณสมบัติ color ใน CSS ใช้กำหนดสีของข้อความในองค์ประกอบ

ไวยากรณ์ (Syntax)

Selector {
  color: /*value*/;
}

ตัวอย่างที่ 1: สีข้อความและพื้นหลังต่างกัน

<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    div {
      height: 100px; width: 200px; margin: 10px;
      display: flex; float: left; font-size: 1.4em;
      word-spacing: 30px; line-height: 4.5em;
      border: thin solid blue; border-radius: 40%;
      text-align: center; color: rebeccapurple;
    }
    div + div {
      background-color: beige; color: red;
      text-align: justify; text-decoration: line-through;
    }
  </style>
</head>
<body>
  <div>One Two</div>
  <div>Three </div>
</body>
</html>

ผลลัพธ์

การกำหนดสีข้อความด้วย CSS color Property

ตัวอย่างที่ 2: สีข้อความใน span ภายใน p

<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    p {
      margin: 10px; display: flex; float: left;
      border: 3px groove green; text-align: center;
      color: rebeccapurple;
    }
    span {
      background-color: beige; color: red; text-decoration: underline;
    }
  </style>
</head>
<body>
  <h2>What is Tableau?</h2>
  <p>Tableau is a Business Intelligence tool for visually analyzing the data.
    <span>Users can create and distribute an interactive and shareable dashboard, which depict the trends, variations, and density of the data in the form of graphs and charts. </span>
    Tableau can connect to files, relational and Big Data sources to acquire and process data.</p>
</body>
</html>

ผลลัพธ์

การกำหนดสีข้อความด้วย CSS color Property

รูปแบบค่าสี (Color Values)

รูปแบบตัวอย่างคำอธิบาย
Named Colorred, rebeccapurpleชื่อสีมาตรฐาน 140+ สี
Hex#ff0000, #f00RGB เลขฐาน 16
RGBrgb(255,0,0)Red, Green, Blue (0-255)
RGBArgba(255,0,0,0.5)RGB + Alpha (0-1)
HSLhsl(0,100%,50%)Hue, Saturation, Lightness
HSLAhsla(0,100%,50%,0.5)HSL + Alpha

การสืบทอด (Inheritance)

color ถูกสืบทอดโดยอัตโนมัติจากพาเรนต์ ไม่ต้องกำหนดซ้ำสำหรับลูกทุกตัว