CSS Colors

Applying Colors in CSS

Colors are one of the most commonly used style properties in CSS. You can apply colors to a wide range of elements, including text, borders, and backgrounds. In CSS, you use the color property for text color and the background-color property for background color.

Text Color Example:

CSS

Background Color Example:

Tutorials dojo strip
CSS




CSS Color Formats

1. Color Keywords

CSS provides basic color keywords like red, blue, green, black, white, etc. This is the easiest method for applying basic colors.

CSS

2. HEX Color Codes

A HEX color code is a six-digit code representing a color, starting with a #. Each pair of digits represents the intensity of red, green, and blue (RGB), ranging from 00 to FF.

CSS
  • #3498db is broken down as:
    • 34 (red),
    • 98 (green),
    • db (blue).

3. RGB Color

The RGB format allows you to specify a color using its red, green, and blue components, with values ranging from 0 to 255.

CSS
  • This example represents Tomato Red with:
    • 255 for red,
    • 99 for green,
    • 71 for blue.

4. RGBA (RGB with Alpha)

RGBA is similar to RGB but with an added alpha (opacity) value. The alpha value ranges from 0 (completely transparent) to 1 (fully opaque).

CSS

5. HSL Color

HSL stands for Hue, Saturation, and Lightness. Hue is a degree on the color wheel (0-360), saturation is a percentage (0% is grayscale, 100% is full color), and lightness is also a percentage (0% is black, 100% is white).

CSS

6. HSLA (HSL with Alpha)

Similar to RGBA, HSLA adds an alpha value for transparency.

CSS




CSS Colors Using Different Color Formats

CSS




Choosing the Right Color Format

Keywords:

  • HEX: Commonly used in web design and supported by all browsers.
  • RGB/RGBA: Great for fine-tuning colors with more precision.
  • HSL/HSLA: Intuitive for adjusting hue, saturation, and lightness values.




CSS Labs

Tutorials dojo strip