Computer >> Máy Tính >  >> Lập trình >> CSS

Đặt màu văn bản của phần tử bằng CSS

Thuộc tính CSS color được sử dụng để thay đổi màu văn bản của một phần tử. Chúng tôi có thể chỉ định các giá trị dưới dạng tên màu tiêu chuẩn, rgb (), rgba (), hsl (), hsla () và giá trị thập lục phân.

Cú pháp

Cú pháp cho thuộc tính màu CSS như sau -

Selector {
   color: /*value*/
}

Các ví dụ sau minh họa thuộc tính màu CSS -

Ví dụ

<!DOCTYPE html>
<html>
<head>
<style>
div {
   height: 50px;
   width: 50px;
   float: right;
   color: white;
   background-color: #2f5587;
}
p {
   color: rgba(225,5,135,0.7);
   border: 2px solid #16c618;
   box-shadow: 0 7px 0 5px hsl(90, 60%, 70%);
}
</style>
</head>
<body>
<h2>Example Heading</h2>
<div>This is it</div>
<p>Demo text is visible here.</p>
</body>
</html>

Đầu ra

Điều này cho kết quả sau -

Đặt màu văn bản của phần tử bằng CSS

Ví dụ

<!DOCTYPE html>
<html>
<head>
<style>
li:nth-of-type(even) {
   color: dodgerblue;
   border: 6px groove #16c618;
   padding: 4px;
   box-shadow: inset -100px 0 5px red;
}
</style>
</head>
<body>
<h2>Learn Servlets</h2>
<ul>
<li>Java Servlets are programs that run on a Web or Application server.</li>
<li>Java Servlets often serve the same purpose as programs implemented using the CGI.</li>
<li>Servlets are platform-independent because they are written in Java.
</li>
</ul>
</body>
</html>

Đầu ra

Điều này cho kết quả sau -

Đặt màu văn bản của phần tử bằng CSS