Regex for Hex Color Code Validation
Quick Answer: Hex color regex: ^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$. This matches 3-digit shorthand (#fff), 6-digit (#ffffff), and 8-digit with alpha (#ffffffcc). The # prefix is optional in this pattern.
FAQ
What is an 8-digit hex color?
8-digit hex includes alpha transparency. #ff000080 is red at 50% opacity. The last two digits (00-ff) set the alpha channel.
Are hex colors case sensitive?
No. #FF0000, #ff0000, and #Ff0000 are all identical red. Convention is lowercase for CSS, but both are valid everywhere.