LearningCSS TutorialColors, Units & Values
CHAPTER 04 · BEGINNER

CSS Colors, Units & Values

CSS me sirf property likhna enough nahi hota—sahi value aur unit choose karna bhi important hai. Is chapter me colors, fixed vs relative units, percentages aur viewport units ko practical way me samjhenge.

English + Hinglish20 min readPractice included

What are CSS values?

A CSS declaration combines a property with a value. The value tells the browser what result you want.

values.cssCSS
h1 {
  color: #0b7cff;
  font-size: 2rem;
  margin-bottom: 16px;
}
Hinglish Explanation

color, font-size aur margin-bottom properties hain. #0b7cff, 2rem aur 16px unki values hain.

Keyword values

Some CSS values are words defined by CSS, such as color names or special keywords.

keywords.cssCSS
.message {
  color: navy;
  background-color: transparent;
}

.icon {
  color: currentColor;
}

transparent means fully transparent color. currentColor uses the element's computed color value and can be useful for borders, icons and outlines.

Hex colors

Hexadecimal colors start with # and are commonly written with six hexadecimal digits.

hex.cssCSS
.heading {
  color: #0b1f3a;
}

.button {
  background-color: #0b7cff;
  color: #ffffff;
}
Simple idea

Hex color me red, green aur blue channels encode hote hain. Beginner level par exact conversion yaad karna zaroori nahi; practical color picker aur design tokens ke through values use karoge.

RGB and alpha

RGB expresses a color using red, green and blue channels. An alpha value controls transparency.

rgb.cssCSS
.title {
  color: rgb(11, 124, 255);
}

.overlay {
  background-color: rgb(15 23 42 / 0.7);
}

The alpha value 0.7 makes the background partially transparent.

HSL colors

HSL describes color using hue, saturation and lightness. It can feel intuitive when creating lighter or darker variations.

hsl.cssCSS
.badge {
  background-color: hsl(212 100% 52%);
}

.badge-soft {
  background-color: hsl(212 100% 95%);
}
Tip: Project me ek hi color format use karna mandatory nahi hai, but consistency team code ko easier to read banati hai.

Color and readability

Good color choice is not only about appearance. Text and background need enough contrast so content remains readable.

readable.cssCSS
.notice {
  background-color: #eaf3ff;
  color: #0b2a4f;
}
Accessibility: Sirf color se meaning communicate mat karo. Error state ke saath text, icon ya clear label bhi do.

Absolute length: px

px is a CSS pixel unit. It is useful when you need a predictable small dimension such as borders, icon sizes or exact spacing values.

pixels.cssCSS
.card {
  border: 1px solid #dbe4ee;
  padding: 20px;
  border-radius: 12px;
}
Hinglish Explanation

px easy aur direct unit hai, lekin har cheez ko fixed pixels me lock karna responsive design ko difficult bana sakta hai. Context ke hisaab se relative units bhi use karo.

rem unit

rem is relative to the root element's font size. It is commonly useful for typography and consistent spacing systems.

rem.cssCSS
html {
  font-size: 16px;
}

h1 {
  font-size: 2rem;
}

.card {
  padding: 1.5rem;
}

With a 16px root font size, 2rem computes to 32px and 1.5rem computes to 24px.

em unit

em is relative to the relevant font size for the property. For font-size, it relates to the parent's computed font size; for many other properties, it relates to the element's own font size.

em.cssCSS
.button {
  font-size: 1rem;
  padding: 0.75em 1.25em;
}
Why useful?

Button ka padding em me rakhne par font size change hone ke saath spacing proportionally scale ho sakti hai.

Percentage values

Percentages are relative values, but what they are relative to depends on the property.

percent.cssCSS
.content {
  width: 80%;
}

.progress-bar {
  width: 65%;
}

For common width usage, the percentage is based on the containing block's available width.

Remember: Percentage ka reference har property me same nahi hota. Jab behavior confusing lage, property documentation check karo.

Viewport units

vw and vh are based on viewport width and height. 1vw is 1% of viewport width, while 1vh is 1% of viewport height.

viewport.cssCSS
.hero {
  min-height: 60vh;
}

.hero h1 {
  font-size: 5vw;
}
Use carefully: Pure vw font sizes can become too small or too large. Responsive typography often works better with limits such as clamp().

Other useful relative units

relative-units.cssCSS
.text-column {
  max-width: 65ch;
}

.square {
  width: 20vmin;
  height: 20vmin;
}
  • ch is based on the width of the zero glyph and is useful for readable text line lengths.
  • vmin uses the smaller viewport dimension.
  • vmax uses the larger viewport dimension.

calc() and clamp()

CSS functions can combine or limit values. calc() performs calculations, while clamp() sets a minimum, preferred and maximum value.

functions.cssCSS
.layout {
  width: calc(100% - 2rem);
}

.hero-title {
  font-size: clamp(2rem, 5vw, 4rem);
}
Hinglish Explanation

clamp(2rem, 5vw, 4rem) ka matlab: font size 2rem se chhota nahi hoga, responsive preferred value 5vw hogi, aur 4rem se bada nahi hoga.

Unitless values

Not every numeric CSS value needs a unit. Some properties accept or commonly use unitless numbers.

unitless.cssCSS
body {
  line-height: 1.6;
}

.card {
  opacity: 0.9;
}

A unitless line-height scales with the element's font size and is often convenient for readable typography.

How to choose a unit

  • px: borders, icons, small fixed details and exact values.
  • rem: scalable typography and consistent global spacing.
  • em: component-relative spacing that should scale with local text.
  • %: sizes relative to a containing context.
  • vw/vh: viewport-related layout or responsive effects.
  • ch: readable text width.
  • clamp(): bounded responsive sizing.
No single “best” unit: Good CSS me units mix hote hain. Goal context ke according predictable aur responsive behavior choose karna hai.

Common beginner mistakes

  • Zero ke saath unnecessary unit likhna, jaise margin: 0px.
  • Har spacing aur font size ko fixed pixels me lock kar dena.
  • em nesting se unexpected scaling ignore karna.
  • Percentage ka reference context samjhe bina use karna.
  • Text aur background me poor contrast rakhna.
  • Viewport font sizes ko min/max limit ke bina use karna.
  • Color ko hi error/success ka only signal banana.

Practice Task

Ek “Course Card” component banao aur colors + units experiment karo.

  1. Heading color ko hex me set karo.
  2. Card background ko RGB ya HSL me set karo.
  3. Secondary text ke liye readable contrasting color choose karo.
  4. Card padding ko rem me do.
  5. Button padding ko em me try karo.
  6. Card width ko percentage me set karo aur max-width add karo.
  7. Heading font size ko clamp() se responsive banao.
  8. Text container ko ch unit se readable width do.
  9. Browser resize karke observe karo kaunsi values fixed hain aur kaunsi scale karti hain.