CSS (Cascading Style Sheets) is the language used to style and visually format web pages.
It controls how HTML elements look on screen — including colors, fonts, spacing, layout, and responsiveness.
What is CSS — Detailed Definition
CSS (Cascading Style Sheets) is a stylesheet language that describes the presentation and layout of HTML documents. It allows developers to separate content (HTML) from design (CSS), making websites visually appealing, consistent, and responsive across devices.
👉 In simple words:
HTML = structure
CSS = appearance
Without CSS → websites look plain
With CSS → websites look professional
Example — HTML without CSS
<h1>Hello World</h1>
<p>This is my website</p>
Output:
Hello World
This is my website
Example — HTML with CSS
<h1 style="color: blue; font-size: 40px;">Hello World</h1>
<p style="color: gray;">This is my website</p>
Now the text has styling.
Hello World
This is my website
CSS Syntax — Detailed Definition
CSS syntax defines how styles are written and applied to HTML elements.
Each CSS rule consists of a selector and a declaration block.
Structure
selector {
property: value;
}
Parts Explained
- Selector → target HTML element
- Property → style aspect to change
- Value → how it should appear
Example
h1 {
color: red;
font-size: 40px;
}
Meaning:
- All
<h1>elements → red color - Font size → 40px
Multiple Declarations Example
p {
color: #444;
font-size: 18px;
line-height: 1.6;
}
CSS Comments — Detailed Definition
CSS comments are non-executed notes inside CSS code used to explain styles, organize sections, or document decisions. Browsers ignore comments completely.
Syntax
/* comment text */
Example
/* Main page background */
body {
background-color: #f4f6f8;
}
/* Heading style */
h1 {
color: #2c3e50;
}
👉 Comments help with:
- code readability
- teamwork
- maintenance
CSS Colors — Detailed Definition
CSS colors define the visual color appearance of elements on a webpage, including:
- Text color
- Background color
- Borders
- Shadows
- Gradients
- SVG graphics
CSS supports multiple color formats so developers can choose the most suitable representation for design, readability, and dynamic styling.
1. Named Colors
Definition
Named colors are predefined color keywords provided by CSS. Instead of numeric values, you simply write the color name.
CSS supports more than 140 standard color names such as red, blue, green, black, white, coral, tomato.
They are the simplest and most readable way to apply colors.
Example
h1 {
color: red;
}
Explanation
red is a predefined CSS color keyword.
The browser internally converts it to the RGB value (255, 0, 0).
When to Use
- Quick styling
- Prototypes
- Simple designs
- Readable CSS
2. HEX Colors
Definition
HEX colors represent colors using hexadecimal (base-16) values that define RGB intensity.
Format:
#RRGGBB
Where:
- RR → Red (00–FF)
- GG → Green (00–FF)
- BB → Blue (00–FF)
Range:
00 = 0
FF = 255
Example
h1 {
color: #ff0000;
}
Explanation
- ff = 255 red
- 00 = 0 green
- 00 = 0 blue
This produces pure red.
Short HEX
If pairs repeat, you can shorten:
#ff0000 → #f00
#ffffff → #fff
When to Use
- Web design systems
- Brand colors
- Consistent theming
- UI development
3. RGB Colors
Definition
RGB colors define color using numeric intensity of Red, Green, and Blue channels.
Format:
rgb(red, green, blue)
Range:
0–255
Example
h1 {
color: rgb(255, 0, 0);
}
Explanation
- Red = 255
- Green = 0
- Blue = 0
This produces pure red.
Advantage
RGB allows dynamic manipulation in JavaScript:
element.style.color = "rgb(" + r + "," + g + "," + b + ")";
When to Use
- Animations
- JavaScript color changes
- Dynamic UI
- Canvas and SVG graphics
4. RGBA Colors (Transparency)
Definition
RGBA extends RGB by adding an alpha channel that controls transparency (opacity).
Format:
rgba(red, green, blue, alpha)
Alpha range:
0 → fully transparent
1 → fully visible
Example
h1 {
color: rgba(255, 0, 0, 0.5);
}
Explanation
- Red = 255
- Green = 0
- Blue = 0
- Opacity = 0.5 (50% transparent)
Real Use Cases
- Glassmorphism UI
- Overlays
- Shadows
- Hover effects
Example:
.card {
background: rgba(255, 255, 255, 0.2);
}
5. HSL Colors
Definition
HSL represents color using human-perception attributes:
- Hue (color type)
- Saturation (intensity)
- Lightness (brightness)
Format:
hsl(hue, saturation, lightness)
Components
Hue (0–360°)
Color wheel angle:
- 0 → Red
- 120 → Green
- 240 → Blue
Saturation (0–100%)
Color intensity:
- 0% → Gray
- 100% → Pure color
Lightness (0–100%)
Brightness:
- 0% → Black
- 50% → Normal
- 100% → White
Example
h1 {
color: hsl(0, 100%, 50%);
}
Explanation
- Hue = 0 → Red
- Saturation = 100% → Full color
- Lightness = 50% → Normal brightness
This produces pure red.
Why HSL is Powerful
HSL makes color adjustments easier.
Increase brightness:
h1 {
color: hsl(0, 100%, 70%);
}
Change color tone:
h1 {
color: hsl(200, 100%, 50%);
}
Real Example — Button Colors
CSS
button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
}
Explanation
- #007bff → blue background
- white → text color
- border: none → no border
Comparison Table
| Format | Transparency | Human Friendly | Common Use |
|---|---|---|---|
| Named | No | Yes | Simple CSS |
| HEX | No | Moderate | UI design |
| RGB | No | Moderate | JavaScript and graphics |
| RGBA | Yes | Moderate | Overlays |
| HSL | No | Yes | Theming |
Key Takeaways
- CSS supports multiple color systems.
- All formats represent the same colors.
- Choose format based on readability and use case.
- RGBA and HSL are widely used in modern UI design.
CSS Units – Detailed Definition with Real-World Examples
CSS Units Kya Hote Hain?
CSS me jab hum kisi element ka size, spacing, ya position define karte hain, to hume measurement ki zarurat hoti hai.
Isi measurement ko CSS Unit kehte hain.
👉 Simple definition:
CSS Unit = CSS property ki value ka measurement system
Example:
p {
margin: 20px;
}
Yahan:
20= valuepx= unit
Matlab paragraph ke bahar 20-pixel ka space hoga.
CSS Units ke Types
CSS units 2 main categories me hote hain:
- Absolute Units (fixed size)
- Relative Units (context ke hisab se change)
1. Absolute Units – Detailed Definition
Absolute units wo hote hain jo screen ya parent size se independent hote hain.
Ye fixed measurement dete hain.
Common absolute units:
- px (pixel)
- pt (point)
- in (inch)
- cm (centimeter)
px (Pixel) – Detailed Definition
Pixel screen ka sabse chhota visible dot hota hai.
CSS me:
1px = 1/96 inch
👉 px ka size fixed hota hai.
Real-World Example 1 (Website Button)
.button {
width: 120px;
height: 40px;
background: blue;
color: white;
}
Real world me:
- Button ka size har device par same dikhega
- Mobile, laptop, desktop sab me same width
Use case:
- Buttons
- Icons
- Borders
Real-World Example 2 (Profile Image)
.profile {
width: 150px;
height: 150px;
border-radius: 50%;
}
Real life:
- Instagram-style circular profile image
- Exact circle required → px best
2. Relative Units – Detailed Definition
Relative units wo hote hain jo parent, font size, ya screen size ke according change hote hain.
👉 Responsive design ke liye important
Relative units types:
- em
- rem
- %
- vw
- vh
em – Detailed Definition
em current element ke font size ke relative hota hai.
Rule:
1em = current font size
Real-World Example 1 (Card Padding)
.card {
font-size: 16px;
padding: 2em;
}
Calculation:
1em = 16px
2em = 32px
Matlab:
- Text bada hoga → padding bhi badhegi
- Design proportional rahega
Real use:
- Cards
- Buttons
- Components
Real-World Example 2 (Button with scalable spacing)
.button {
font-size: 18px;
padding: 0.5em 1em;
}
If font increases → button spacing bhi increase
rem – Detailed Definition
rem root element (html) ke font size ke relative hota hai.
Default:
html = 16px
1rem = 16px
Real-World Example 1 (Website Typography)
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
p {
font-size: 1rem;
}
Result:
- h1 = 32px
- p = 16px
Agar root font change:
html { font-size: 18px; }
Sab text automatically scale.
Real-World Example 2 (Spacing System)
.section {
margin: 3rem 0;
}
Spacing pure website me consistent.
% (Percentage) – Detailed Definition
% parent element ke size ke relative hota hai.
Rule:
child size = parent size ka %
Real-World Example 1 (Responsive Container)
.container {
width: 80%;
}
If screen width:
- 1200px → 960px
- 800px → 640px
Layout automatically adjust.
Real-World Example 2 (Two Columns)
.left {
width: 50%;
float: left;
}
.right {
width: 50%;
float: right;
}
Real life:
- 2 equal columns
- Screen size change → columns adapt
vw – Detailed Definition
vw = viewport width ka percentage
1vw = screen width ka 1%
Real-World Example 1 (Full width section)
.banner {
width: 100vw;
}
Always full screen width.
Real-World Example 2 (Responsive Heading)
h1 {
font-size: 5vw;
}
Screen:
- 1200px → 60px
- 600px → 30px
Text automatically scale.
vh – Detailed Definition
vh = viewport height ka %
1vh = screen height ka 1%
Real-World Example 1 (Hero Section)
.hero {
height: 100vh;
}
Full screen section.
Real-World Example 2 (Center Layout)
.center-box {
margin-top: 30vh;
}
Element vertical center ke paas.
Unitless Values – Detailed Definition
Kuch CSS properties me unit nahi lagta.
Example:
p {
line-height: 1.6;
}
Meaning:
line height = 1.6 × font size
Real-World Combined Example (Complete Website Section)
html {
font-size: 16px;
}
.container {
width: 90%;
max-width: 1100px;
margin: 3rem auto;
}
.card {
padding: 2rem;
margin-bottom: 2rem;
border-radius: 10px;
}
.card h2 {
font-size: 1.5rem;
}
.card p {
font-size: 1rem;
line-height: 1.6;
}
.button {
padding: 0.6em 1.2em;
font-size: 1rem;
}
Is Example Me Units Ka Real Use
| Unit | Where | Why |
|---|---|---|
| % | container width | responsive |
| px | max-width | limit |
| rem | spacing | scalable |
| em | button padding | proportional |
| unitless | line-height | readable |
Best Practice (Real Web Design Rule)
Modern websites use:
- rem → fonts & spacing
- % → layout
- vw/vh → hero & fluid text
- px → borders/icons
Final Easy Summary
CSS units decide size behavior.
- px → fixed
- rem → root based
- em → element based
- % → parent based
- vw/vh → screen based
Responsive design = mix of units.