Skip to content

Colors and Size Units ​

Ways to Express Colors ​

In CSS, colors can be expressed in several ways besides names.

FormatExampleCharacteristics
Color namered, skyblueIntuitive, but only a limited set of colors
HEX code#ff0000Red, green and blue as two hexadecimal digits each (RRGGBB)
RGBrgb(255, 0, 0)Red, green and blue as numbers from 0 to 255
RGBArgba(255, 0, 0, 0.5)RGB plus opacity (alpha)

How to read a HEX code: #ffffff splits into three pairs: ff (red) + ff (green) + ff (blue). A pair of 00 means none of that color, and ff means the maximum. #ffffff has all three colors at maximum, so it's white; #000000 has all of them at 0, so it's black.

Opacity (Alpha): the last number in rgba() is a value between 0 (fully transparent) and 1 (fully opaque). It's often used when you want only the background color to be slightly see-through.

Units for Expressing Size ​

UnitMeaningAnalogy
pxA fixed number of pixelsAn absolute length measured on paper with a ruler
%A ratio relative to the parent elementA relative length like "half the parent's size"
emA multiple of the parent's (or its own) font size"How many times the current font size"
remA multiple of the root (html) font sizeA ruler based on the whole page

px is easy to work with but never changes even when the screen size changes, while %/em/rem change size flexibly depending on the situation. When building responsive designs, you end up using relative units a lot.

Summary ​

ConceptOne-line summary
Color unitsExpressed with HEX/RGB/RGBA; the alpha value even controls transparency
Size unitspx is an absolute value; %/em/rem are relative values that change with their reference

Reference: personal Notion study notes