Loading...
Loading...
Use named colors, hex codes, and backgrounds to add visual appeal
CSS offers many ways to specify colors. You can use named colors, hex codes, RGB, HSL, and more. Backgrounds can be solid colors, gradients, or even images.
/* Named colors ā easy to remember */
h1 { color: tomato; }
/* Hex codes ā most common in professional code */
h1 { color: #ff6347; }
/* RGB ā red, green, blue values (0-255) */
h1 { color: rgb(255, 99, 71); }
/* RGBA ā RGB with opacity (alpha: 0-1) */
h1 { color: rgba(255, 99, 71, 0.5); }| Format | Example | When to use |
|---|---|---|
| Named | `red`, `blue`, `tomato` | Prototyping, simple colors |
| Hex | `#ff6347` | Production ā most common |
| RGB | `rgb(255, 99, 71)` | When you need precise values |
| RGBA | `rgba(255, 99, 71, 0.5)` | When you need transparency |
body {
background-color: #f0f0f0;
}
.highlight {
background-color: yellow;
}.hero {
background-image: url("hero-bg.jpg");
background-size: cover; /* Fit the entire area */
background-position: center; /* Center the image */
background-repeat: no-repeat;/* Don't tile */
}.button {
background: linear-gradient(to right, #ff7e5f, #feb47b);
/* Gradient from left to right: coral ā peach */
}<div style="background-color: #2c3e50; color: white; padding: 20px;">
<h2 style="color: #e74c3c;">Warning!</h2>
<p>This is a dark card with a red heading.</p>
</div>| ā Wrong | Why | ā Right |
|---|---|---|
| `color: #fff` on white background | White text on white = invisible | Always contrast text and background |
| `background-image: url(photo.jpg)` without `background-size: cover` | Image may repeat or stretch oddly | Add `background-size: cover` |
| Forgetting the `#` on hex codes | `ff0000` is invalid ā needs `#` | `#ff0000` |
| Using too many bright colors | Visually exhausting | Stick to 2-3 main colors + neutrals |
Style a page with:
` with dark text (`#333`)
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
h1 {
color: #1a237e;
text-align: center;
}
.card {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
}
.card p {
color: #333;
font-size: 16px;
}
</style>
</head>
<body>
<h1>My Colorful Page</h1>
<div class="card">
<p>This is a card with a white background and dark text.</p>
</div>
</body>
</html>