CSS Colors
Learn how to add colors in CSS using color names, HEX, RGB, RGBA, HSL, HSLA, transparency, and the currentColor keyword.
Introduction
In the previous lesson, you learned how CSS selectors identify the HTML elements that should receive styles.
Now that you know how to select elements, the next step is to change their visual appearance.
Color is one of the most important visual properties in CSS.
Colors can make a webpage attractive, improve readability, communicate meaning, create hierarchy, and establish a visual identity.
CSS provides several ways to define colors, including color names, hexadecimal values, RGB, RGBA, HSL, and HSLA.
Colors can communicate success, danger, warnings, information, emphasis, branding, and interaction states.
What are CSS Colors?
CSS colors are values used to control the color of text, backgrounds, borders, shadows, outlines, and many other visual parts of an element.
p {
color: blue;
}The color property controls the foreground color of an element, which commonly means the text color.
<p>
Welcome to CSS Colors
</p>p {
color: blue;
}| CSS Property | Color Controls |
|---|---|
| color | Text and foreground color |
| background-color | Element background |
| border-color | Border color |
| outline-color | Outline color |
| text-decoration-color | Underline or decoration color |
| box-shadow | Box shadow color |
| text-shadow | Text shadow color |
Why Do We Need Colors?
Without colors, most webpages would be difficult to understand visually and would lack identity.
Colors help users quickly recognize important information and understand the purpose of interface elements.
<div class="success">
Payment Successful
</div>
<div class="warning">
Your subscription expires soon
</div>
<div class="error">
Payment Failed
</div>.success {
color: green;
}
.warning {
color: orange;
}
.error {
color: red;
}Without Meaningful Colors
- Important information is harder to identify.
- Interface states may look identical.
- Visual hierarchy becomes weaker.
- Brand identity is limited.
- The interface may feel unfinished.
With Meaningful Colors
- Important information stands out.
- States are easier to recognize.
- Visual hierarchy becomes clearer.
- Brand identity becomes stronger.
- The interface feels more polished.
Color should support meaning, not be the only way to communicate it. Use text, icons, labels, or other visual indicators when information is important.
Real-World Analogy
Think about traffic lights.
Each color communicates a different meaning.
| Traffic Color | Meaning | Website Example |
|---|---|---|
| Red | Stop or danger | Error message |
| Yellow | Warning or caution | Warning alert |
| Green | Safe or continue | Success message |
| Blue | Information | Information alert |
Red → Danger / Error
Yellow → Warning / Attention
Green → Success / Complete
Blue → Information / ActionUsers often recognize a color pattern before they finish reading the associated text.
Where Can Colors Be Used?
CSS colors can be used with many different properties.
.card {
color: white;
background-color: purple;
border-color: black;
outline-color: orange;
}<div class="card">
CSS Color Card
</div>| Property | Example |
|---|---|
| color | color: red; |
| background-color | background-color: blue; |
| border-color | border-color: green; |
| outline-color | outline-color: orange; |
| text-decoration-color | text-decoration-color: purple; |
| box-shadow | box-shadow: 0 4px 10px gray; |
| text-shadow | text-shadow: 2px 2px 4px black; |
Once you understand CSS color values, you can use them with many properties that accept a color.
Ways to Define Colors
CSS supports multiple ways to represent colors.
| Color Format | Example |
|---|---|
| Color Name | red |
| HEX | #ff0000 |
| RGB | rgb(255, 0, 0) |
| RGBA | rgba(255, 0, 0, 0.5) |
| HSL | hsl(0, 100%, 50%) |
| HSLA | hsla(0, 100%, 50%, 0.5) |
.one {
color: red;
}
.two {
color: #ff0000;
}
.three {
color: rgb(255, 0, 0);
}
.four {
color: hsl(0, 100%, 50%);
}All four rules above can produce the same red color.
1. Color Names
The simplest way to define a color is to use a predefined CSS color name.
h1 {
color: red;
}
p {
color: blue;
}
button {
background-color: green;
}<h1>Red Heading</h1>
<p>Blue Paragraph</p>
<button>Green Button</button>| Color Name | Typical Appearance |
|---|---|
| red | Red |
| blue | Blue |
| green | Green |
| yellow | Yellow |
| orange | Orange |
| purple | Purple |
| black | Black |
| white | White |
| gray | Gray |
| pink | Pink |
Advantages
- Easy to read.
- Easy to remember.
- Good for simple examples.
- Useful for quick testing.
Limitations
- Less precise for custom designs.
- Names may not match brand colors.
- Large design systems need more control.
- Fine shade adjustments are limited.
Color names are easy for beginners, but real projects often use HEX, RGB, or HSL values for precise control.
2. HEX Colors
HEX colors represent colors using hexadecimal numbers.
A standard HEX color begins with a hash symbol followed by six hexadecimal characters.
selector {
color: #RRGGBB;
}.red {
color: #ff0000;
}
.green {
color: #00ff00;
}
.blue {
color: #0000ff;
}| HEX Value | Color |
|---|---|
| #ff0000 | Red |
| #00ff00 | Green |
| #0000ff | Blue |
| #ffffff | White |
| #000000 | Black |
| #808080 | Gray |
| #ffff00 | Yellow |
| #800080 | Purple |
HEX Color Structure
A six-digit HEX color contains three pairs of values.
#RRGGBB
RR → Red
GG → Green
BB → BlueEach pair can range from 00 to ff.
| HEX Pair | Intensity |
|---|---|
| 00 | Minimum intensity |
| 33 | Low intensity |
| 80 | Medium intensity |
| cc | High intensity |
| ff | Maximum intensity |
#ff0000
ff → Maximum Red
00 → No Green
00 → No Blue
Result → Pure Red#800080
80 → Medium Red
00 → No Green
80 → Medium Blue
Result → PurpleShorthand HEX Colors
Some six-digit HEX colors can be shortened to three digits when each pair contains identical characters.
#ff0000 → #f00
#00ff00 → #0f0
#0000ff → #00f
#ffffff → #fff
#000000 → #000#abc
becomes
#aabbcc.box {
color: #fff;
background-color: #00f;
}#12ab34 cannot be shortened because the characters in each pair are not identical.
3. RGB Colors
RGB colors define a color using red, green, and blue channels.
Each channel commonly uses a number from 0 to 255.
rgb(red, green, blue).red {
color: rgb(255, 0, 0);
}
.green {
color: rgb(0, 255, 0);
}
.blue {
color: rgb(0, 0, 255);
}| RGB Value | Result |
|---|---|
| rgb(255, 0, 0) | Red |
| rgb(0, 255, 0) | Green |
| rgb(0, 0, 255) | Blue |
| rgb(255, 255, 255) | White |
| rgb(0, 0, 0) | Black |
| rgb(128, 128, 128) | Gray |
RGB Color Mixing
RGB creates colors by mixing different intensities of red, green, and blue light.
Red + Green → Yellow
Red + Blue → Magenta
Green + Blue → Cyan
Red + Green + Blue
at maximum → White
All channels at 0 → Black| Red | Green | Blue | Result |
|---|---|---|---|
| 255 | 0 | 0 | Red |
| 0 | 255 | 0 | Green |
| 0 | 0 | 255 | Blue |
| 255 | 255 | 0 | Yellow |
| 255 | 0 | 255 | Magenta |
| 0 | 255 | 255 | Cyan |
| 255 | 255 | 255 | White |
| 0 | 0 | 0 | Black |
.yellow {
background-color: rgb(255, 255, 0);
}
.magenta {
background-color: rgb(255, 0, 255);
}
.cyan {
background-color: rgb(0, 255, 255);
}4. RGBA Colors
RGBA extends RGB by adding an alpha value for transparency.
rgba(red, green, blue, alpha)The alpha value commonly ranges from 0 to 1.
| Alpha Value | Appearance |
|---|---|
| 0 | Fully transparent |
| 0.25 | 25% opaque |
| 0.5 | 50% opaque |
| 0.75 | 75% opaque |
| 1 | Fully opaque |
.one {
background-color: rgba(255, 0, 0, 1);
}
.two {
background-color: rgba(255, 0, 0, 0.7);
}
.three {
background-color: rgba(255, 0, 0, 0.4);
}
.four {
background-color: rgba(255, 0, 0, 0.1);
}Colors with alpha transparency are commonly used for overlays, shadows, subtle borders, badges, and translucent backgrounds.
5. HSL Colors
HSL represents colors using hue, saturation, and lightness.
hsl(hue, saturation, lightness)| Part | Meaning |
|---|---|
| Hue | Position on the color wheel |
| Saturation | Intensity of the color |
| Lightness | How light or dark the color appears |
.red {
color: hsl(0, 100%, 50%);
}
.green {
color: hsl(120, 100%, 50%);
}
.blue {
color: hsl(240, 100%, 50%);
}Understanding Hue
Hue represents a position on a circular color wheel.
The hue is commonly written as an angle from 0 to 360 degrees.
| Hue | Approximate Color |
|---|---|
| 0° | Red |
| 60° | Yellow |
| 120° | Green |
| 180° | Cyan |
| 240° | Blue |
| 300° | Magenta |
| 360° | Red again |
0° / 360°
Red
│
300° │ 60°
Magenta │ Yellow
\ │ /
\ │ /
240° Blue ───── Color ───── 120° Green
/ Wheel \
/ \
180° Cyan.one {
background-color: hsl(0, 100%, 50%);
}
.two {
background-color: hsl(60, 100%, 50%);
}
.three {
background-color: hsl(120, 100%, 50%);
}
.four {
background-color: hsl(240, 100%, 50%);
}Saturation & Lightness
Saturation controls the intensity or purity of an HSL color.
A saturation of 100% produces a vivid color, while 0% removes the color intensity and produces a gray shade.
.one {
background-color: hsl(240, 100%, 50%);
}
.two {
background-color: hsl(240, 60%, 50%);
}
.three {
background-color: hsl(240, 20%, 50%);
}
.four {
background-color: hsl(240, 0%, 50%);
}Lightness controls how light or dark the color appears.
| Lightness | Appearance |
|---|---|
| 0% | Black |
| 25% | Dark color |
| 50% | Normal color |
| 75% | Light color |
| 100% | White |
.one {
background-color: hsl(240, 100%, 20%);
}
.two {
background-color: hsl(240, 100%, 40%);
}
.three {
background-color: hsl(240, 100%, 60%);
}
.four {
background-color: hsl(240, 100%, 80%);
}6. HSLA Colors
HSLA extends HSL by adding an alpha value for transparency.
hsla(hue, saturation, lightness, alpha).one {
background-color: hsla(240, 100%, 50%, 1);
}
.two {
background-color: hsla(240, 100%, 50%, 0.7);
}
.three {
background-color: hsla(240, 100%, 50%, 0.4);
}
.four {
background-color: hsla(240, 100%, 50%, 0.1);
}HSL
- Uses hue.
- Uses saturation.
- Uses lightness.
- No separate alpha value.
HSLA
- Uses hue.
- Uses saturation.
- Uses lightness.
- Adds alpha transparency.
The Alpha Channel
The alpha channel controls the opacity of an individual color.
It allows the background behind the color to remain partially visible.
<div class="background">
<div class="overlay">
Transparent Overlay
</div>
</div>.background {
background-color: blue;
}
.overlay {
background-color: rgba(255, 0, 0, 0.5);
}opacity Property
- Affects the entire element.
- Can affect text and children.
- Changes overall element opacity.
Alpha Color
- Affects only that color value.
- Text can remain fully opaque.
- Useful for transparent backgrounds.
.box-one {
background-color: red;
opacity: 0.5;
}
.box-two {
background-color: rgba(255, 0, 0, 0.5);
}If only the background should be transparent while the text remains fully visible, use a color with an alpha channel instead of reducing the opacity of the entire element.
Transparent Color
CSS provides the transparent keyword for a fully transparent color.
.button {
background-color: transparent;
border: 2px solid blue;
color: blue;
}.transparent-background {
background-color: transparent;
}
.transparent-border {
border-color: transparent;
}The transparent keyword creates a fully transparent color and can be used where CSS accepts a color value.
The currentColor Keyword
The currentColor keyword uses the current value of the color property.
.box {
color: purple;
border: 3px solid currentColor;
}Because the text color is purple, the border also becomes purple.
<div class="box">
Matching Text and Border
</div>.button {
color: green;
border: 2px solid currentColor;
}
.icon {
color: blue;
fill: currentColor;
}currentColor allows borders, icons, and other color-aware parts to automatically follow the element foreground color.
Applying Colors to Elements
Different CSS properties can use different color values on the same element.
<div class="card">
<h2>CSS Course</h2>
<p>
Learn modern CSS step by step.
</p>
<button>
Start Learning
</button>
</div>.card {
background-color: #f4f0ff;
border: 2px solid #6c5ce7;
}
.card h2 {
color: #6c5ce7;
}
.card p {
color: rgb(80, 80, 80);
}
.card button {
background-color: hsl(248, 74%, 63%);
color: white;
}| Element Part | Color Format Used |
|---|---|
| Card Background | HEX |
| Card Border | HEX |
| Heading | HEX |
| Paragraph | RGB |
| Button Background | HSL |
| Button Text | Color Name |
Complete Color Example
The following example combines multiple CSS color formats in one interface.
<div class="profile-card">
<div class="badge">
PRO
</div>
<h2>Alex Johnson</h2>
<p>
Frontend Developer
</p>
<button>
View Profile
</button>
</div>.profile-card {
background-color: #ffffff;
border: 2px solid rgb(108, 92, 231);
}
.badge {
background-color: rgba(0, 184, 148, 0.15);
color: rgb(0, 130, 105);
}
.profile-card h2 {
color: hsl(248, 53%, 45%);
}
.profile-card p {
color: #666666;
}
.profile-card button {
background-color: hsl(248, 74%, 63%);
color: white;
}Browser Rendering Flow
When the browser encounters CSS colors, it parses the color values and converts them into colors that can be painted on the screen.
CSS Rule
│
▼
Read Color Property
│
▼
Read Color Value
│
├── Color Name
├── HEX
├── RGB / RGBA
└── HSL / HSLA
│
▼
Validate Value
│
▼
Calculate Final Color
│
▼
Combine Alpha if Present
│
▼
Paint Element
│
▼
Display Output| Stage | Purpose |
|---|---|
| Read Property | Determine what part receives the color |
| Parse Value | Understand the color format |
| Validate | Check whether the value is valid |
| Calculate | Determine the actual color |
| Composite | Combine transparent colors with backgrounds |
| Paint | Draw the final pixels |
If a browser cannot understand a color value, the invalid declaration is ignored.
Real-World Applications
Colors are used throughout modern websites and applications.
Branding
Use consistent brand colors across websites and applications.
Alerts
Use visual colors for success, warning, error, and information messages.
Buttons
Differentiate primary, secondary, dangerous, and disabled actions.
Navigation
Highlight active links, hover states, and selected sections.
Dashboards
Represent categories, statuses, charts, and metrics.
E-Commerce
Highlight prices, discounts, stock states, and promotional badges.
Themes
Create light, dark, and custom color themes.
Components
Style cards, borders, backgrounds, text, and interactive states.
Advantages
CSS provides flexible color systems for simple pages and complex design systems.
Visual Design
Colors make interfaces more attractive and expressive.
Visual Hierarchy
Important elements can receive stronger visual emphasis.
Communication
Colors help communicate states and categories.
Branding
Consistent colors strengthen visual identity.
Multiple Formats
Choose names, HEX, RGB, HSL, and alpha-enabled formats.
Transparency
Alpha channels create overlays and subtle effects.
Reusability
Color values can be reused across components and themes.
Flexible Interfaces
Colors support websites, applications, dashboards, and responsive designs.
Common Beginner Mistakes
Write #ff0000, not ff0000.
HEX values use digits 0-9 and letters a-f.
Use a valid HEX color length such as three or six digits for the common formats taught here.
#aabbcc can become #abc, but #12ab34 cannot be reduced to three digits.
Traditional numeric RGB channels are commonly written from 0 to 255.
RGB uses function syntax, while HEX begins with a hash symbol.
When using the comma-based syntax taught here, separate the channel values correctly.
An alpha color affects that color value, while opacity affects the whole element.
In the common decimal syntax taught here, use values such as 0, 0.5, and 1.
Saturation and lightness are written as percentages.
Hue represents a position on the color wheel rather than a red, green, or blue channel.
In HSL, 100% lightness produces white.
In HSL, 0% lightness produces black.
Saturation controls color intensity, not transparency.
The opacity property also affects the content and child elements.
Text can become difficult to read when its color is too similar to the background.
Important states should also use text, icons, labels, or other indicators.
A large number of random colors can make the interface inconsistent.
Inconsistent color formats can make a stylesheet harder to maintain.
Large projects should centralize repeated colors using reusable strategies such as CSS variables.
Best Practices
- Choose a consistent color palette for the project.
- Use colors intentionally rather than randomly.
- Use color to support visual hierarchy.
- Use color to communicate interface states.
- Do not depend only on color to communicate important information.
- Combine important colors with text labels or icons.
- Maintain sufficient contrast between text and backgrounds.
- Test text readability on different screens.
- Check interactive elements in normal, hover, focus, active, and disabled states.
- Use color names for simple examples and quick testing.
- Use precise color formats for custom interface designs.
- Remember that HEX colors begin with a hash symbol.
- Use valid hexadecimal characters in HEX values.
- Understand the #RRGGBB structure.
- Use shorthand HEX only when each pair can be correctly reduced.
- Remember that #aabbcc can become #abc.
- Do not shorten arbitrary six-digit HEX values.
- Understand that RGB represents red, green, and blue channels.
- Use appropriate channel values in RGB.
- Remember that rgb(255, 0, 0) represents red.
- Remember that rgb(0, 255, 0) represents green.
- Remember that rgb(0, 0, 255) represents blue.
- Use alpha transparency when only a specific color should be translucent.
- Use rgba() when working with the traditional RGBA syntax taught in this lesson.
- Use alpha values intentionally.
- Use 0 for full transparency.
- Use 1 for full opacity.
- Use intermediate alpha values for partial transparency.
- Understand that HSL means hue, saturation, and lightness.
- Use hue to choose the base color family.
- Use saturation to control color intensity.
- Use lightness to control how light or dark a color appears.
- Remember that 0% saturation creates a gray shade.
- Remember that 0% lightness creates black.
- Remember that 100% lightness creates white.
- Use HSL when creating related shades and color variations.
- Use HSLA when HSL colors need transparency.
- Understand the difference between alpha transparency and the opacity property.
- Use alpha colors when text or child content should remain fully opaque.
- Use the transparent keyword when a fully transparent color is needed.
- Use currentColor when another property should follow the element foreground color.
- Use currentColor for matching borders and icons when appropriate.
- Keep brand colors consistent across the application.
- Use consistent colors for success states.
- Use consistent colors for warning states.
- Use consistent colors for error states.
- Use consistent colors for information states.
- Do not assume every user perceives colors in the same way.
- Test color combinations for accessibility.
- Avoid extremely low-contrast placeholder text.
- Ensure links remain recognizable.
- Provide visible focus states for keyboard users.
- Do not remove focus colors without providing a clear replacement.
- Use transparent overlays carefully.
- Ensure text remains readable over translucent backgrounds.
- Consider the background beneath transparent colors.
- Remember that the same transparent color can look different over different backgrounds.
- Use a limited number of primary interface colors.
- Create lighter and darker variations systematically.
- Avoid using a different random color for every component.
- Keep color roles consistent.
- Use one color role for primary actions.
- Use another role for dangerous actions.
- Use neutral colors for secondary information.
- Avoid hardcoding the same color value repeatedly in large projects.
- Use CSS variables for repeated project colors.
- Use meaningful variable names such as --color-primary.
- Prefer role-based names over temporary visual names when building themes.
- Use --color-danger rather than --red when the value represents an error role.
- Keep color definitions centralized in larger applications.
- Document important design-system colors.
- Test colors in both light and dark themes when both are supported.
- Check transparent colors in every supported theme.
- Check borders against their surrounding backgrounds.
- Check disabled controls for readability.
- Check error messages for clear visibility.
- Check success messages for clear visibility.
- Check selected navigation states.
- Check hover states on pointer devices.
- Check focus states using keyboard navigation.
- Check touch interfaces without relying on hover.
- Use browser developer tools to inspect computed color values.
- Use the browser color picker when debugging.
- Check whether another CSS rule overrides the intended color.
- Check inherited color values when text colors appear unexpectedly.
- Remember that the color property can be inherited by descendants.
- Remember that background-color is not inherited by default.
- Use inheritance intentionally.
- Avoid unnecessary color declarations on every nested element.
- Use currentColor when inheritance can simplify related colors.
- Keep the chosen color format consistent when practical.
- Do not convert formats only for appearance.
- Choose a format that fits the design workflow.
- Use HEX when the project convention prefers compact fixed colors.
- Use RGB when channel-based values are convenient.
- Use HSL when adjusting hue, saturation, and lightness is useful.
- Use alpha-enabled colors for transparency.
- Validate color values when styles do not appear.
- Remember that invalid color declarations are ignored.
- Practice converting simple colors between formats.
- Practice creating red, green, and blue using RGB.
- Practice creating lighter and darker shades using HSL.
- Practice adding transparency with alpha values.
- Practice using colors on text.
- Practice using colors on backgrounds.
- Practice using colors on borders.
- Practice using colors on shadows.
- Build small color palettes before styling complete pages.
- Use colors consistently.
- Keep colors readable.
- Keep colors accessible.
- Keep colors maintainable.
A good interface uses colors consistently according to roles such as primary, secondary, success, warning, danger, background, and text.
Frequently Asked Questions
How do I change text color in CSS?
Use the color property, such as color: blue;.
How do I change background color?
Use the background-color property, such as background-color: yellow;.
What color formats does CSS support?
Common formats include color names, HEX, RGB, RGBA, HSL, and HSLA.
What is the easiest CSS color format?
Color names are usually the easiest for beginners because values such as red, blue, and green are easy to read.
What is a HEX color?
A HEX color represents red, green, and blue values using hexadecimal notation, such as #ff0000.
Why does HEX start with #?
The hash symbol identifies the following value as hexadecimal color notation in CSS.
What does #ff0000 mean?
It means maximum red, no green, and no blue, producing pure red.
What does #ffffff mean?
It represents white.
What does #000000 mean?
It represents black.
Can HEX colors be shortened?
Yes, when each pair contains identical characters. For example, #aabbcc can become #abc.
Can #12ab34 be shortened?
No. Its character pairs cannot be correctly reduced to single characters.
What does RGB mean?
RGB means red, green, and blue.
What is the RGB value for red?
rgb(255, 0, 0).
What is the RGB value for white?
rgb(255, 255, 255).
What is the RGB value for black?
rgb(0, 0, 0).
What is RGBA?
RGBA is RGB with an additional alpha value used to control transparency.
What does an alpha value of 0 mean?
It means fully transparent.
What does an alpha value of 1 mean?
It means fully opaque.
What does an alpha value of 0.5 mean?
It creates a partially transparent color at approximately half opacity.
What does HSL mean?
HSL means hue, saturation, and lightness.
What does hue control?
Hue controls the base color position on the color wheel.
What does saturation control?
Saturation controls the intensity or purity of the color.
What does lightness control?
Lightness controls how light or dark the color appears.
What happens at 0% saturation?
The color loses its hue intensity and becomes a gray shade.
What happens at 0% lightness?
The result is black.
What happens at 100% lightness?
The result is white.
What is HSLA?
HSLA is HSL with an additional alpha value for transparency.
What is the difference between RGBA and HSLA?
RGBA defines the base color using red, green, and blue channels, while HSLA uses hue, saturation, and lightness. Both include alpha transparency.
What is the transparent keyword?
It represents a fully transparent color.
What is currentColor?
currentColor uses the current value of the color property.
What is the difference between alpha and opacity?
Alpha affects an individual color value, while the opacity property affects the entire element, including its content.
Why is my CSS color not working?
The color value may be invalid, the property may be incorrect, the selector may not match, or another CSS declaration may override it.
Which color format should I use?
Use the format that fits your project and workflow. HEX is common for fixed colors, RGB is useful for channel-based values, and HSL is convenient for creating related color variations.
Should I use color names in professional projects?
They are valid, but precise custom designs often use HEX, RGB, HSL, or reusable CSS variables.
Can I use different color formats in the same project?
Yes, although consistency can make a stylesheet easier to understand and maintain.
Why is color contrast important?
Good contrast helps users read text and identify interface elements more easily.
Should color be the only way to show an error?
No. Important states should also use text, icons, labels, or other indicators.
What comes after CSS colors?
The next lesson covers CSS units and how values such as px, %, em, rem, vw, and vh are used.
Key Takeaways
- CSS colors control the visual colors of webpage elements.
- The color property commonly controls text color.
- The background-color property controls an element background.
- The border-color property controls border color.
- CSS supports multiple color formats.
- Color names are simple and easy to read.
- HEX colors begin with a hash symbol.
- Standard six-digit HEX colors use the #RRGGBB structure.
- RR represents red.
- GG represents green.
- BB represents blue.
- HEX channel values range from 00 to ff.
- #ff0000 represents red.
- #00ff00 represents green.
- #0000ff represents blue.
- #ffffff represents white.
- #000000 represents black.
- Some six-digit HEX values can be shortened.
- #aabbcc can be written as #abc.
- RGB means red, green, and blue.
- Traditional numeric RGB channels commonly range from 0 to 255.
- rgb(255, 0, 0) represents red.
- rgb(0, 255, 0) represents green.
- rgb(0, 0, 255) represents blue.
- RGBA adds an alpha channel to RGB.
- Alpha controls color transparency.
- An alpha value of 0 is fully transparent.
- An alpha value of 1 is fully opaque.
- HSL means hue, saturation, and lightness.
- Hue selects the base color family.
- Saturation controls color intensity.
- Lightness controls how light or dark a color appears.
- A saturation of 0% produces a gray shade.
- A lightness of 0% produces black.
- A lightness of 100% produces white.
- HSLA adds alpha transparency to HSL.
- The transparent keyword represents a fully transparent color.
- currentColor uses the current value of the color property.
- Alpha colors and the opacity property behave differently.
- Alpha affects an individual color.
- Opacity affects the entire element.
- Transparent colors can look different over different backgrounds.
- Colors should be used consistently.
- Colors can communicate states and meaning.
- Important information should not depend only on color.
- Good contrast improves readability.
- Invalid color declarations are ignored.
- CSS colors are essential for branding, hierarchy, states, and interface design.
Summary
CSS colors control the appearance of text, backgrounds, borders, shadows, outlines, and many other visual parts of a webpage.
Color names provide a simple way to define common colors such as red, blue, green, black, and white.
HEX colors represent red, green, and blue using hexadecimal values in a format such as #RRGGBB.
RGB colors define red, green, and blue channels using numeric values.
RGBA extends RGB with an alpha channel for transparency.
HSL represents colors using hue, saturation, and lightness, making it useful for creating related color variations.
HSLA extends HSL with alpha transparency.
The transparent keyword creates a fully transparent color, while currentColor allows another property to use the current foreground color.
Alpha transparency affects an individual color value, while the opacity property affects the entire element and its content.
Good color choices improve readability, hierarchy, communication, branding, and the overall user experience.
Colors should be used consistently, with sufficient contrast, and should not be the only method used to communicate important information.
In the next lesson, you will learn CSS units and understand how values such as pixels, percentages, em, rem, viewport width, and viewport height control sizes and spacing.