HTML colors

HTML colors are used to set the color of various elements on a webpage. Colors can be defined in several ways in HTML, including by name, hexadecimal values, RGB values, RGBA values (which include an alpha channel for opacity), HSL values, and HSLA values.

Here are the different ways to specify colors in HTML:

1. Color Names: There are 140 predefined color names in HTML.


<p style="color: red;">This is a red paragraph.</p>

2. Hexadecimal Values: Colors can be specified using a hex code, which starts with # followed by six hexadecimal digits.


<p style="color: #ff0000;">This is a red paragraph.</p>

3. RGB Values: Colors can be defined using the rgb() function, with values ranging from 0 to 255 for red, green, and blue.


<p style="color: rgb(255, 0, 0);">This is a red paragraph.</p>

4. RGBA Values: Similar to RGB, but includes an alpha channel for opacity, with values ranging from 0 (fully transparent) to 1 (fully opaque).


<p style="color: rgba(255, 0, 0, 0.5);">This is a semi-transparent red paragraph.</p>

5. HSL Values: Colors can be defined using the hsl() function, with values for hue (0-360 degrees), saturation (0%-100%), and lightness (0%-100%).


<p style="color: hsl(0, 100%, 50%);">This is a red paragraph.</p>

6. HSLA Values: Similar to HSL, but includes an alpha channel for opacity.


<p style="color: hsla(0, 100%, 50%, 0.5);">This is a semi-transparent red paragraph.</p>

By using these methods, you can apply colors to text, backgrounds, borders, and other elements on a webpage to enhance its visual appeal and usability.

HTML colors – Objective Questions (MCQs)

Q1. How do you specify a color in HTML using a name?






Q2. Which of the following is a valid way to specify colors in HTML?






Q3. Which attribute is used to set background color in HTML?






Q4. Which CSS property is used to set text color?






Q5. Which of the following is a valid hexadecimal color code?






Related HTML colors Topics