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:

This is a sample text with hexadecimal colors.

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:

This is a sample text with RGB colors.

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:

This is a sample text with RGBA colors.

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:

This is a box with a linear gradient background.

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:

This is a sample text with HSL colors.

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:

This is a sample text with predefined color names.

7. Background Color

The `background-color` property specifies the background color of an element.

/* Background Color */
.element {
    background-color: lightgray;
}
        

Result:

This is an element with a background color.

8. Text Color

The `color` property sets the color of the text.

/* Text Color */
.element {
    color: navy;
}
        

Result:

This text has a navy color.

9. Border Color

The `border-color` property sets the color of the border.

/* Border Color */
.element {
    border: 2px solid lime;
}
        

Result:

This element has a lime border.

10. Transparency

The `opacity` property allows you to set the transparency of an element.

/* Transparency */
.element {
    opacity: 0.5;
}
        

Result:

This element is 50% transparent.

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:

This box has a transparent background.

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:

This border matches the current text color.

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:

This uses named colors like "rebeccapurple".

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:

This has high contrast for better accessibility.

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:

This is an example of dark mode styling.

16. Opacity

The `opacity` property controls the transparency of an entire element, affecting both background and content.

/* Opacity Example */
.element {
    opacity: 0.7;
}
        

Result:

This box is semi-transparent.

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:

Blending modes create unique visual effects.

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:

This uses a CSS variable for its color.

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:

This is an example of advanced color functions.

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:

This is the parent text.
This child inherits the color from the parent.

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

Result:

Clipped circle with a background color.

Additional Resources