Web Development
6 min read
Updated July 2026

How to Use HEX Colors in CSS: Custom Properties, Alpha & Fallbacks

Practical guide to applying hexadecimal colors in CSS stylesheets, CSS custom properties (variables), modern 8-digit alpha channels, and dark-mode switching.

CP
Color Pickers Editorial TeamReviewed for Technical Accuracy

Modern CSS Color Declarations

Hexadecimal color notation is universally supported across every web browser since HTML 1.0. Modern CSS expands hex support to include 8-digit alpha transparency and CSS custom property theming.

1. Standard CSS Properties with Hex Codes

.card {
  color: #0F172A; /* Text color */
  background-color: #FFFFFF; /* Surface */
  border: 1px solid #E2E8F0; /* Border */
  box-shadow: 0 4px 12px #0F172A1A; /* 10% opacity shadow */
}

2. CSS Custom Properties for Dynamic Theming

:root {
  --primary: #3B82F6;
  --primary-hover: #2563EB;
  --bg: #F8FAFC;
}
[data-theme='dark'] {
  --bg: #0F172A;
}

Try Related Color Tools

Related Articles & Guides

Browse All Guides →