CSS Color Functions
If you asked me a few months ago, “What does it take for a website to stand out?” I may have said fancy animations, creative layouts, cool interactions, and maybe just the general aesthetics, without pointing out something in particular. If you ask me now, after working on color for the better part of the year, I can confidently say it’s all color. Among all the aspects that make a design, a good color system will make it as beautiful as possible. However, color in CSS can be a bit hard to fully understand since there are many ways to set the same color, and sometimes they even look the same, but underneath are completely different technologies. That’s why, in this guide, we will walk through all the ways you can set up colors in CSS and all the color-related properties out there! They are in your phone, in what your eye sees, and on any screen you look at; they essentially capture everything. Design-wise, I see the amazing use of colors on sites listed over at awwwards.com, and I’m always in awe. Not all color is the same. In fact, similar colors can live in different worlds, known as color spaces. Take for example, sRGB, the color space used on the web for the better part of its existence and hence the most known. While it’s the most used, there are many colors that are simply missing in sRGB that new color spaces like CIELAB and Oklab bring, and they cover a wider range of colors sRGB could only dream of, but don’t let me get ahead of myself. A color space is the way we arrange and represent colors that exist within a device, like printers and monitors. We have different types of color spaces that exist in media (Rec2020, Adobe RGB, etc), but not all of them are covered in CSS. Luckily, the ones we have are sufficient to produce all the awesome and beautiful colors we need. In this guide, we will be diving into the three main color spaces available in CSS: sRGB, CIELAB, and OkLab. The sRGB is one of the first color spaces we learn. Inside, there are three color functions, which are essentially notations to define a color: sRGB has been a standard color space for the web since 1996. However, it’s closer to how old computers represented color, rather than how humans understand it, so it had some problems like not being able to capture the full gamut of modern screens. Still, many modern applications and websites use sRGB, so even though it is the “old way” of doing things, it is still widely accepted and used today. All three values are non-negative, and they go from It also has an optional value (the alpha value) preceded by a forward slash. It determines the level of opacity for the color, which goes from There are two ways you can write inside You want to combine the two syntax formats, yes? That’s a no-no. It won’t even work. But, following oneconsistent format will do the trick, so do that instead. Either you’re so used to the old syntax and it’s hard for you to move on, continue to use the legacy syntax, or you’re one who’s willing to try and stick to something new, use the modern syntax. In terms of syntax, the So, what’s the difference between Breaking news! There is no difference.Initially, only In a nutshell, The hexadecimal CSS color code is a 3, 4, 6, or 8 (being the maximum) digit code for colors in sRGB. It’s basically a shorter way of writing In the hexadecimal color system, the 6-digit style is done in pairs. Each pair represents red ( Each value in the pair can go from Notice how I used caps for the letters ( In reality, each value in the 3-digit system is duplicated and then translated to a visible color BUT, this severely limits the colors you can set. What if I want to target the color For the alpha value, Both One cool thing: the hue angle goes from ( Plus, you can easily get a complementary color from the opposite angle (i.e., adding You want to combine the two syntax formats like in Instead, stick to one of the syntaxes, like in So, what’s the difference between Breaking news (again)! They’re the same. So, why does My advice: just use Although this color function is barely used, it’s completely valid to use, so it’s up to personal preference. CSS named colors are hardcoded keywords representing predefined colors in sRGB. You are probably used to the basic: Named colors are often discouraged because their names do not always match what color you would expect. The CIELAB color space is a relatively new color space on the web that represents a wider color gamut, closer to what the human eye can see, so it holds a lot more color than the sRGB space. For this color function, we have three axes in a space-separated list to determine how the color is set. This is useful when you’re trying to obtain new colors and provide support for screens that do support them. Actually, most screens and all major browsers now support The CSS The CSS They both use the same color space, but instead of having The CSS Björn Ottosson created this color space as an “OK” and even better version of the lab color space. It was created to solve the limitations of CIELAB and CIELAB color space like image processing in Perceptual uniformity occurs when there’s a smooth change in the direction of a gradient color from one point to another. If you notice stark contrasts like the example below for Notice how the change from one color to another is the same in OKlab actually provides a better saturation of colors while still maintaining the hue and lightness present in colors in CIELAB (and even a smoother transition between colors!). The Again, this solves one of the issues in lab which is perceptual uniformity so if you’re looking to use a better alternative to lab, use The CSS The The CSS The To use this function, you must simply be aware of these 6 parameters: The You’re basically mixing two colors of any type in a color space. Do take note, the accepted color spaces here are different from the color spaces accepted in the To use this function, you must be aware of these three values: Here’s how it works. We have: Let’s take an example, say, converting a color from All the values above will be translated to the corresponding colors in Although, the You can even be wacky and use math functions: All CSS color functions support the relative color syntax. The relative color syntax, simply put, is a way to access other colors in another color function or value, then translating it to the values of the current color function. It goes “from The relative color syntax is, however, different than the Remember, the CSS is totally capable of transitioning from one color to another. See the “CSS Gradients Guide” for a full run-down, including of the different types of gradients with examples. There are a lot of properties that support the use of color. Just so you know, this list does not contain deprecated properties. This CSS property sets the accent color for UI controls like checkboxes and radio buttons, and any other form element Accent colors are a way to style unique elements in respect to the chosen color scheme. Applies solid colors as background on an element. Shorthand for setting the color of all four borders. Adds shadows to element for creating the illusion of depth. The property accepts a number of arguments, one of which sets the shadow color. Specifies the color of the text input cursor (caret). Sets the foreground color of text and text decorations. Sets the color of a line between columns in a multi-column layout. This property can’t act alone, so you need to set the Sets the color of the SVG shape Specifies the flood color to use for Specifies the color of the lighting source to use for Sets the color of an element’s outline. Specifies the color of gradient stops for the Defines the color of the outline of Sets the color of text decoration lines like underlines. Specifies the color of emphasis marks on text. Applies shadow effects to text, including color.Colors are in everything
What’s a color space?
The sRGB Color Space
rgb(), hsl(), and hwb().The
rgb()function
rgb() uses three values, r, g, and b which specifies the redness, greenness, and blueness of the color you want.0to 255..element { color: rgb(245 123 151);}0(or 0%) for a completely transparent color, to 1(or 100%) for a fully opaque one..element { color: rgb(245 123 151 / 20%);}rgb(). Either using the legacy syntax that separates the three values with commas or the modern syntax that separates each with spaces./* This would not work */.element { color: rgb(225, 245, 200 / 0.5);}/* Neither will this */.element { color: rgb(225 245 200, 0.5);}/* Or this */.element { color: rgb(225, 245 200 / 0.5);}/* Valid (Modern syntax) */.element { color: rgb(245 245 255 / 0.5);}/* Valid (Legacy syntax) */.element { color: rgb(245, 245, 255, 0.5);}The
rgba()functionrgba() is essentially the same as rgb() with an extra alpha value used for transparency.rgba() function can be written in two ways:/).element { color: rgba(100, 50, 0, 0.5);}.element { color: rgba(100 50 0 / 0.5);}rgba() and rgb()?rgba() could set the alpha value for opacity, but in recent years, rgb()now supports transparency using the forward slash (/) before the alpha value.rgb() also supports legacy syntax (commas) and modern syntax (spaces), so there’s practically no reason to use rgba() anymore; it’s even noted as a CSS mistake by folks at W3C.rgb() and rgba() are the same, so just use rgb()./* This works */.element-1 { color: rgba(250 30 45 / 0.8);}/* And this works too, so why not just use this? */.element-2 { color: rgb(250 30 45 / 0.8);}The hexadecimal notation

rgb(). The hexadecimal color (or hex color) begins with a hash token (#) and then a hexadecimal number, which means it goes from 0 to 9 and then skips to letters a to f (a being 10, b being 11, and so on, up to f for 15).RR), blue (BB), and green (GG).00 to FF, which it’s equivalent to 255 in rgb().F) and not lowercase letters like I did previously? Well, that’s because hexadecimals are not case-sensitive in CSS, so you don’t have to worry about uppercase or lowercase letters when dealing with hexadecimal colors..element { color: #abc;}.element { color: #abc; /* Equals #AABBCC */}213 in the red space, or how would I get a blue of value 103? It’s impossible. That’s why you can only get a total number of 4,096 colors here as opposed to the 17 million in the 6-digit notation. Still, if you want a fast way of getting a certain color in hexadecimal without having to worry about the millions of other colors, use the 3-digit notation..element { color: #abcd; /* Same as #AABBCCDD */}0represents 00(a fully transparent color) and Frepresents FF(a fully opaque color)..element { color: #abcdef;}.element { color: #faded101;}The
hsl()function
hsl() and rgb() live in the sRGB space, but they access colors differently. And while the consensus is that hsl() is far more intuitive than rgb(), it all boils down to your preference.hsl() takes three values: h, s, and l, which set its hue, saturation, and lightness, respectively.0deg to 360deg.0(or 0%) to 100(or 100%).0deg–360deg), but we might as well use negative angles or angles above 360deg, and they will circle back to the right hue. Especially useful for infinite color animation. Pretty neat, right?180degto the current hue) on the color wheel./* Current color */.element { color: hsl(120deg 40 60 / 0.8);}/* Complementary color */.element { color: hsl(300deg 40 60 / 0.8);}rgb(), yes? That’s also a no-no. It won’t work./* This would not work */.element { color: hsl(130deg, 50, 20 / 0.5);}/* Neither will this */.element { color: hsl(130deg 50 20, 0.5);}/* Or this */.element { color: hsl(130deg 50, 20 / 0.5);}rgb():/* Valid (Modern syntax) */ .element { color: hsl(130deg 50 20 / 0.5);}/* Valid (Modern syntax) */ .element { color: hsl(130deg, 50, 20, 0.5);}The
hsla()functionhsla() is essentially the same with hsl(). It uses three values to represent its color’s hue (h), saturation (s), and lightness (l), and yes (again), an alpha value for transparency (a). We can write hsla() in two different ways:/).element { color: hsla(120deg, 100%, 50%, 0.5);}.element { color: hsla(120deg 100% 50% / 0.5);}hsla() and hsl()?hsl() and hsla() both:hsla() still exist? Well, apart from being one of the mistakes of CSS, many applications on the web still use hsla()since there wasn’t a way to set opacity with hsl() when it was first conceived.hsl(). It’s the same as hsla() but less to write./* This works */.element-1 { color: hsla(120deg 80 90 / 0.8);}/* And this works too, so why not just use this? */.element-2 { color: hsl(120deg 80 90 / 0.8);}The
hwb()function
hwb() also uses hue for its first value, but instead takes two values for whiteness and blackness to determine how your colors will come out (and yes, it also does have an optional transparency value, a, just like rgb()and hsl())..element { color: hwb(80deg 20 50 / 0.5);}h is the same as the hue angle in hsl(), which represents the color position in the color wheel from 0(or 0deg) to 360(or 360deg).w, represents the whiteness in the color. It ranges from 0/0% (no white) to 100/100% (full white if b is 0).b, represents the blackness in the color. It ranges from 0/0% (no black) to 100/100% (fully black if w is 0).a, for the color’s opacity, preceded by a forward slash The value’s range is from 0.0(or 0%) to 1.0(or 100%).Named colors
white, blue, black, red, but there are a lot more, totaling 147 in all, that are defined in the Color Modules Level 4 specification.The CIELAB Color Space
The
lab()function
.element { color: lab(50 20 20 / 0.9);}l represents the degree of whiteness to blackness of the color. Its range being 0/(or 0%) (black) to 100(or 100%) (white).a represents the degree of greenness to redness of the color. Its range being from -125/0% (green) to125(or 100%) (red).b represents the degree of blueness to yellowness of the color. Its range is also from -125(or 0%) (blue) to 125(or 100%) (red).0.0(or 0%) to 1.0(or 100%).lab(), so you should be good.lab() color function’s a and b values are actually unbounded. Meaning they don’t technically have an upper or lower limit. But, at practice, those are their limits according to the spec.The
lch()function
lch()color function is said to be better and more intuitive than lab(). .element { color: lch(10 30 300deg);}l, a, and b, lchuses lightness, chroma, and hue.lrepresents the degree of whiteness to blackness of the color. Its range being 0(or 0%) (black) to 100(or 100%) (white).crepresents the color’s chroma (which is like saturation). Its range being from 0(or 100%) to 150or (or 100%).hrepresents the color hue. The value’s range is also from 0(or 0deg) to 360(or 360deg).0.0(or 0%) to 1.0(or 100%).lch()color function’s chroma (c) value is actually unbounded. Meaning it doesn’t technically have an upper or lower limit. But, in practice, the chroma values above are the limits according to the spec.The OkLab Color Space
lab(), such as making an image grayscale, and perceptual uniformity. The two color functions in CSS that correspond to this color space are oklab() and oklch().rgb() when transitioning from one hue to another, that is referred to as a non-uniform perceptual colormap.oklab() without any stark contrasts as opposed to rgb()? Yeah, OKLab color space solves the stark contrasts present and gives you access to many more colors not present in sRGB.The
oklab()functionoklab() color function, just like lab(), generates colors according to their lightness, red/green axis, blue/yellow axis, and an alpha value for color opacity. Also, the values for oklab() are different from that of lab() so please watch out for that..element { color: oklab(30% 20% 10% / 0.9);}l represents the degree of whiteness to blackness of the color. Its range being 0(or 0%) (black) to 0.1(or 100%) (white).a represents the degree of greenness to redness of the color. Its range being from -0.4(or -100%) (green) to 0.4(or 100%) (red).b represents the degree of blueness to yellowness of the color. The value’s range is also from -0.4(or 0%) (blue) to 0.4(or -100%) (red).0.0(or 0%) to 1.0(or 100%).oklab().oklab() color function’s a and b values are actually unbounded. Meaning they don’t technically have an upper or lower limit. But, theoretically, those are the limits for the values according to the spec.The
oklch()function
oklch() color function, just like lch(), generates colors according to their lightness, chroma, hue, and an alpha value for color opacity. The main difference here is that it solves the issues present in lab()and lch()..element { color: oklch(40% 20% 100deg / 0.7);}l represents the degree of whiteness to blackness of the color. Its range being 0.0(or 0%) (black) to 1.0(or 100%) (white).c represents the color’s chroma. Its range being from 0(or 0%) to 0.4(or 100%) (it theoretically doesn’t exceed 0.5).h represents the color hue. The value’s range is also from 0(or 0deg) to 360(or 360deg).0.0(or 0%) to 1.0(or 100%).oklch() color function’s chroma (c) value is actually unbounded. Meaning it doesn’t technically have an upper or lower limit. But, theoretically, the chroma values above are the limits according to the spec.The
color()function
color() function allows access to colors in nine different color spaces, as opposed to the previous color functions mentioned, which only allow access to one.color space you want to access colors from. They can either be srgb, srgb-linear, display-p3, a98-rgb, prophoto-rgb, rec2020, xyz, xyz-d50, or xyz-d65c1, c2, and c3) specifies the coordinates in the color space for the color ranging from 0.0 – 1.0.0.0(or 0%) to 1.0(or 100%).The
color-mix()function
color-mix() function mixes two colors of any type in a given color space. Basically, you can create an endless number of colors with this method and explore more options than you normally would with any other color function. A pretty powerful CSS function, I would say..element { color-mix(in oklab, hsl(40 20 60) 80%, red 20%);}color()function.in colorspace specifies the interpolation method used to mix the colors, and these can be any of these 15 color spaces: srgb, srgb-linear, display-p3, a98-rgb, prophoto-rgb, rec2020, lab, oklab, xyz, xyz-d50, xyz-d65, hsl, hwb, lch, and oklch.0% to 100%.The Relative Color Syntax

.element{ color-function(from origin-color c1 c2 c3 / alpha)}from is a mandatory keyword you must set to extract the color values from origin-color.origin-color, represents a color function or value or even another relative color that you want to get color from.0.0(or 0%) to 1.0(or 100%) which either set from the origin-color or set manually,rgb()to lab():.element { color: lab(from rgb(255 210 01 / 0.5) l a b / a);}rgb(). Now, let’s take a look at another example where we convert a color from rgb()to oklch():.element { color: oklch(from rgb(255 210 01 / 0.5) 50% 20% h / a);}l and c values were changed, the h and a would be taken from the original color, which in this case is a light yellowish color in rgb().<color>” to another..element { color: oklch(from rgb(255 210 01 / 0.5) calc(50% + var(--a)) calc(20% + var(--b)) h / a);}color() function in that you have to include the color space name and then fully write out the channels, like this:.element { color: color(from origin-color colorspace c1 c2 c3 / alpha);}color-mix() function is not a part of this. You can have relative color functions inside the color functions you want to mix, yes, but the relative color syntax is not available in color-mix() directly.Color gradients

Properties that support color values
accent-colorprogress { accent-color: lightgreen;}background-color.element { background-color: #ff7a18;}border-color/* Sets all border colors */.element { border-color: lch(50 50 20);}/* Sets top, right, bottom, left border colors */.element { border-color: black green red blue;}box-shadow.element { box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);}caret-color.element { caret-color: lch(30 40 40);}color.element { color: lch(80 10 20);}column-rule-colorcolumns and column-rule-style property first before using this..element { column: 3; column-rule-style: solid; column-rule-color: lch(20 40 40); /* highlight */}fill.element { fill: lch(40 20 10);}flood-color<feFlood> and <feDropShadow> elements inside the <filter> element for <svg>. This should not be confused with the flood-color CSS attribute, as this is a CSS property and that’s an HTML attribute (even though they basically do the same thing). If this property is specified, it overrides the CSS flood-color attribute.element { flood-color: lch(20 40 40);}lighting-color<feDiffuseLighting> and <feSpecularLighting> elements inside the <filter> element for <svg>. .element { lighting-color: lch(40 10 20);}outline-color.element { outline-color: lch(20 40 40);}stop-color<stop> tags for <svg>..element { stop-color: lch(20 40 40);}stroke<svg>..element { stroke: lch(20 40 40);}text-decoration-color.element { text-decoration-color: lch(20 40 40);}text-emphasis-color.element { text-emphasis-color: lch(70 20 40);}text-shadow.element { text-shadow: 1px 1px 1px lch(50 10 30);}Almanac references
Color functions
color rgb()
.element { color: rgb(0 0 0 / 0.5); }
Sunkanmi Fafowora color hsl()
.element { color: hsl(90deg, 50%, 50%); }
Sunkanmi Fafowora color hwb()
.element { color: hwb(136 40% 15%); }
Gabriel Shoyombo color CSS functions lab()
.element { color: lab(50% 50% 50% / 0.5); }
Sunkanmi Fafowora at-rules lch()
.element { color: lch(10% 0.215 15deg); }
Sunkanmi Fafowora color oklab()
.element { color: oklab(25.77% 25.77% 54.88%; }
Sunkanmi Fafowora color oklch()
.element { color: oklch(70% 0.15 240); }
Gabriel Shoyombo color color()
.element { color: color(rec2020 0.5 0.15 0.115 / 0.5); }
Sunkanmi Fafowora Color properties
accent-color
.element { accent-color: #f8a100; }
Geoff Graham background-color
.element { background-color: #ff7a18; }
Chris Coyier caret-color forms caret-color
.element { caret-color: red; }
Chris Coyier color
.element { color: #f8a100; }
Sara Cope column-rule-color
.element { column-rule-color: #f8a100; }
Geoff Graham fill
.element { fill: red; }
Geoff Graham outline outline-color
.element { outline-color: #f8a100; }
Mojtaba Seyedi stroke
.module { stroke: black;}
Geoff Graham text-decoration-color
.element { text-decoration-color: orange; }
Marie Mosley text-emphasis
.element { text-emphasis: circle red; }
Joel Olawanle text-shadow
p { text-shadow: 1px 1px 1px #000; }
Sara Cope Related articles & tutorials
accessibility color generative color hex values hsl web colors Working With Colors Guide
Sarah Drasner color color() color-gamut display-p3 lab lch web colors The Expanding Gamut of Color on the Web
Ollie Williams The Tragicomic History of CSS Color Names
Geoff Graham color color() display-p3 hsl hwb lab lch rgba A Whistle-Stop Tour of 4 New CSS Color Features
Chris Coyier color easing gradients Using Different Color Spaces for Non-Boring Gradients
Chris Coyier dark mode functions UI/IX Design Come to the light-dark() Side
Sara Joy Color Mixing With Animation Composition
Geoff Graham color hex 8-Digit Hex Codes?
Chris Coyier custom properties dark mode A DRY Approach to Color Themes in CSS
Christopher Kirk-Nielsen Accessibility Basics: Testing Your Page For Color Blindness
Chris Coyier transparency Adventures in CSS Semi-Transparency Land
Ana Tudor border tables Change Color of All Four Borders Even With `border-collapse: collapse;`
Daniel Jauch a11y accessibility color color color-adjust Color contrast accessibility tools
Robin Rendle custom properties Contextual Utility Classes for Color with Custom Properties
Christopher Kirk-Nielsen calc color custom properties hsl Creating Color Themes With Custom Properties, HSL, and a Little calc()
Dieter Raber background emoji shadow Creating Colorful, Smart Shadows
Chris Coyier background-image css basics CSS Basics: Using Fallback Colors
Chris Coyier accessibility accessibility color design systems Designing accessible color systems
Robin Rendle color keyframes Mixing Colors in Pure CSS
Carter Li Overriding The Default Text Selection Color With CSS
Chris Coyier Reverse Text Color Based on Background Color Automatically in CSS
Robin Rendle color So Many Color Links
Chris Coyier accessibility CSS css variables hsl rgba Switch font color for different backgrounds with CSS
Facundo Corradini accessibility color color hsl lab The Best Color Functions in CSS?
Chris Coyier color variables What do you name color variables?
Chris Coyier color Why is Nobody Using the hwb() Color Function?
Sunkanmi Fafowora Table of contents
color()functioncolor-mix()functionComments
Leave a Reply Cancel reply
- 最近发表
- 随机阅读
-
- Roadmap Planning Tools
- Sustainability Metrics Dashboard
- Fog Computing Platform
- Data Protection System
- Multilingual Website Setup
- Team Communication Tools
- Send Time Optimization
- Release Notes Generator
- The freeCodeCamp.org Copyright Policy
- Mixing Console Configuration
- Employee Directory System
- File Sharing Platform Security
- The Docker Handbook – Learn Docker for Beginners
- Diagnostic Tools Platform
- Database Management Best Practices Guide
- Board Management System
- The Software Architecture Handbook
- Recipe Management Platform
- Heart Rate Calculator
- Social Impact Tools
- 搜索
-
Thank you, Sunkanmi, for this great guide! I’ve had trouble wrapping my head around all the color formats other than sRGB, and this really helped! Great job!
ReplyYou’re welcome!
I’m super glad you found this useful. :)
I’d comment on how incredibly useful this guide is, but i’ve got some serious experimentingto do just now
ReplyThank you Sunkanmi for this very simple and easy to understand guide!
ReplyThanks men now I understand how to use colors.Actually I wish to how was your experience in learning how to build a website.Me am being coached by a friend and now am doing css but sometimes I get hard for me.
ReplyPlz men I would wish to know more from you in website development in college, bcoz me am self taught.
ReplyThank you so much for this article! Had no idea CSS supported such color manipulation. You have made my life much easier!
Reply