Skip to main content
AllDevToolsHub
Back to all patterns

Hex Color With Optional Alpha (#RGB / #RGBA / #RRGGBB / #RRGGBBAA)

Web & Network

Matches modern CSS hex colors including 3, 4, 6, or 8 hex digits (alpha-channel forms).

/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/

How it works

CSS Color Module Level 4 added 4-digit (#RGBA) and 8-digit (#RRGGBBAA) hex notations. This pattern covers all four standard lengths.

Test Cases

Should Match

  • #FFF
  • #FFFA
  • #FFFFFF
  • #FFFFFF80

Should NOT Match

  • #FF
  • FFFFFF
  • #GGGGGG

Quick Summary

Matches 3, 4, 6, or 8-digit hex colors per CSS Color Module Level 4.