HTML Colors

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.

Color Names

HTML supports 140 standard color names that can be used to apply colors directly to elements.

Color Name Example
Red
Try yourself
        
                <div style="color: red;">This text is red.</div>
        
    
Blue
Try yourself
        
                <div style="color: blue;">This text is blue.</div>
        
    
Green
Try yourself
        
                <div style="color: green;">This text is green.</div>
        
    

HEX Codes

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
        
                <div style="color: #FF0000;">This text is red using HEX code.</div>
        
    
#0000FF
Try yourself
        
                <div style="color: #0000FF;">This text is blue using HEX code.</div>
        
    
#008000
Try yourself
        
                <div style="color: #008000;">This text is green using HEX code.</div>
        
    

RGB Values

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
        
                <div style="color: rgb(255, 0, 0);">This text is red using RGB.</div>
        
    
rgb(0, 0, 255)
Try yourself
        
                <div style="color: rgb(0, 0, 255);">This text is blue using RGB.</div>
        
    
rgb(0, 128, 0)
Try yourself
        
                <div style="color: rgb(0, 128, 0);">This text is green using RGB.</div>
        
    

HSL Values

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
        
                <div style="color: hsl(0, 100%, 50%);">This text is red using HSL.</div>
        
    
hsl(240, 100%, 50%)
Try yourself
        
                <div style="color: hsl(240, 100%, 50%);">This text is blue using HSL.</div>
        
    
hsl(120, 100%, 25%)
Try yourself
        
                <div style="color: hsl(120, 100%, 25%);">This text is green using HSL.</div>
        
    

Opacity

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
        
                <div style="opacity: 1;">This text is fully opaque.</div>
        
    
opacity: 0.5
Try yourself
        
                <div style="opacity: 0.5;">This text is half transparent.</div>
        
    
opacity: 0
Try yourself
        
                <div style="opacity: 0;">This text is fully transparent.</div>
        
    

Gradient Backgrounds

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
        
                <div style="background: linear-gradient(to right, red, yellow); height: 100px;">This is a linear gradient background.</div>
        
    
Radial Gradient
Try yourself
        
                <div style="background: radial-gradient(circle, red, yellow); height: 100px;">This is a radial gradient background.</div>
        
    

Important Notes

Here are some important notes and best practices for using colors in HTML and CSS:


Summary

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.