🎨 Bài 3: Colors & Backgrounds

Màu sắc và Background trong CSS

🌈 1. CSS Colors - Màu sắc

CSS hỗ trợ nhiều cách để định nghĩa màu sắc.

📍 1.1. Color Names (Tên màu)

CSS có 140 tên màu được định nghĩa sẵn.

h1 {
  color: red;
}

p {
  color: blue;
}

div {
  background-color: lightblue;
}
📺 Một số màu phổ biến:
red
blue
green
yellow
orange
purple
pink
gray
💡 Lưu ý: Tên màu dễ nhớ nhưng hạn chế về lựa chọn. Trong thực tế, ta thường dùng Hex hoặc RGB.

📍 1.2. Hex Colors (Mã Hex)

Đị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 */
📺 Ví dụ Hex Colors:
#ff0000
#00ff00
#0000ff
#ffff00
#ff00ff
#00ffff
#667eea
#764ba2

📍 1.3. RGB Colors

Đị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);

📍 1.4. RGBA Colors (RGB + Alpha)

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);
📺 Demo RGBA - Độ trong suốt:
100% opacity (rgba 1.0)
70% opacity (rgba 0.7)
40% opacity (rgba 0.4)
10% opacity (rgba 0.1)

📍 1.5. HSL Colors

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);

📊 So sánh các cách định nghĩa màu

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

🎨 2. Background Color

/* 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);
}
📺 Demo Background Colors:
Background: #3498db (Solid Color)
Background: rgba(52, 152, 219, 0.3) (Transparent)
Background: Linear Gradient

🖼️ 3. Background Image

📍 3.1. background-image

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');
}

📍 3.2. background-repeat

/* 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;

📍 3.3. background-position

/* 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;

📍 3.4. background-size

/* 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;
📺 Demo background-size:
background-size: cover
background-size: contain

📍 3.5. background-attachment

/* Background cuộn theo (default) */
background-attachment: scroll;

/* Background cố định (parallax effect) */
background-attachment: fixed;

/* Background cố định theo viewport */
background-attachment: local;

📍 3.6. Background Shorthand

/* 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 */
✅ Best Practices:
  • Luôn set background-color làm fallback khi ảnh không load
  • Dùng background-size: cover cho hero sections
  • Optimize ảnh trước khi dùng làm background
  • Dùng gradient để tạo overlay cho ảnh background

🌈 4. CSS Gradients

📍 4.1. Linear Gradient

/* 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%
);
📺 Demo Linear Gradients:
to right
to bottom
45deg
multi-color

📍 4.2. Radial Gradient

/* 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);
📺 Demo Radial Gradients:
circle
ellipse

📍 4.3. Conic Gradient

/* 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
);

🎯 Bài tập thực hành

Bài 1: Tạo Color Palette

Tạo trang hiển thị bảng màu của brand:

Bài 2: Hero Section với Background

Tạo hero section với:

Bài 3: Button với Gradient

Tạo button với các effect:

💡 Gợi ý code mẫu Bài 2:
/* 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;
}