Màu sắc và Background trong CSS
CSS hỗ trợ nhiều cách để định nghĩa màu sắc.
CSS có 140 tên màu được định nghĩa sẵn.
h1 {
color: red;
}
p {
color: blue;
}
div {
background-color: lightblue;
}
Định nghĩa màu bằng mã Hex: #RRGGBB
/* Đỏ */
color: #ff0000;
/* Xanh dương */
color: #0000ff;
/* Tím */
color: #667eea;
/* Viết tắt: #rgb */
color: #f00; /* Tương đương #ff0000 */
color: #00f; /* Tương đương #0000ff */
Định nghĩa màu bằng Red, Green, Blue (0-255).
/* Đỏ */
color: rgb(255, 0, 0);
/* Xanh lá */
color: rgb(0, 255, 0);
/* Tím nhạt */
color: rgb(200, 150, 255);
RGB + độ trong suốt (alpha: 0.0 - 1.0).
/* Đỏ trong suốt 50% */
background: rgba(255, 0, 0, 0.5);
/* Xanh trong suốt 30% */
background: rgba(0, 100, 255, 0.3);
/* Đen trong suốt 80% */
background: rgba(0, 0, 0, 0.8);
Hue (Màu sắc 0-360), Saturation (Độ bão hòa 0-100%), Lightness (Độ sáng 0-100%).
/* Đỏ */
color: hsl(0, 100%, 50%);
/* Xanh lá */
color: hsl(120, 100%, 50%);
/* Xanh dương */
color: hsl(240, 100%, 50%);
/* HSLA - có độ trong suốt */
background: hsla(240, 100%, 50%, 0.5);
| Cách | Ví dụ | Ưu điểm | Nhược điểm |
|---|---|---|---|
| Color Name | red |
Dễ nhớ, dễ đọc | Giới hạn màu |
| Hex | #ff0000 |
Ngắn gọn, phổ biến | Khó đọc giá trị |
| RGB | rgb(255,0,0) |
Dễ hiểu giá trị | Dài hơn Hex |
| RGBA | rgba(255,0,0,0.5) |
Có độ trong suốt | - |
| HSL/HSLA | hsl(0,100%,50%) |
Dễ điều chỉnh tone | Ít phổ biến hơn |
/* Background đơn sắc */
div {
background-color: #f0f0f0;
}
/* Background trong suốt */
.overlay {
background-color: rgba(0, 0, 0, 0.5);
}
/* Background gradient */
.gradient {
background: linear-gradient(to right, #667eea, #764ba2);
}
div {
background-image: url('image.jpg');
}
/* Gradient */
div {
background-image: linear-gradient(to bottom, #667eea, #764ba2);
}
/* Nhiều background */
div {
background-image:
url('overlay.png'),
url('background.jpg');
}
/* Không lặp lại */
background-repeat: no-repeat;
/* Lặp theo chiều ngang */
background-repeat: repeat-x;
/* Lặp theo chiều dọc */
background-repeat: repeat-y;
/* Lặp cả 2 chiều (default) */
background-repeat: repeat;
/* Từ khóa */
background-position: center;
background-position: top right;
background-position: bottom left;
/* Pixel */
background-position: 50px 100px;
/* Phần trăm */
background-position: 50% 50%;
/* Mix */
background-position: center 20px;
/* Cover - Phủ kín, có thể bị cắt */
background-size: cover;
/* Contain - Hiển thị hết ảnh */
background-size: contain;
/* Kích thước cụ thể */
background-size: 200px 150px;
/* Phần trăm */
background-size: 100% auto;
/* Tự động */
background-size: auto;
/* Background cuộn theo (default) */
background-attachment: scroll;
/* Background cố định (parallax effect) */
background-attachment: fixed;
/* Background cố định theo viewport */
background-attachment: local;
/* Cách viết đầy đủ */
div {
background-color: #f0f0f0;
background-image: url('image.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
/* Cách viết ngắn gọn */
div {
background: #f0f0f0 url('image.jpg') no-repeat center / cover;
}
/* Thứ tự: color image repeat position / size */
background-size: cover cho hero sections/* Gradient từ trên xuống */
background: linear-gradient(red, blue);
/* Gradient từ trái sang phải */
background: linear-gradient(to right, red, blue);
/* Gradient chéo */
background: linear-gradient(45deg, red, blue);
/* Gradient nhiều màu */
background: linear-gradient(to right, red, yellow, green);
/* Gradient với color stops */
background: linear-gradient(to right,
red 0%,
yellow 50%,
green 100%
);
/* Gradient tròn từ giữa */
background: radial-gradient(red, blue);
/* Gradient hình elip */
background: radial-gradient(circle, red, blue);
/* Gradient tại vị trí cụ thể */
background: radial-gradient(at top left, red, blue);
/* Gradient với size */
background: radial-gradient(circle 200px, red, blue);
/* Gradient xoay tròn */
background: conic-gradient(red, yellow, green, blue, red);
/* Pie chart */
background: conic-gradient(
red 0deg 90deg,
yellow 90deg 180deg,
green 180deg 270deg,
blue 270deg 360deg
);
Tạo trang hiển thị bảng màu của brand:
Tạo hero section với:
Tạo button với các effect:
/* Hero Section */
.hero {
height: 100vh;
background-image:
linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
url('https://unsplash.com/photos/random/1920x1080');
background-size: cover;
background-position: center;
background-attachment: fixed;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.hero h1 {
color: white;
font-size: 4rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
.hero p {
color: white;
font-size: 1.5rem;
margin-top: 20px;
}