CSS Colors · Astro Tech Blog

Colors

CSS offers many ways to represent colors:

Demo: Color Formats
HTML
<div class="named">Named: red</div>
<div class="hex">HEX: #6366f1</div>
<div class="rgb">RGB: rgb(5, 150, 105)</div>
<div class="rgba">RGBA: rgba(239,68,68,0.7)</div>
<div class="hsl">HSL: hsl(271, 81%, 56%)</div>
CSS
div { padding: 8px 16px; margin: 4px 0; border-radius: 6px; color: white; font-weight: bold; }
.named { background: red; }
.hex { background: #6366f1; }
.rgb { background: rgb(5, 150, 105); }
.rgba { background: rgba(239, 68, 68, 0.7); }
.hsl { background: hsl(271, 81%, 56%); }
Live Output Window

Color Formats

FormatExampleNotes
Namedred, blue, tomato148 named colors
HEX#ff0000, #f006 or 3 digit hex
RGBrgb(255, 0, 0)0–255 each channel
RGBArgba(255, 0, 0, 0.5)RGB + alpha (transparency)
HSLhsl(0, 100%, 50%)Hue, Saturation, Lightness
HSLAhsla(0, 100%, 50%, 0.5)HSL + alpha

Opacity

You can also use the opacity property to make an entire element transparent:

Demo: Opacity
HTML
<div class="opaque">opacity: 1</div>
<div class="semi">opacity: 0.6</div>
<div class="ghost">opacity: 0.3</div>
CSS
div { background: #6366f1; color: white; padding: 8px 16px; margin: 4px 0; border-radius: 6px; }
.semi { opacity: 0.6; }
.ghost { opacity: 0.3; }
Live Output Window