CSS Colors
Understanding and Applying Colors in CSS
1. Hexadecimal Colors
Hexadecimal color codes are written as #RRGGBB, where RR, GG, and BB are two-digit hexadecimal values representing red, green, and blue, respectively.
/* Hexadecimal Color */ .element { color: #FF5733; background-color: #333333; }
Result:
2. RGB Colors
RGB colors use the format `rgb(red, green, blue)` with values ranging from 0 to 255 for each channel.
/* RGB Color */ .element { color: rgb(255, 87, 51); background-color: rgb(51, 51, 51); }
Result:
3. RGBA for Transparency
RGBA extends RGB with an alpha channel, allowing you to specify the transparency level.
/* RGBA Color */ .element { background-color: rgba(0, 0, 0, 0.5); color: rgba(255, 255, 255, 0.8); }
Result:
4. Gradients
Gradients are created using `linear-gradient()` or `radial-gradient()` for smooth color transitions.
/* Linear Gradient */ .element { background: linear-gradient(to right, red, yellow); }
Result:
5. HSL Colors
HSL colors are defined using the format `hsl(hue, saturation%, lightness%)` where hue is a degree on the color wheel (0-360), saturation is a percentage, and lightness is a percentage.
/* HSL Color */ .element { color: hsl(30, 100%, 50%); background-color: hsl(0, 0%, 20%); }
Result:
6. Predefined Color Names
CSS supports 140 predefined color names, such as `red`, `blue`, and `green`.
/* Predefined Color Names */ .element { color: red; background-color: blue; }
Result:
7. Background Color
The `background-color` property specifies the background color of an element.
/* Background Color */ .element { background-color: lightgray; }
Result:
8. Text Color
The `color` property sets the color of the text.
/* Text Color */ .element { color: navy; }
Result:
9. Border Color
The `border-color` property sets the color of the border.
/* Border Color */ .element { border: 2px solid lime; }
Result:
10. Transparency
The `opacity` property allows you to set the transparency of an element.
/* Transparency */ .element { opacity: 0.5; }
Result:
11. Transparent Keyword
The `transparent` keyword specifies a fully transparent color, which can be useful for layering.
/* Transparent Background */ .element { background-color: transparent; border: 2px solid black; }
Result:
12. CurrentColor Keyword
The `currentColor` keyword uses the current text color for other properties, like borders or shadows.
/* CurrentColor Example */ .element { color: blue; border: 2px solid currentColor; }
Result:
13. Named Colors
CSS has 140 predefined color names. These include primary colors (like "red"), shades (like "lightblue"), and special colors (like "rebeccapurple").
/* Named Colors */ .element { color: rebeccapurple; background-color: lightgoldenrodyellow; }
Result:
14. Contrast and Accessibility
Good contrast between text and background is essential for readability. Use tools to ensure your design is accessible.
/* High Contrast Example */ .element { color: black; background-color: white; }
Result:
15. Dark Mode
Use the `prefers-color-scheme` media query to detect user preferences for dark mode and apply appropriate styles.
/* Dark Mode Example */ @media (prefers-color-scheme: dark) { .element { background-color: black; color: white; } }
Result:
16. Opacity
The `opacity` property controls the transparency of an entire element, affecting both background and content.
/* Opacity Example */ .element { opacity: 0.7; }
Result:
17. Blending Modes
The `mix-blend-mode` property defines how an element's content blends with the background.
/* Blending Mode Example */ .element { background-color: red; mix-blend-mode: multiply; }
Result:
18. CSS Variables for Colors
CSS variables allow you to reuse color values throughout your stylesheet, improving maintainability.
/* CSS Variables */ :root { --main-color: #3498db; } .element { color: var(--main-color); }
Result:
19. Color Functions
CSS supports functions like `color()`, `hsl()`, and `rgb()` to define dynamic or device-dependent colors.
/* Color Functions */ .element { color: color(display-p3 1 0.5 0); }
Result:
20. Color Inheritance
The inherit
keyword forces an element to take the color value from its parent element. This is useful for maintaining consistent styling across child elements.
/* Inherit Color Example */ .parent { color: green; } .child { color: inherit; }
Result:
21. Clipping Colors
The `clip-path` property allows you to shape an element while applying colors.
/* Clipping Example */ .element { background-color: orange; clip-path: circle(50%); }