Short hex color
Matches 3-digit CSS hex colors such as #fff.
Suggested pattern
^#[0-9A-Fa-f]{3}$
What it matches
#fff#09A
Common limitations
- Only accepts 3-digit hex colors.
- Does not accept 6-digit colors.
Visual regex workbench
Build, import, explain, test and share regex visually.
Regex guide
Hex color validation is common in CSS tools, design systems, theme editors and form inputs. This guide provides practical regex examples for short, long and alpha hex colors.
Matches 3-digit CSS hex colors such as #fff.
^#[0-9A-Fa-f]{3}$
#fff#09AMatches 6-digit CSS hex colors such as #ffffff.
^#[0-9A-Fa-f]{6}$
#ffffff#1A2b3CMatches both 3-digit and 6-digit CSS hex colors.
^#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$
#fff#ffffffMatches 4-digit and 8-digit CSS hex colors with alpha transparency.
^#(?:[0-9A-Fa-f]{4}|[0-9A-Fa-f]{8})$
#ffff#ffffff80Matches 3, 4, 6 or 8-digit CSS hex colors.
^#(?:[0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$
#fff#ffff#ffffff#ffffff80Matches hex color values without the leading # symbol.
^(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$
fffffffffA hex color is a CSS color value written with hexadecimal digits, usually starting with a # symbol.
Common formats include #fff, #ffffff and newer alpha formats such as #ffffff80.
Short hex colors use three digits, while long hex colors use six digits.
For example, #fff is shorthand for #ffffff and #000 is shorthand for #000000.
Modern CSS also supports 4-digit and 8-digit hex colors where the last digit pair represents alpha transparency.
This allows colors such as #ffffff80 to represent white with partial opacity.
A common mistake is accepting any string of hexadecimal characters without checking the exact length.
Another mistake is forgetting whether the # symbol should be required or optional.
Hex color regex patterns are useful in theme editors, CSS generators, design tools, admin panels and form validation.
They provide a quick way to validate color inputs before saving them or applying them to a user interface.