Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / CSS Colors

2,087 people viewed this

CSS Colors

CSS Colors, Units and Values

Learning Objectives

  • Use common CSS color formats.
  • Understand absolute and relative length units.
  • Recognize numeric, percentage, keyword, and functional values.
  • Choose suitable units for responsive designs.

Color Formats

h1 { color: red; }
.hex { color: #3366cc; }
.rgb { color: rgb(51 102 204); }
.rgba { color: rgb(51 102 204 / 50%); }
.hsl { color: hsl(220 60% 50%); }

CSS supports named colors, hexadecimal notation, RGB/RGBA-style alpha syntax, HSL/HSLA-style alpha syntax, and modern color spaces such as Lab, LCH, OKLab and OKLCH in supported browsers.

Length Units

  • px: CSS pixel, an absolute CSS length unit.
  • em: relative to the element's font size for many properties.
  • rem: relative to the root element's font size.
  • %: relative to the relevant property's reference dimension or value.
  • vw and vh: viewport-relative units.
  • svh, lvh, dvh: small, large and dynamic viewport-height units.
  • ch: based on the width of the zero glyph of the font.

Examples

html { font-size: 16px; }
.card { padding: 1rem; }
.card-title { margin-bottom: 1em; }
.container { width: 90%; max-width: 70rem; }
.hero { min-height: 100vh; }
.heading { font-size: clamp(1.5rem, 4vw, 3rem); }

Other CSS Values

.box { width: auto; }
.hidden { display: none; }
.panel { opacity: 0.8; }
.responsive-image { width: 100%; height: auto; }

CSS values can be keywords, numbers, lengths, percentages, colors, functions, and more. Some properties accept unitless numbers, such as line-height and opacity.

Accessibility and Readability

Choose sufficient text/background contrast. Do not rely on color alone to convey information. Use relative sizing where it supports user preferences, and test pages at zoomed text sizes and narrow viewport widths.

Practice

Create a responsive card with a hexadecimal background color, rem-based padding, a percentage-based width, a max-width, and a fluid heading using clamp().

Quiz

  1. Which is a valid hexadecimal color? (A) #3366cc (B) 3366cc# (C) color=blue (D) rgb:blue. Answer: A. Hex colors begin with a hash.
  2. What does rem reference? (A) Root element font size (B) Image width (C) Viewport width only (D) Border width. Answer: A. Rem is relative to the root font size.
  3. What does em commonly reference for font-size? (A) Parent's computed font size (B) Browser history (C) Screen resolution (D) HTML file size. Answer: A. For font-size, em is relative to the inherited font size.
  4. Which unit is viewport-width based? (A) vw (B) rem (C) cm only (D) ch. Answer: A. One vw represents one percent of the viewport width.
  5. Which function can clamp a value between minimum and maximum? (A) clamp() (B) color() only (C) border() (D) select(). Answer: A. Clamp provides a lower bound, preferred value, and upper bound.
  6. Which property sets text color? (A) color (B) font-weight (C) padding (D) display. Answer: A. Color sets foreground color.
  7. What is alpha used for in a color? (A) Transparency/opacity component (B) Font size (C) Element ID (D) Layout direction. Answer: A. Alpha controls the color's opacity component.
  8. Which property often accepts a unitless number? (A) opacity (B) width in pixels (C) margin in pixels (D) border-width in pixels. Answer: A. Opacity accepts values from 0 to 1.
  9. What is a useful responsive image rule? (A) max-width: 100%; height: auto (B) width: 2000px always (C) display: none always (D) position: fixed always. Answer: A. It allows the image to scale within its container.
  10. Why check color contrast? (A) Improve readability and accessibility (B) Increase HTML file size (C) Change server ports (D) Create selectors. Answer: A. Adequate contrast helps users perceive content.

Key Takeaways

  • CSS supports several color notations.
  • Choose units based on context and responsive requirements.
  • Functions such as clamp() enable fluid sizing.
  • Test readability, contrast, and zoom behavior.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars