Home >>CSS Tutorial >CSS Colors

CSS Colors

CSS Colors

Predefined color names are specified in using CSS, or RGB, HEX, HSL, RGBA, HSLA values.

CSS Color Names

A color can be specified by using a color name in CSS:

Let’s take an example of CSS colors:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:LightGray;">LightGray</h1>
<h1 style="background-color:Violet;">Violet</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
</body>
</html>
Output :

 

DodgerBlue

SlateBlue

Tomato

Orange

Gray

LightGray

Violet

MediumSeaGreen

 

CSS Background Color

In HTML elements you can also set the background color:

Let’s take an example of CSS Background color:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<h2 style="background-color:Green;">
Welcome to phptpoint tutorial 
</h2>
</body>
</html> 
Output :

 

Hello World

Welcome to phptpoint tutorial

CSS Text Color

You can set the color of text:

<!DOCTYPE html>
<html>
<body>
<h3 style="color:Tomato;">Hello World</h3>
<p style="color:DodgerBlue;"> An investment in knowledge pays the best interest.</p>
<p style="color:red;"> Education is the most powerful weapon.</p>
</body>
</html>
Output :

 

Hello World

An investment in knowledge pays the best interest.

Education is the most powerful weapon.

CSS Border Color

You can set the color of borders:

Let’s Take an example of CSS border Color:

<!DOCTYPE html>
<html>
<body>
<h1 style="border: 2px solid Tomato;">Hello World</h1>
<h1 style="border: 2px solid DodgerBlue;">Hello World</h1>
<h1 style="border: 2px solid red;">Hello World</h1>
</body>
</html>
Output :

 

Hello World

Hello World

Hello World

CSS Color Values

You can also specified colors using RGB values, HEX values, HSL values, RGBA values, and HSLA values:

Same as color name "Tomato".

Let’s take an example of CSS Color Values:

<!DOCTYPE html>
<html>
<body>
<p>Same as color name "Tomato":</p>
<h1 style="background-color:rgb(255, 99, 71);">rgb(255, 99, 71)</h1>
<h1 style="background-color:#ff6347;">#ff6347</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">hsl(9, 100%, 64%)</h1>
<p>Same as color name "Tomato", but 50% transparent:</p>
<h1 style="background-color:rgba(255, 99, 71, 0.5);">rgba(255, 99, 71, 0.5)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">hsla(9, 100%, 64%, 0.5)</h1>
</body>
</html>
Output :

 

Same as color name "Tomato":

rgb(255, 99, 71)

#ff6347

hsl(9, 100%, 64%)

Same as color name "Tomato", but 50% transparent:

rgba(255, 99, 71, 0.5)

hsla(9, 100%, 64%, 0.5)

CSS RGB Value

You can also specified colors as an RGB value, using this formula:

rgb(red, green, blue)

The intensity of the color between 0 and 255 is defined by each parameter (red, green, and blue).

For example, rgb (255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0.

All color parameters must be set to 0, to display the color black like this: rgb(0, 0, 0).

All color parameters must be set to 255 to display the color white, like this: rgb (255, 255, 255).

Let’s take an example of CSS RGB Value:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:rgb(255, 0, 0);">rgb(255, 0, 0)</h1>
<h1 style="background-color:rgb(0, 0, 255);">rgb(0, 0, 255)</h1>
<h1 style="background-color:rgb(60, 179, 113);">rgb(60, 179, 113)</h1>
<h1 style="background-color:rgb(238, 130, 238);">rgb(238, 130, 238)</h1>
<h1 style="background-color:rgb(255, 165, 0);">rgb(255, 165, 0)</h1>
<h1 style="background-color:rgb(106, 90, 205);">rgb(106, 90, 205)</h1>
</body>
</html>
Output :

 

rgb(255, 0, 0)

rgb(0, 0, 255)

rgb(60, 179, 113)

rgb(238, 130, 238)

rgb(255, 165, 0)

rgb(106, 90, 205)

CSS HEX Value

You can also be specified color using a hexadecimal value :

#rrggbb

The hexadecimal values between 00 and ff (same as decimal 0-255), Where rr (red), gg (green) and bb (blue).

For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others are set to the lowest value (00).

Let’s take an example CSS Hex Value:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:#ff0000;">#ff0000</h1>
<h1 style="background-color:#0000ff;">#0000ff</h1>
<h1 style="background-color:#3cb371;">#3cb371</h1>
<h1 style="background-color:#ee82ee;">#ee82ee</h1>
<h1 style="background-color:#ffa500;">#ffa500</h1>
<h1 style="background-color:#6a5acd;">#6a5acd</h1>
</body>
</html>
Output :

 

#ff0000

#0000ff

#3cb371

#ee82ee

#ffa500

#6a5acd

Let’s Take another example :

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:#000000;">#000000</h1>
<h1 style="background-color:#3c3c3c;">#3c3c3c</h1>
<h1 style="background-color:#787878;">#787878</h1>
<h1 style="background-color:#b4b4b4;">#b4b4b4</h1>
<h1 style="background-color:#f0f0f0;">#f0f0f0</h1>
<h1 style="background-color:#ffffff;">#ffffff</h1>
</body>
</html>

HSL Value

A color can also be specified using hue, saturation, and lightness (HSL) in the form:

hsl(hue, saturation, lightness)

Hue is a degree from 0 to 360 on the color wheel. you can specified the color where Red-0, green-120, and blue-240.

Saturation is always in percentage value, 0% means a shade of gray, and 100% is the full color.

Lightness is also always in a percentage, 0% is black, 50% is neither light or dark, 100% is white.

Let’s take an Example of HSL Value:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h1>
<h1 style="background-color:hsl(240, 100%, 50%);">hsl(240, 100%, 50%)</h1>
<h1 style="background-color:hsl(147, 50%, 47%);">hsl(147, 50%, 47%)</h1>
<h1 style="background-color:hsl(300, 76%, 72%);">hsl(300, 76%, 72%)</h1>
<h1 style="background-color:hsl(39, 100%, 50%);">hsl(39, 100%, 50%)</h1>
<h1 style="background-color:hsl(248, 53%, 58%);">hsl(248, 53%, 58%)</h1>
</body>
</html>
Output :

 

hsl(0, 100%, 50%)

hsl(240, 100%, 50%)

hsl(147, 50%, 47%)

hsl(300, 76%, 72%)

hsl(39, 100%, 50%)

hsl(248, 53%, 58%)

Saturation

Intensity of a color can be described as a Saturation.

No shades of gray, 100% is pure color.

You can still see the color in 50% is 50% gray.

You can no longer see the color, 0% is completely gray.

Let’s take an Example of Saturation:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h1>
<h1 style="background-color:hsl(0, 80%, 50%);">hsl(0, 80%, 50%)</h1>
<h1 style="background-color:hsl(0, 60%, 50%);">hsl(0, 60%, 50%)</h1>
<h1 style="background-color:hsl(0, 40%, 50%);">hsl(0, 40%, 50%)</h1>
<h1 style="background-color:hsl(0, 20%, 50%);">hsl(0, 20%, 50%)</h1>
<h1 style="background-color:hsl(0, 0%, 50%);">hsl(0, 0%, 50%)</h1>
</body>
</html>
Output :

 

hsl(0, 100%, 50%)

hsl(0, 80%, 50%)

hsl(0, 60%, 50%)

hsl(0, 40%, 50%)

hsl(0, 20%, 50%)

hsl(0, 0%, 50%)

Lightness

It can be described as how much light of a color you want to give, where 0% means no light (black), 50% means 50% light (neither dark nor light) 100% means full lightness (white).

Let’s Take an example of Lightness:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:hsl(0, 100%, 0%);">hsl(0, 100%, 0%)</h1>
<h1 style="background-color:hsl(0, 100%, 25%);">hsl(0, 100%, 25%)</h1>
<h1 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h1>
<h1 style="background-color:hsl(0, 100%, 75%);">hsl(0, 100%, 75%)</h1>
<h1 style="background-color:hsl(0, 100%, 90%);">hsl(0, 100%, 90%)</h1>
<h1 style="background-color:hsl(0, 100%, 100%);">hsl(0, 100%, 100%)</h1>
</body>
</html>
Output :

 

hsl(0, 100%, 0%)

hsl(0, 100%, 25%)

hsl(0, 100%, 50%)

hsl(0, 100%, 75%)

hsl(0, 100%, 90%)

hsl(0, 100%, 100%)

RGBA Value

It is an extension of RGB color values with an alpha channel .

It specifies the opacity for a color.

An RGBA color value is specified with:

rgba(red, green, blue, alpha)

A number between 0.0 (fully transparent) and 1.0 (not transparent at all) is a alpha parameter :

Let's take an example of RGBA Value:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:rgba(255, 99, 71, 0);">rgba(255, 99, 71, 0)</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.2);">rgba(255, 99, 71, 0.2)</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.4);">rgba(255, 99, 71, 0.4)</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.6);">rgba(255, 99, 71, 0.6)</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.8);">rgba(255, 99, 71, 0.8)</h1>
<h1 style="background-color:rgba(255, 99, 71, 1);">rgba(255, 99, 71, 1)</h1>
</body>
</html>
Output :

 

rgba(255, 99, 71, 0)

rgba(255, 99, 71, 0.2)

rgba(255, 99, 71, 0.4)

rgba(255, 99, 71, 0.6)

rgba(255, 99, 71, 0.8)

rgba(255, 99, 71, 1)

HSLA Value

It is also an extension of HSL color values with an alpha channel.

It also specifies the opacity for a color.

An HSLA color value is specified with:

hsla(hue, saturation, lightness, alpha)

A number between 0.0 (fully transparent) and 1.0 (not transparent at all) is a alpha parameter:

Let’s an example of HSLA Value:

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:hsla(9, 100%, 64%, 0);">hsla(9, 100%, 64%, 0)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.2);">hsla(9, 100%, 64%, 0.2)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.4);">hsla(9, 100%, 64%, 0.4)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.6);">hsla(9, 100%, 64%, 0.6)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.8);">hsla(9, 100%, 64%, 0.8)</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 1);">hsla(9, 100%, 64%, 1)</h1>
</body>
</html>
Output :

 

hsla(9, 100%, 64%, 0)

hsla(9, 100%, 64%, 0.2)

hsla(9, 100%, 64%, 0.4)

hsla(9, 100%, 64%, 0.6)

hsla(9, 100%, 64%, 0.8)

hsla(9, 100%, 64%, 1)


No Sidebar ads