CSS
π¨ Color Definitions in CSS
Colors play a major role in web design and user experience.
In CSS, colors are used for:
- Text
- Backgrounds
- Borders
- Shadows
- Gradients
- UI elements
CSS provides several different ways to define colors.
The four most common color systems are:
- 1.Named Colors
- 2.RGB and RGBA
- 2.HSL and HSLA
- 2.Hexadecimal Colors
1. Named Colors
CSS includes many predefined color names that you can use directly.
These are simple and beginner-friendly.
π Examples
p {
color: red;
}
h1 {
background-color: blue;
}
div {
border-color: green;
}
π‘ Common Named Colors
Some popular named colors include:
- red
- blue
- green
- black
- white
- gray
- orange
- purple
β οΈ Limitation
Named colors are easy to use, but they provide limited color variety.
2. RGB and RGBA
RGB stands for:
- Red
- Green
- Blue
Each color value ranges from:
0 β 255
By combining these values, you can create millions of colors.
π RGB Examples
div {
background-color: rgb(255, 0, 0);
}
p {
color: rgb(0, 0, 255);
}
π Result:
- rgb(255, 0, 0) β Red
- rgb(0, 0, 255) β Blue
π RGBA (RGB + Alpha)
RGBA adds an alpha channel for transparency.
Alpha values range from:
0.0 β fully transparent
1.0 β fully opaque
π RGBA Example
div {
background-color: rgba(255, 0, 0, 0.5);
}
p {
color: rgba(0, 0, 255, 0.7);
}
π‘ This creates semi-transparent colors.
πΉ 3. HSL and HSLA
HSL stands for:
- Hue
- Saturation
- Lightness
This system is often easier for humans to understand and adjust.
π HSL Components
| Component | Meaning |
|---|---|
| Hue | Color type 10Β°β360Β° |
| Saturation | Color intensity 0%β100% |
| Lightness | Brightness 0%β100% |
π HSL Examples
div {
background-color: hsl(0, 100%, 50%);
}
p {
color: hsl(120, 100%, 50%);
}
π Result:
- hsl(0, 100%, 50%) β Red
- hsl(120, 100%, 50%) β Green
π HSLA (HSL + Alpha)
HSLA adds transparency support.
π HSLA Example
div {
background-color: hsla(0, 100%, 50%, 0.5);
}
π‘ Creates a semi-transparent red color.
πΉ 4. Hexadecimal Colors
Hex colors are one of the most widely used color systems in CSS.
They use hexadecimal values to represent RGB colors.
π Hex Format
#RRGGBB
Each pair represents:
- RR β Red
- GG β Green
- BB β Blue
Values range from:
0β9 and AβF
π Hex Examples
div {
background-color: #ff0000;
}
p {
color: #00ff00;
}
h1 {
border-color: #0000ff;
}
π Result:
- #ff0000 β Red
- #00ff00 β Green
- #0000ff β Blue
π― Combining Multiple Color Methods
Different color systems can be used together in the same element.
π Example
div {
background-color: #3498db;
color: rgba(255, 255, 255, 0.8);
border: 2px solid hsl(200, 80%, 40%);
}
β¨ Short Hex Syntax
Hex colors can also use shorthand notation.
| Short | Full |
|---|---|
| #f00 | #ff0000 |
| #0f0 | #00ff00 |
| #00f | #0000ff |
π― Combining Multiple Color Methods
Different color systems can be used together in the same element.
π Example
div {
background-color: #3498db;
color: rgba(255, 255, 255, 0.8);
border: 2px solid hsl(200, 80%, 40%);
}
π In This Example:
- Background uses Hex
- Text uses RGBA
- Border uses HSL
π Quick Comparison
| Method | Advantages | Best Use |
|---|---|---|
| Named Colors | Simple and readable | Quick styling |
| RGB/RGBA | Precise color control | Transparency effects |
| HSL/HSLA | Easier color adjustment | Design systems |
| Hex | Compact and widely supported | General web design |
π§ Quick Summary
- CSS supports multiple color formats
- RGB and Hex are the most commonly used systems
- RGBA and HSLA allow transparency control
- HSL makes color adjustments easier
- Hex colors are highly compatible across browsers
