Colors in HTML are used to style elements and make web pages visually appealing. You can define colors using various methods such as color names, HEX codes, RGB values, and HSL values.
HTML supports 140 standard color names that can be used to apply colors directly to elements.
Color Name | Example |
---|---|
Red |
Try yourself
|
Blue |
Try yourself
|
Green |
Try yourself
|
HEX codes are used to define colors in HTML and CSS. A HEX code is a six-digit combination of numbers and letters, prefixed with a #.
HEX Code | Example |
---|---|
#FF0000 |
Try yourself
|
#0000FF |
Try yourself
|
#008000 |
Try yourself
|
RGB values define colors using the Red, Green, and Blue color model. Each value is specified as an integer between 0 and 255, or as a percentage.
RGB Value | Example |
---|---|
rgb(255, 0, 0) |
Try yourself
|
rgb(0, 0, 255) |
Try yourself
|
rgb(0, 128, 0) |
Try yourself
|
HSL stands for Hue, Saturation, and Lightness. It is an alternative way to define colors in HTML and CSS.
HSL Value | Example |
---|---|
hsl(0, 100%, 50%) |
Try yourself
|
hsl(240, 100%, 50%) |
Try yourself
|
hsl(120, 100%, 25%) |
Try yourself
|
Opacity is used to control the transparency of an element. It is specified using the opacity property in CSS.
Opacity Value | Example |
---|---|
opacity: 1 |
Try yourself
|
opacity: 0.5 |
Try yourself
|
opacity: 0 |
Try yourself
|
Gradients allow you to create smooth transitions between two or more colors. They can be linear or radial.
Gradient Type | Example |
---|---|
Linear Gradient |
Try yourself
|
Radial Gradient |
Try yourself
|
Here are some important notes and best practices for using colors in HTML and CSS:
HTML and CSS provide various ways to define and apply colors to elements. By understanding and using color names, HEX codes, RGB values, HSL values, opacity, and gradients, you can create visually appealing and accessible web pages.