HTML colors, more accurately referred to as web colors or CSS colors in modern web development, are the methods used to specify and display colors on web pages. While originally defined within HTML specifications, the primary method for defining colors on the web now resides in the CSS Color Module.
These colors can be specified in several ways:
- Color Names: A set of predefined color names are recognized by browsers, such as
red,blue,green,black,white,purple,aqua, etc. There are over 140 such predefined names.
Code
<p style="color: blue;">This text is blue.</p>
- Hexadecimal (Hex) Values: This is a very common method, representing colors as a six-digit hexadecimal number prefixed with a
#. Each pair of digits represents the intensity of Red, Green, and Blue, respectively (e.g.,#RRGGBB). Values range from00(no intensity) toFF(full intensity). For example, pure red is#FF0000, and white is#FFFFFF.
Code
<div style="background-color: #FF0000;">This div has a red background.</div>
- RGB (Red, Green, Blue) Values: Colors are defined by specifying the intensity of red, green, and blue light components using a numerical value from 0 to 255 for each. The format is
rgb(red, green, blue).
Code
<span style="color: rgb(0, 128, 0);">This text is green.</span>
- RGBA (Red, Green, Blue, Alpha) Values: An extension of RGB, RGBA includes an “alpha” channel for specifying opacity. The alpha value ranges from 0 (completely transparent) to 1 (completely opaque).
Code
<p style="background-color: rgba(255, 0, 0, 0.5);">This background is semi-transparent red.</p>
- HSL (Hue, Saturation, Lightness) Values: This method describes colors based on their hue (a degree on the color wheel from 0 to 360), saturation (percentage of color intensity), and lightness (percentage of brightness).
Code
<div style="background-color: hsl(120, 100%, 50%);">This div has a vibrant green background.</div>
- HSLA (Hue, Saturation, Lightness, Alpha) Values: Similar to RGBA, HSLA adds an alpha channel for opacity to HSL values.
Code
<span style="color: hsla(240, 100%, 50%, 0.7);">This text is semi-transparent blue.</span>
These various methods provide flexibility in defining a vast spectrum of colors for web page elements like text, backgrounds, borders, and more.
