Introduction
CSS gives you multiple ways to express colors and multiple systems of measurement for lengths. Choosing the right color format and unit type affects both visual accuracy and how well a design adapts across screen sizes.
Cricket analogy: Choosing between hex colors and HSL is like choosing between a bowler's raw pace figures and their economy rate, different formats describing the same underlying performance.
Syntax
.box {
color: #ff5733;
background-color: rgb(255, 87, 51);
border-color: hsl(11, 100%, 60%);
opacity: 0.9;
width: 20rem;
padding: 1.5em;
font-size: 1.2vw;
}Explanation
Colors can be written as keywords (red), hexadecimal codes (#ff5733), rgb()/rgba() functions, or hsl()/hsla() functions, where hsl represents hue (0-360 degrees), saturation, and lightness — often the most intuitive for adjusting a color's tone. Units fall into two categories: absolute units like px, which represent a fixed physical size, and relative units like %, em, rem, vw, and vh, which scale based on another value such as the parent element's font size, the root font size, or the viewport dimensions. rem is especially useful because it always references the root (html) element's font size, making layouts easier to scale consistently.
Cricket analogy: hsl's hue-saturation-lightness sliders are like adjusting a pitch report's pace, bounce, and turn independently, more intuitive than memorizing a raw hex code of numbers.
Example
html { font-size: 16px; }
.title {
font-size: 2rem; /* 32px, based on root */
margin-bottom: 1em; /* relative to this element's own font-size */
color: hsla(210, 80%, 45%, 0.85);
}
.hero {
width: 100vw;
height: 50vh;
}Output
The .title element renders at 32px because 2rem multiplies the root's 16px font size. Its bottom margin equals the element's own computed font size (32px), since 1em references the current element. The .hero element stretches to the full width and half the height of the browser viewport, resizing automatically whenever the window changes.
Cricket analogy: The .title's 32px size, doubled from the 16px root, is like a batting average scaled up by a form multiplier, the base stat times a situational factor gives the final number.
Key Takeaways
- Colors can be expressed as keywords, hex, rgb()/rgba(), or hsl()/hsla().
- hsl() is intuitive for tweaking hue, saturation, and lightness independently.
- px is an absolute unit; %, em, rem, vw, and vh are relative units that adapt to context.
- rem scales relative to the root font-size, making it ideal for consistent, scalable typography and spacing.
Practice what you learned
1. Which color function lets you specify hue, saturation, and lightness directly?
2. What does the unit `rem` scale relative to?
3. Which unit represents 1% of the viewport width?
4. What does the fourth value in `rgba(255, 0, 0, 0.5)` control?
Was this page helpful?
You May Also Like
CSS Units for Layout (px, %, rem, em, vh/vw)
How absolute and relative CSS length units differ and when to use px, %, rem, em, and viewport units in layouts.
CSS Custom Properties (Variables)
Learn how to define and reuse values across a stylesheet using CSS custom properties and the var() function.
Responsive Design and Media Queries
Techniques and CSS media queries used to adapt layouts across different screen sizes and devices.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics