CSS - Colors



CSS Colors can be specified using predefined name of colors, RGB, RGBA, HSL, HSLA and Hexadecimal values. CSS allow us to change background color, text color, border color and caret color of any elements in HTML documents.

Colors are very important aspects of web design, as they not only enhance the visual appeal but also influence user behavior.

Background Color

Text Color

Change Background Color and Text Color

Table of Contents


 

Following are all the value we can set to color properties in css.

  • Color name: CSS has a set of predefined color names that you can use directly. For example, red, blue, green, grey, lightblue, aquamarine. There are lot more.
  • background-color: grey;
    
  • Hexadecimal code: It starts with a hash (#) followed by six hexadecimal digits that represent RGB value of color.
  • background-color: #FF0000; /* Red Color */
    
  • Short Hexadecimal code: Shorter version of hexadecimal format where each of the RGB components is represented by a single digit, and the value is duplicated.
  • background-color: #F00; /* Red Color */
    
  • RGB Function: The rgb() function defines color by taking three parameters, red, green, and blue values. Parameter values can vary from 0 to 255.
  • background-color: rgb(0, 0, 255); /* Blue Color */
    
  • RGBA Function: The rgb() function defines color by taking four parameters, red, green, blue and alpha values. ALpha value describes intensity of color with a decimal value between 0 and 1.
  • /* Half Intense Blue Color */
    background-color: rgba(0, 0, 255, 0.5); 
    
  • HSL Function: The hsl() function accepts three parameters for colors, Hue (0 to 360 degree), Saturation (%), and Lightness (%)
  • /* Green Color */
    background-color: hsl(120, 100%, 50%); 
    
  • HSLA Function: The hsla() function accepts four parameters for colors, Hue (0 to 360 degree), Saturation (%), Lightness (%) and alpha( 0 to 1 decimal number)
  • /* Half Intense Green Color */
    background-color: hsla(120, 100%, 50%, 0.5); 
    

CSS Background Color

In CSS, we can set background color for elements like div, span, body, paragraph etc using background-color property.

Example

In this example we set different background colors for body, div, span elements in HTML.

 <!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body{
            background-color: lightgrey;
            padding: 10px;
        }
        div{
            background-color: cyan;
            padding: 10px;
        }
        span{
            background-color: yellow;
            padding: 10px;
        }
        p{
            background-color: white;
            padding: 10px;
        }
    </style>
</head>

<body>
   <div>
      This is a Div 
   </div> <br>
   <span>
      This ia a span
   </span>
   <p>
      This is paragraph
   </p>
</body>
</html>

CSS Text Color

In CSS, we can use color property to set color for texts inside any type of element

Example

In this example we set different text colors for p, div, span elements in HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
      div{
         background-color: cyan;
         color: red;
         padding: 10px;
      }
      span{
         background-color: yellow;
         color: green;
         padding: 10px;
      }
      p{
         background-color: black;
         color: white;
         padding: 10px;
      } 
    </style>
</head>

<body>
   <div>
      This is a Div 
   </div> <br>
   <span>
      This ia a span
   </span>
   <p>
      This is paragraph
   </p>
</body>
</html>

CSS Border Color

In CSS, we can use the border-color property to set the color of the border for any element.

Example

In this example, we set different border colors for p, div, and span elements in HTML.

<!DOCTYPE html>
<html lang="en">
<head>
<style>
  div {
     border: 2px solid;
     border-color: red;
     padding: 10px;
     margin: 10px 0;
  }
  span {
     border: 2px solid;
     border-color: green;
     padding: 10px;
     margin: 10px 0;
  }
  p {
     border: 2px solid;
     border-color: blue;
     padding: 10px;
     margin: 10px 0;
  } 
</style>
</head>

<body>
   <div>
      This is a Div 
   </div> <br>
   <span>
      This ia a span
   </span>
   <p>
      This is paragraph
   </p>
</body>
</html>

CSS Caret Color

The caret-color property in CSS specifies the color of the text cursor (caret) inside an input or textarea element. This can be customized to enhance the user interface.

Example

In this example, we set cursor color for input and textarea elements.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      input, textarea {
         width: 100%;
         padding: 5px;
         margin-top: 10px;
         box-sizing: border-box;
         caret-color: darkred;
      }
   </style>
</head>

<body>
   <input type="text" 
         placeholder="Click here...">
   <textarea>
      Type Something
   </textarea>
</body>
</html>

CSS Color Keywords

There are a few keywords that are reserved to serve particular purposes, all the reserved color keywords are: inherit, transparent and currentColor. All of these keywords purpose and usage are described below.

CSS Colors Inherit Keyword

The inherit keyword in CSS is used to make an element inherit the computed value of a property from its parent element. This can be useful for maintaining consistency in styling.

Example

In this example, the parent element has a text color of dark red, and the child element inherits this color using the inherit keyword.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
   .parent {
      color: darkred;
      padding: 10px;
      border: 1px solid #ccc;
   }
   .child {
      color: inherit;
      padding: 10px;
      border: 1px solid #ccc;
      background-color: lightgray;
   } 
   </style>
</head>

<body>
<div class="parent">
      This is the parent element with color 
      set to dark red.
      <div class="child">
         This is the child element inheriting 
         the color from the parent.
      </div>
</div>
</body>
</html>

CSS Colors Transparent Keyword

Transparent keyword for a color value used represent a fully transparent color. ( You can also set opacity zero to achieve fully transparent color ). Checkout following example

Example

Following code shows how to make color of child element to transparent.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      .parent {
         background-color: grey;
         padding: 10px;
      }
      .child {
         /* Sets the background to fully transparent */
         background-color: transparent;
         border: solid;
         padding: 10px;
      }
   </style>
</head>

<body>
      <div class="parent">
         Parent Element
         <div class="child">
            Child Element
         </div>
      </div>
</body>
</html>

CSS Colors currentColor Keyword

The currentColor is a special CSS keyword that represents the current value of the color property for an element.

Example

In this code we use currentColor property for border of child, This will tell browser to use text color of parent for border color of child.

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
      .parent {
      /* Sets the text color to blue */
         color: blue; 
         border: 2px solid black;
         padding: 20px;
      }
      .child {
      /* Inherits text color from parent for border */
         border: 2px solid currentColor; 
         padding: 10px;
      }
    </style>
</head>

<body>
    <div class="parent">
        Parent Element
        <div class="child">
            Child Element
        </div>
    </div>
</body>
</html>

CSS Building Color Codes

You can build millions of color codes using our Color Code Builder. Check the CSS Color Picker.

CSS Browser Safe Colors

Here is the list of 216 colors which are supposed to be most safe and computer independent colors. These colors vary from hexa code 000000 to FFFFFF. These colors are safe to use because they ensure that all computers would display the colors correctly when running a 256 color palette.

000000 000033 000066 000099 0000CC 0000FF
003300 003333 003366 003399 0033CC 0033FF
006600 006633 006666 006699 0066CC 0066FF
009900 009933 009966 009999 0099CC 0099FF
00CC00 00CC33 00CC66 00CC99 00CCCC 00CCFF
00FF00 00FF33 00FF66 00FF99 00FFCC 00FFFF
330000 330033 330066 330099 3300CC 3300FF
333300 333333 333366 333399 3333CC 3333FF
336600 336633 336666 336699 3366CC 3366FF
339900 339933 339966 339999 3399CC 3399FF
33CC00 33CC33 33CC66 33CC99 33CCCC 33CCFF
33FF00 33FF33 33FF66 33FF99 33FFCC 33FFFF
660000 660033 660066 660099 6600CC 6600FF
663300 663333 663366 663399 6633CC 6633FF
666600 666633 666666 666699 6666CC 6666FF
669900 669933 669966 669999 6699CC 6699FF
66CC00 66CC33 66CC66 66CC99 66CCCC 66CCFF
66FF00 66FF33 66FF66 66FF99 66FFCC 66FFFF
990000 990033 990066 990099 9900CC 9900FF
993300 993333 993366 993399 9933CC 9933FF
996600 996633 996666 996699 9966CC 9966FF
999900 999933 999966 999999 9999CC 9999FF
99CC00 99CC33 99CC66 99CC99 99CCCC 99CCFF
99FF00 99FF33 99FF66 99FF99 99FFCC 99FFFF
CC0000 CC0033 CC0066 CC0099 CC00CC CC00FF
CC3300 CC3333 CC3366 CC3399 CC33CC CC33FF
CC6600 CC6633 CC6666 CC6699 CC66CC CC66FF
CC9900 CC9933 CC9966 CC9999 CC99CC CC99FF
CCCC00 CCCC33 CCCC66 CCCC99 CCCCCC CCCCFF
CCFF00 CCFF33 CCFF66 CCFF99 CCFFCC CCFFFF
FF0000 FF0033 FF0066 FF0099 FF00CC FF00FF
FF3300 FF3333 FF3366 FF3399 FF33CC FF33FF
FF6600 FF6633 FF6666 FF6699 FF66CC FF66FF
FF9900 FF9933 FF9966 FF9999 FF99CC FF99FF
FFCC00 FFCC33 FFCC66 FFCC99 FFCCCC FFCCFF
FFFF00 FFFF33 FFFF66 FFFF99 FFFFCC FFFFFF
Advertisements