CSS: Values and Units of Measurement
Overview
In CSS, property length values are needed to provide layout and presentation control. There are two main types - absolute and relative. The absolute length is not relative to anything else and is typically the same size on most devices. Relative length is dependent on other items including the parent, sibling, or the size of the viewport.
"Every property used in CSS has a value type defining the set of values that are allowed for that
property."
"The numeric type you will come across most frequently is the length (or unit of measurement). For
example, 10px (pixels) or 30em. There are two types of lengths used in CSS — relative and absolute. It's
important to know the difference in order to understand how big things will become." - MDN
Prepare
- Video Demonstration: ▶️ CSS: Units of Measurement| (4:47 mins, Transcript )
- Color values in CSS can be coded by:
- keyword:
antiquewhite
,burlywood
,yellow
,lightyellow
, etc. - hexadecimal RGB (red, green, blue):
#faebd7
where the first two characters are the red value, the next two are the green value, and the last two are the blue value in hexadecimal (base16) format (0 to F). - RGB or RGBA:
rgb(250, 235, 215)
orrgba(250, 235, 215, 1)
wherer
is a red value ranging from 0 to 255,g
is a green value ranging from 0 to 255,b
is a blue value ranging from 0 to 255, and thea
is the alpha value ranging from 0 to 1. - HSLA:
hsla(34, 78%, 91%, 1)
whereh
is the hue (0 to 360),s
is the saturation (0% to 100%),l
is the lightness (0% to 100%), anda
is the alpha value (0 to 1).
- keyword:
"There are many ways to specify color in CSS, some of which are more recently implemented than others. The same color values can be used everywhere in CSS, whether you are specifying text color, background color, or whatever else." - MDN
Activity Instructions
In this activity, answer the following questions about CSS value units/lengths.
- What is the most common absolute length unit for screens?
Help - Scroll down to Absolute length units to find the answer.
Check Your Understanding
px (pixels)
- Which CSS relative length unit is based upon the font size of the
root element?
Check Your Understanding
rem
- Given a hexadecimal (base16) color value of #0e421c,
what value is the Green level?
Check Your Understanding
42
There are two hexadecimal values per color. First red, then green, and then blue. That is why there are six total characters with values in hex (0 to F).
Optional Resources
- CSS Values and Units - MDN
- Color Section - MDN
- Named Colors / Keywords - MDN