𝗝.𝗣. 𝗠𝗢𝗥𝗚𝗔𝗡 𝗙𝗥𝗘𝗘 𝗩𝗜𝗥𝗧𝗨𝗔𝗟 𝗣𝗥𝗢𝗚𝗥𝗔𝗠𝗦 𝟮𝟬𝟮𝟲! 🎓
Explore J.P. Morgan's FREE Virtual Job Simulation Programs
✅ 100% FREE Virtual Learning
✅ Work on Practical, Industry-Based Tasks
✅ Learn at Your Own Pace
✅ Build Valuable Professional Skills
🎯 Perfect For:- College Students | Freshers | Graduates | Job Seekers
🔗 𝗟𝗶𝗻𝗸 👇:-
https://pdlink.in/3X8KJmE
⚡ Learn New Skills • Gain Practical Exposure • Boost Your Career!
Explore J.P. Morgan's FREE Virtual Job Simulation Programs
✅ 100% FREE Virtual Learning
✅ Work on Practical, Industry-Based Tasks
✅ Learn at Your Own Pace
✅ Build Valuable Professional Skills
🎯 Perfect For:- College Students | Freshers | Graduates | Job Seekers
🔗 𝗟𝗶𝗻𝗸 👇:-
https://pdlink.in/3X8KJmE
⚡ Learn New Skills • Gain Practical Exposure • Boost Your Career!
❤5
🚀 𝗗𝗮𝘁𝗮 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝘂𝗿𝘀𝗲 📊
Learn Excel, SQL, Python & Power BI Live Online Data Analytics Program
💰 Highest Salary: ₹12.6 LPA
📈 Average Salary: ₹5.7 LPA
🏢 500+ Partner Companies
𝗔𝗽𝗽𝗹𝘆 𝗡𝗼𝘄👇 :-
https://pdlink.in/45vk5ph
🔥 Turn your interest in data into practical, job-ready skills!
Learn Excel, SQL, Python & Power BI Live Online Data Analytics Program
💰 Highest Salary: ₹12.6 LPA
📈 Average Salary: ₹5.7 LPA
🏢 500+ Partner Companies
𝗔𝗽𝗽𝗹𝘆 𝗡𝗼𝘄👇 :-
https://pdlink.in/45vk5ph
🔥 Turn your interest in data into practical, job-ready skills!
🌐 Web Development Roadmap, Part 27
CSS Units (px, %, em, rem, vh, vw and fr)
CSS units define the size of elements, text, spacing, widths, heights, and layouts.
For example, when you write:
h1 {
font-size: 32px;
}
The px defines the size of the heading.
CSS units are divided into two main categories:
• Absolute units
• Relative units
1. Pixels (px)
Pixels are commonly used to define fixed sizes.
.box {
width: 300px;
height: 200px;
padding: 20px;
}
Here:
• Width = 300 pixels
• Height = 200 pixels
• Padding = 20 pixels
Use pixels when you need a specific size.
2. Percentage (%)
Percentage defines a size relative to the parent element's corresponding dimension.
<div class="parent">
<div class="child">Hello</div>
</div>
`
.parent {
width: 600px;
}
.child {
width: 50%;
}
The child width will be 300px because it is 50% of its parent's 600px width.
3. EM (em)
For font size, it is calculated relative to the inherited font size of the parent.
.parent {
font-size: 20px;
}
.child {
font-size: 2em;
}
The child font size becomes 40px.
Calculation:
20px × 2 = 40px
Note: For properties such as padding and margin,
4. REM (rem)
It is calculated relative to the font size of the root HTML element.
By default, browsers generally use 16px as the root font size.
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
16px × 2 = 32px
So the heading will have a font size of 32px.
Difference between em and rem:
•
•
5. Viewport Height (vh)
1vh = 1% of viewport height.
.hero {
height: 100vh;
}
This makes the element as tall as the viewport.
Useful for:
• Full-screen sections
• Landing pages
• Hero sections
6. Viewport Width (vw)
1vw = 1% of viewport width.
.container {
width: 80vw;
}
This makes the container 80% of the viewport width.
• Responsive layouts
• Full-width sections
• Flexible sizing
7. FR Unit
It is mainly used with CSS Grid to divide available space into fractions.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
This creates three equal-width columns.
Another example:
.container {
display: grid;
grid-template-columns: 2fr 1fr;
}
The first column gets twice the available space of the second column.
8. Other CSS Units
Some additional units you should know:
•
•
•
•
•
•
•
•
•
9. Practical Example
Let's create a responsive card using different CSS units.
HTML:
<div class="card">
<h2>Learn Web Development</h2>
<p>Start learning HTML, CSS and JavaScript.</p>
</div>
CSS:
.card {
width: 80%;
max-width: 500px;
padding: 2rem;
margin: 5vh auto;
background-color: lightblue;
border-radius: 1rem;
}
.card h2 {
font-size: 2rem;
}
.card p {
font-size: 1rem;
}
`
This example combines percentages, pixels, rem, and vh to create a flexible layout.
10. Quick Comparison
• px: Meaning Pixels · Common usage Fixed sizing
• %: Meaning Percentage · Common usage Relative sizing
• em: Meaning Relative to font size · Common usage Text and spacing
CSS Units (px, %, em, rem, vh, vw and fr)
CSS units define the size of elements, text, spacing, widths, heights, and layouts.
For example, when you write:
h1 {
font-size: 32px;
}
The px defines the size of the heading.
CSS units are divided into two main categories:
• Absolute units
• Relative units
1. Pixels (px)
Pixels are commonly used to define fixed sizes.
.box {
width: 300px;
height: 200px;
padding: 20px;
}
Here:
• Width = 300 pixels
• Height = 200 pixels
• Padding = 20 pixels
Use pixels when you need a specific size.
2. Percentage (%)
Percentage defines a size relative to the parent element's corresponding dimension.
<div class="parent">
<div class="child">Hello</div>
</div>
`
.parent {
width: 600px;
}
.child {
width: 50%;
}
The child width will be 300px because it is 50% of its parent's 600px width.
3. EM (em)
em is a relative unit.For font size, it is calculated relative to the inherited font size of the parent.
.parent {
font-size: 20px;
}
.child {
font-size: 2em;
}
The child font size becomes 40px.
Calculation:
20px × 2 = 40px
Note: For properties such as padding and margin,
em is based on the font size of the element itself.4. REM (rem)
rem stands for Root EM.It is calculated relative to the font size of the root HTML element.
By default, browsers generally use 16px as the root font size.
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
16px × 2 = 32px
So the heading will have a font size of 32px.
Difference between em and rem:
•
em depends on the relevant element's inherited or own font size, depending on the property.•
rem depends on the root HTML font size.5. Viewport Height (vh)
vh represents a percentage of the viewport height.1vh = 1% of viewport height.
.hero {
height: 100vh;
}
This makes the element as tall as the viewport.
Useful for:
• Full-screen sections
• Landing pages
• Hero sections
6. Viewport Width (vw)
vw represents a percentage of the viewport width.1vw = 1% of viewport width.
.container {
width: 80vw;
}
This makes the container 80% of the viewport width.
• Responsive layouts
• Full-width sections
• Flexible sizing
7. FR Unit
fr stands for fractional unit.It is mainly used with CSS Grid to divide available space into fractions.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
This creates three equal-width columns.
Another example:
.container {
display: grid;
grid-template-columns: 2fr 1fr;
}
The first column gets twice the available space of the second column.
8. Other CSS Units
Some additional units you should know:
•
cm, Centimeters•
mm, Millimeters•
in, Inches•
pt, Points•
pc, Picas•
vmin, 1% of the smaller viewport dimension•
vmax, 1% of the larger viewport dimension•
ch, Based on the width of the "0" character•
ex, Based on the x-height of the font9. Practical Example
Let's create a responsive card using different CSS units.
HTML:
<div class="card">
<h2>Learn Web Development</h2>
<p>Start learning HTML, CSS and JavaScript.</p>
</div>
CSS:
.card {
width: 80%;
max-width: 500px;
padding: 2rem;
margin: 5vh auto;
background-color: lightblue;
border-radius: 1rem;
}
.card h2 {
font-size: 2rem;
}
.card p {
font-size: 1rem;
}
`
This example combines percentages, pixels, rem, and vh to create a flexible layout.
10. Quick Comparison
• px: Meaning Pixels · Common usage Fixed sizing
• %: Meaning Percentage · Common usage Relative sizing
• em: Meaning Relative to font size · Common usage Text and spacing
❤4
- rem: Meaning Relative to root font size · Common usage Consistent typography
- vh: Meaning Viewport height · Common usage Full-screen sections
- vw: Meaning Viewport width · Common usage Responsive widths
- fr: Meaning Fraction · Common usage CSS Grid layouts
Key Takeaways
- Use
for fixed dimensions.
- Use
for relative sizing.
- Use
and
for scalable typography and spacing.
- Use
and
for viewport-based sizing.
- Use
to divide available space in CSS Grid.
Understanding CSS units is essential for creating responsive websites that work across mobile phones, tablets, and desktops.
Double Tap ❤️ For More
- vh: Meaning Viewport height · Common usage Full-screen sections
- vw: Meaning Viewport width · Common usage Responsive widths
- fr: Meaning Fraction · Common usage CSS Grid layouts
Key Takeaways
- Use
px
for fixed dimensions.
- Use
%
for relative sizing.
- Use
em
and
rem
for scalable typography and spacing.
- Use
vh
and
vw
for viewport-based sizing.
- Use
fr
to divide available space in CSS Grid.
Understanding CSS units is essential for creating responsive websites that work across mobile phones, tablets, and desktops.
Double Tap ❤️ For More
❤13
❤2
What does 50% represent in CSS?
Anonymous Quiz
56%
A) Half of the parent element's corresponding size
16%
B) 50 pixels
23%
C) Half of the screen height
5%
D) Fixed width
🤔2
What does rem stand for?
Anonymous Quiz
21%
A) Relative Element
23%
B) Root EM
16%
C) Responsive EM
41%
D) Root Element Margin
By default, what is the root font size in most browsers?
Anonymous Quiz
14%
A) 10px
41%
B) 12px
39%
C) 16px
5%
D) 20px
What does 1vh represent?
Anonymous Quiz
12%
A) 1% of viewport width
75%
B) 1% of viewport height
8%
C) 1 pixel
6%
D) 1% of parent width
Which unit is commonly used with CSS Grid to divide available space?
Anonymous Quiz
21%
A) px
16%
B) em
46%
C) fr
16%
D) rem
If the parent font size is 20px, what is 2em for a child’s font size?
Anonymous Quiz
18%
A) 20px
14%
B) 30px
63%
C) 40px
5%
D) 60px
If the root font size is 16px, what is 2rem?
Anonymous Quiz
8%
A) 16px
18%
B) 24px
71%
C) 32px
3%
D) 40px
Which CSS unit represents 1% of the viewport width?
Anonymous Quiz
19%
A) vh
56%
B) vw
19%
C) fr
7%
D) em
What does grid-template-columns: 1fr 1fr; create?
Anonymous Quiz
47%
A) Two equal-width columns
35%
B) Two fixed-width columns
12%
C) One column
6%
D) Two equal-height rows
❤3