Use CSS color values to style text, backgrounds, and borders while maintaining readable contrast.
CSS lets you choose colors for different parts of an element. The color property sets text color, background-color sets the surface behind it, and border-color sets the color of its border. Each property controls a different part, so changing the background does not change the text color.
.color-box {
color: #333333;
background-color: #cce7ff;
border: 2px solid;
border-color: #004080;
padding: 20px;
}This rule gives the box dark text, a light blue background, and a dark blue border. The border declaration provides a solid border, and border-color gives it a color. One value colors all four sides; four values can give the sides different colors, as in border-color: red green blue yellow;.
The color property is inherited: child elements usually use their parent's text color unless they have a color of their own.
.parent {
color: blue;
}
.override {
color: red;
}The first child has no color declaration, so it inherits blue from .parent. The second child has its own declaration, so it uses red instead. Inheritance makes it possible to set a shared text color on a parent and change a child when needed.
CSS accepts several ways to describe a color. You can use these values with properties such as color and background-color.
Named colors are predefined identifiers such as red, green, and blue. They are easy to read and use:
body {
background-color: lightblue;
}
h1 {
color: navy;
}Each name represents a specific color. For example, red corresponds to #ff0000, while green corresponds to #008000. Names are convenient when you do not need to specify a precise shade.
A six-digit hexadecimal color has the form #RRGGBB. Each pair describes the intensity of red, green, or blue, in that order. Hexadecimal uses digits from 0 to 9 and letters from A to F; 00 is the lowest intensity and FF is the highest.
For example, #FF5733 combines full red (FF), moderate green (57), and lower blue (33) to produce a red-orange. Letters can be uppercase or lowercase: #FFFFFF and #ffffff both mean white.
When all three components are equal, the result is a neutral gray. #000000 is black, #808080 is a middle gray, and #FFFFFF is white. Increasing the equal components makes the gray lighter.
body {
background-color: #eeeeee;
}
h1 {
color: #333333;
}Here the heading's darker gray stands against a lighter gray background.
RGB describes a color by combining red, green, and blue intensities. In the examples below, each component ranges from 0 to 255:
h1 {
color: rgb(255, 105, 180);
}The first value controls red, the second green, and the third blue. For comparison, rgb(255, 0, 0) is red, rgb(0, 0, 255) is blue, and rgb(255, 255, 255) is white. Setting all three to 0 gives black.
HSL describes color using hue, saturation, and lightness:
0 or 360 is red, 120 is green, and 240 is blue.0% gives gray, while 100% gives full saturation.0% is black, 100% is white, and 50% is in the middle.h1 {
color: hsl(240, 100%, 50%);
}This produces a vivid blue: the hue selects blue, the saturation is full, and lightness is at the midpoint. HSL makes it convenient to adjust saturation or lightness while keeping the hue the same.
An alpha channel controls the opacity of a color. An alpha of 0 is completely transparent, while 1 is completely opaque. Values between them let the background behind the color show through.
.color-box {
background-color: rgba(255, 87, 51, 0.3);
}The first three values specify the orange color; 0.3 makes that background color 30% opaque. The alpha belongs to the background color, so it does not itself fade the box's text. You can also add alpha using hsla(), as in hsla(12, 100%, 57%, 0.5). In an eight-digit hex color such as #FF573380, the last two digits represent alpha.
The opacity property also ranges from 0 to 1, but it affects the appearance of the whole element, including its children:
.container {
opacity: 0.6;
}If the container holds an image and text, both appear 60% opaque along with the container. Use an alpha value on the background color when you want that color to be translucent while keeping the text unaffected.
These two keywords help connect an element's colors to its surroundings and its own text:
transparent: A fully transparent color. For example, background-color: transparent lets what is behind the background show through; it does not make the element's text invisible.currentColor: The element's color value. You can use it to keep a border matched to the text.For example, a card can use both values:
.card {
color: #333333;
border: 2px dashed currentColor;
background-color: transparent;
}The border uses the card's text color. If that color changes, the border follows it, and the transparent background lets the surface behind the card show through.
CSS custom properties, also called variables, store values for reuse. Define a name with --, then use its value with var():
:root {
--color-primary: #195b87;
--color-secondary: #2ecc71;
}
.button {
background-color: var(--color-primary);
color: white;
}
.header {
border-bottom: 2px solid var(--color-secondary);
}White button text sits on the dark blue primary color, and the header uses the secondary color for its border. Reusing a variable helps keep colors consistent: changing its definition updates the places that use it.
Custom properties can also be scoped to an element. The following rules define a light theme and replace its values when the body has the dark-mode class:
:root {
--background-color: #fff;
--text-color: #000;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
body.dark-mode {
--background-color: #333;
--text-color: #fff;
}Adding dark-mode to the body changes the variables used for its background and text. Check contrast for each color pair you use, including pairs in different themes.
Try it yourself
HTML + CSS
color, background-color, and border-color style text, backgrounds, and borders separately.opacity affects the appearance of an element and its children.