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.
What are CSS values?
A CSS declaration combines a property with a value. The value tells the browser what result you want.
h1 {
color: #0b7cff;
font-size: 2rem;
margin-bottom: 16px;
}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.
.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.
.heading {
color: #0b1f3a;
}
.button {
background-color: #0b7cff;
color: #ffffff;
}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.
.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.
.badge {
background-color: hsl(212 100% 52%);
}
.badge-soft {
background-color: hsl(212 100% 95%);
}Color and readability
Good color choice is not only about appearance. Text and background need enough contrast so content remains readable.
.notice {
background-color: #eaf3ff;
color: #0b2a4f;
}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.
.card {
border: 1px solid #dbe4ee;
padding: 20px;
border-radius: 12px;
}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.
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.
.button {
font-size: 1rem;
padding: 0.75em 1.25em;
}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.
.content {
width: 80%;
}
.progress-bar {
width: 65%;
}For common width usage, the percentage is based on the containing block's available width.
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.
.hero {
min-height: 60vh;
}
.hero h1 {
font-size: 5vw;
}vw font sizes can become too small or too large. Responsive typography often works better with limits such as clamp().Other useful relative units
.text-column {
max-width: 65ch;
}
.square {
width: 20vmin;
height: 20vmin;
}chis based on the width of the zero glyph and is useful for readable text line lengths.vminuses the smaller viewport dimension.vmaxuses 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.
.layout {
width: calc(100% - 2rem);
}
.hero-title {
font-size: clamp(2rem, 5vw, 4rem);
}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.
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.
Common beginner mistakes
- Zero ke saath unnecessary unit likhna, jaise
margin: 0px. - Har spacing aur font size ko fixed pixels me lock kar dena.
emnesting 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.
- Heading color ko hex me set karo.
- Card background ko RGB ya HSL me set karo.
- Secondary text ke liye readable contrasting color choose karo.
- Card padding ko
remme do. - Button padding ko
emme try karo. - Card width ko percentage me set karo aur
max-widthadd karo. - Heading font size ko
clamp()se responsive banao. - Text container ko
chunit se readable width do. - Browser resize karke observe karo kaunsi values fixed hain aur kaunsi scale karti hain.