CSS Colors
By Flavio Copes
Learn how to work with colors in CSS using the color, background-color and border-color properties, with values from named colors to RGB, HSL and hex codes.
By default an HTML page is rendered by web browsers quite sadly in terms of the colors used.
We have a white background, black color, and blue links. That’s it.
Luckily CSS gives us the ability to add colors to our designs.
We have these properties:
colorbackground-colorborder-color
All of them accept a color value, which can be in different forms.
Named colors
First, we have CSS keywords that define colors. CSS started with 16, but today there is a huge number of colors names:
aliceblueantiquewhiteaquaaquamarineazurebeigebisqueblackblanchedalmondbluebluevioletbrownburlywoodcadetbluechartreusechocolatecoralcornflowerbluecornsilkcrimsoncyandarkbluedarkcyandarkgoldenroddarkgraydarkgreendarkgreydarkkhakidarkmagentadarkolivegreendarkorangedarkorchiddarkreddarksalmondarkseagreendarkslatebluedarkslategraydarkslategreydarkturquoisedarkvioletdeeppinkdeepskybluedimgraydimgreydodgerbluefirebrickfloralwhiteforestgreenfuchsiagainsboroghostwhitegoldgoldenrodgraygreengreenyellowgreyhoneydewhotpinkindianredindigoivorykhakilavenderlavenderblushlawngreenlemonchiffonlightbluelightcorallightcyanlightgoldenrodyellowlightgraylightgreenlightgreylightpinklightsalmonlightseagreenlightskybluelightslategraylightslategreylightsteelbluelightyellowlimelimegreenlinenmagentamaroonmediumaquamarinemediumbluemediumorchidmediumpurplemediumseagreenmediumslatebluemediumspringgreenmediumturquoisemediumvioletredmidnightbluemintcreammistyrosemoccasinnavajowhitenavyoldlaceoliveolivedraborangeorangeredorchidpalegoldenrodpalegreenpaleturquoisepalevioletredpapayawhippeachpuffperupinkplumpowderbluepurplerebeccapurpleredrosybrownroyalbluesaddlebrownsalmonsandybrownseagreenseashellsiennasilverskyblueslateblueslategrayslategreysnowspringgreensteelbluetantealthistletomatoturquoisevioletwheatwhitewhitesmokeyellowyellowgreen
plus tranparent, and currentColor which points to the color property, for example useful to make the border-color inherit it.
They are defined in the CSS Color Module, Level 4. They are case insensitive.
Wikipedia has a nice table which lets you pick the perfect color by its name.
Named colors are not the only option.
RGB and RGBa
You can use the rgb() function to calculate a color from its RGB notation, which sets the color based on its red-green-blue parts. From 0 to 255:
p {
color: rgb(255, 255, 255); /* white */
background-color: rgb(0, 0, 0); /* black */
}
rgba() lets you add the alpha channel to enter a transparent part. That can be a number from 0 to 1:
p {
background-color: rgba(0, 0, 0, 0.5);
}
Hexadecimal notation
Another option is to express the RGB parts of the colors in the hexadecimal notation, which is composed by 3 blocks.
Black, which is rgb(0,0,0) is expressed as #000000 or #000 (we can shortcut the 2 numbers to 1 if they are equal).
White, rgb(255,255,255) can be expressed as #ffffff or #fff.
The hexadecimal notation lets express a number from 0 to 255 in just 2 digits, since they can go from 0 to “15” (f).
We can add the alpha channel by adding 1 or 2 more digits at the end, for example #00000033 or the short form #0003. Every current browser supports both the #RRGGBBAA and the #RGBA form.
HSL and HSLa
HSL = Hue Saturation Lightness. It sits alongside RGB as a standard way to write colors in CSS.
In this notation, black is hsl(0, 0%, 0%) and white is hsl(0, 0%, 100%).
If you are more familiar with HSL than RGB because of your past knowledge, you can definitely use that.
You also have hsla() which adds the alpha channel to the mix, again a number from 0 to 1: hsl(0, 0%, 0%, 0.5)
To convert a color between hex, RGB, HSL and OKLCH, try my free color converter. For newer CSS color syntax like oklch() and color-mix(), see modern CSS colors.
Want me to talk about your product? You can sponsor this site.