๐จ CSS COURSE โ MODULE 3: COLORS & UNITS
๐ File: colors-units.md
๐จ Colors & Units in CSS
๐ Introduction
CSS uses colors and units to define how elements look and how they are sized.
๐ Colors = appearance ๐ Units = sizing & spacing
1. CSS Color Types
๐ฅ HEX Colors
Most common in web design.
h1 {
color: #ff0000;
}
โ Red color
โ Starts with #
๐ฉ RGB Colors
Uses Red, Green, Blue values.
h1 {
color: rgb(255, 0, 0);
}
โ Same red color โ Range: 0 โ 255
๐ฆ RGBA Colors (with transparency)
div {
background-color: rgba(0, 0, 0, 0.5);
}
โ Last value = transparency (0 โ 1)
HSL Colors
Hue, Saturation, Lightness
h1 {
color: hsl(0, 100%, 50%);
}
โ More human-friendly color system
๐ฏ Color Comparison
| Type | Example | Use |
|---|---|---|
| HEX | #ff0000 | common |
| RGB | rgb(255,0,0) | dynamic |
| RGBA | rgba(0,0,0,0.5) | transparency |
| HSL | hsl(0,100%,50%) | modern design |
๐ 2. CSS Units
๐ Absolute Units
๐ px (Pixels)
p {
font-size: 16px;
}
โ Fixed size โ Most commonly used
๐ Relative Units (VERY IMPORTANT)
๐ %
div {
width: 50%;
}
โ Relative to parent
๐ em
p {
font-size: 2em;
}
โ Relative to parent font size
๐ rem (BEST PRACTICE)
p {
font-size: 1.5rem;
}
โ Relative to root (html) โ Used in modern websites
๐ฑ Viewport Units (RESPONSIVE DESIGN)
๐ vw (viewport width)
div {
width: 50vw;
}
โ 50% of screen width
๐ vh (viewport height)
div {
height: 100vh;
}
โ Full screen height
๐ง Important Concept
px โ fixed
% โ parent-based
em โ relative to parent text
rem โ root-based (recommended)
vw/vh โ screen-based
๐งฉ Real Example
<div class="box">
<h1>Hello CSS</h1>
<p>Learning colors and units</p>
</div>
.box {
width: 50%;
background-color: #f0f0f0;
padding: 20px;
}
h1 {
color: rgb(0, 120, 255);
font-size: 2rem;
}
p {
color: rgba(0, 0, 0, 0.7);
font-size: 1rem;
}
โ ๏ธ Common Mistakes
- Mixing px and em randomly โ
- Not understanding rem โ
- Overusing px in responsive design โ
- Using too many color formats randomly โ
๐งช Mini Exercise
Create a page with:
- A heading with HEX color
- A paragraph with RGB color
- A container using % width
- A box using padding in px
- Text using rem