← Back to tutorials

CSS Colors & Units

Understand how to use different color formats in CSS and various CSS units.

CSS Colors & Units Overview

CSS provides various ways to specify colors and measurement units, giving you precise control over the visual appearance of your web pages.

Color Systems:
  • Named Colors: red, blue, transparent
  • Hexadecimal: #FF0000, #00FF00
  • RGB/RGBA: rgb(255,0,0), rgba(255,0,0,0.5)
  • HSL/HSLA: hsl(0,100%,50%), hsla(0,100%,50%,0.5)
  • Modern Formats: OKLCH, LCH, Lab
Unit Categories:
  • Absolute Units: px, pt, cm, in
  • Relative Units: em, rem, %, ch
  • Viewport Units: vw, vh, vmin, vmax
  • Flexible Units: fr, min-content, max-content
Best Practices:
  • Use relative units for responsive designs
  • Choose HSL for intuitive color manipulation
  • Use rem units for consistent spacing
  • Leverage viewport units for full-screen layouts
  • Consider accessibility with color contrast

CSS Color Formats

CSS supports multiple color formats, each with its own advantages and use cases.

Format Example Description Use Case
Hexadecimal #FF5733 6-digit hex code (RRGGBB) Web design, brand colors
RGB rgb(255, 87, 51) Red, Green, Blue values (0-255) Programmatic color generation
RGBA rgba(255, 87, 51, 0.7) RGB with alpha transparency Overlays, glass effects
HSL hsl(12, 100%, 60%) Hue, Saturation, Lightness Color manipulation, themes
HSLA hsla(12, 100%, 60%, 0.7) HSL with alpha transparency Modern UI design
Color Format Examples:
#FF5733
rgb(255,87,51)
rgba(255,87,51,0.7)
hsl(12,100%,60%)
hsla(12,100%,60%,0.7)
/* Different color formats in practice */
.primary-button {
    background-color: #4e73df; /* Hexadecimal */
    color: rgb(255, 255, 255); /* RGB */
}

.overlay {
    background-color: rgba(0, 0, 0, 0.5); /* RGBA with transparency */
}

.theme-accent {
    background-color: hsl(210, 64%, 50%); /* HSL for easy adjustments */
}

.glass-effect {
    background-color: hsla(0, 0%, 100%, 0.1); /* HSLA for modern UI */
}

CSS Color Keywords

CSS provides a set of predefined color names that are easy to remember and use.

Basic Color Keywords:
red
blue
green
yellow
purple
orange
pink
brown
System Color Keywords:
currentColor
transparent
inherit
initial
unset
/* Using color keywords */
.warning {
    background-color: orange;
    color: white;
}

.success {
    background-color: green;
    color: white;
}

.transparent-box {
    background-color: transparent;
    border: 2px solid currentColor;
}

.system-default {
    background-color: unset;
    color: inherit;
}
Note: There are 140+ named colors in CSS including less common ones like rebeccapurple, chartreuse, and aquamarine.

Color Names

Absolute Units

Absolute units are fixed in size and don't change based on other factors. They're useful for print styles and when you need precise control.

Unit Name Equivalent Use Case
px Pixels 1/96th of 1in Screen design, images
pt Points 1/72nd of 1in Print styles, typography
pc Picas 12pt = 1pc Print design
in Inches 2.54cm = 1in Print media
cm Centimeters 0.3937in = 1cm Print media
mm Millimeters 0.1cm = 1mm Print media
Absolute Unit Comparison:
Pixels (px)
100px

Fixed screen size

Points (pt)
100pt

Print measurements

Centimeters (cm)
5cm

Physical dimensions

/* Using absolute units */
.print-header {
    font-size: 14pt; /* Points for print */
    margin: 1cm; /* Centimeters for physical measurement */
}

.screen-element {
    width: 300px; /* Pixels for screen */
    padding: 20px;
}

.fixed-size {
    height: 2in; /* Inches for precise control */
    width: 3in;
}
Caution: Avoid using absolute units for responsive web design as they don't adapt to different screen sizes or user preferences.

Relative Units

Relative units are calculated based on other lengths, making them ideal for responsive and accessible design.

Unit Relative To Description Use Case
em Parent element's font size 1em = current font size Component scaling
rem Root element's font size 1rem = root font size Global scaling
% Parent element Percentage of parent Layout proportions
ch Width of "0" character 1ch = width of "0" Typography, max widths
ex Height of "x" character 1ex = height of "x" Vertical rhythm
Relative Unit Examples:
EM Units
Parent: 20px
1.5em = 30px
REM Units
Root: 16px
1.5rem = 24px
Percentage
Parent: 200px
50% = 100px
/* Using relative units */
.container {
    font-size: 16px; /* Base size */
}

.heading {
    font-size: 2rem; /* 32px - relative to root */
    margin-bottom: 1em; /* 32px - relative to current font size */
}

.box {
    width: 50%; /* Half of parent width */
    padding: 1.5rem; /* Consistent spacing */
}

.typography {
    max-width: 60ch; /* Optimal line length */
    line-height: 1.6em; /* Relative to font size */
}

Viewport Units

Viewport units are relative to the size of the browser viewport, making them perfect for responsive full-screen layouts.

Unit Relative To Description Use Case
vw Viewport width 1vw = 1% of viewport width Responsive widths
vh Viewport height 1vh = 1% of viewport height Full-height sections
vmin Viewport minimum 1vmin = 1% of smaller dimension Square elements
vmax Viewport maximum 1vmax = 1% of larger dimension Large scale elements
Viewport Unit Examples:
50vw Width
50% of viewport
50vh Height
20% of viewport height
20vmin Square
20vmin
/* Using viewport units */
.hero-section {
    height: 100vh; /* Full viewport height */
    width: 100vw; /* Full viewport width */
}

.responsive-text {
    font-size: 5vw; /* Scales with viewport width */
    margin: 2vh auto; /* Vertical margin relative to height */
}

.square-element {
    width: 50vmin; /* Always square based on smaller dimension */
    height: 50vmin;
}

.large-element {
    width: 80vmax; /* Based on larger dimension */
    height: 60vmin;
}
Viewport Unit Tips:
  • Use vh for full-screen sections
  • Combine vw with clamp() for responsive typography
  • Use vmin for elements that should remain square
  • Be cautious with 100vh on mobile due to browser UI

CSS Color Functions

Modern CSS provides powerful color functions for dynamic color manipulation and theming.

Key Color Functions:
Function Syntax Description Browser Support
hsl() hsl(hue, saturation, lightness) Hue-Saturation-Lightness model All browsers
hsla() hsla(hue, saturation, lightness, alpha) HSL with transparency All browsers
color-mix() color-mix(in colorspace, color1, color2) Mixes two colors Modern browsers
color-contrast() color-contrast(color vs color1, color2) Finds best contrast color Modern browsers
oklch() oklch(lightness chroma hue) Perceptually uniform colors Modern browsers
Color Function Examples:
hsl(200,80%,60%)
color-mix()
oklch(60%,0.15,240)
/* Using color functions */
.primary {
    background-color: hsl(210, 64%, 50%);
}

.light-variant {
    /* Mix with white for lighter version */
    background-color: color-mix(in srgb, hsl(210, 64%, 50%), white 30%);
}

.dark-variant {
    /* Mix with black for darker version */
    background-color: color-mix(in srgb, hsl(210, 64%, 50%), black 20%);
}

.modern-color {
    /* OKLCH for perceptual uniformity */
    background-color: oklch(60% 0.15 240);
}

.auto-contrast {
    color: color-contrast(black vs white, #333, #666);
}
Modern Color Spaces: OKLCH and LCH provide better perceptual uniformity than HSL, meaning color changes feel more consistent to human vision.

Unit Conversion & Calculations

CSS provides the calc() function for mathematical operations and unit conversions.

Unit Conversion Calculator
16px = 1rem
/* Common conversion */
.element {
    width: calc(16px * 1rem / 16px);
}
Common Unit Conversions:
/* Common unit conversions with calc() */
.container {
    /* Convert px to rem (assuming 16px root) */
    padding: calc(20px / 16 * 1rem); /* 1.25rem */
    
    /* Responsive width with limits */
    width: calc(100vw - 40px);
    max-width: 1200px;
    min-width: 320px;
    
    /* Center element with offset */
    margin-left: calc(50% - 150px);
    
    /* Complex calculations */
    font-size: calc(1rem + 0.5vw);
    
    /* Grid gap calculations */
    gap: calc(2rem + 1vw);
}
Calc() Function Tips:
  • Always include spaces around operators: calc(100% - 20px)
  • You can mix different units in calculations
  • Use for responsive typography with clamp()
  • Combine with CSS variables for dynamic layouts

Interactive Colors & Units Demo

Experiment with different color formats and units to see how they affect elements in real-time:

   Color Preview
Generated CSS:
background-color: #4e73df;
50
This is test element
Generated CSS:
width: 50px;

What's Next?

Now that you understand CSS colors and units, you can explore more advanced topics:

  • CSS Box Model: Understand how spacing, borders, and margins work
  • CSS Typography: Learn about fonts, text properties, and web fonts
  • CSS Layout Techniques: Explore Flexbox and CSS Grid for modern layouts
  • CSS Custom Properties: Master CSS variables for maintainable code
  • CSS Functions: Deep dive into calc(), min(), max(), and clamp()