CSS Units
In this page:
selector {
width: number + unit; /* px, em, rem, %, vw, vh */
}
Rigid Pixel Units (px)
Pixels (px) absolute units हैं। एक pixel किसी standard computer screen पर color के एक single physical dot को represent करता है। क्योंकि pixels absolute हैं, ये पूरी तरह rigid हैं और दूसरे elements या browser settings के relative scale नहीं होते।
- border widths जैसी properties के लिए pixel units इस्तेमाल करें जिन्हें exact, unyielding precision चाहिए।
- Accepted values:
- 1px — एक CSS pixel (लगभग 1/96 inch)
- <number>px — कोई भी integer या decimal length
- 0 — lengths के लिए unitless zero allowed है
उदाहरण: Rigid Pixel Units (px)
<style>
.box {
border: 2px solid black;
}
</style>
<div class="box">2px border stays exactly 2px always</div>
Flexible Root em Units (rem)
rem (Root Em) unit एक relative unit है। यह root html element के base font size (जो ज़्यादातर browsers में default रूप से 16px है) के relative values calculate करता है। यह rem units को highly accessible और responsive बनाता है।
- यह सुनिश्चित करने के लिए कि वे browser text settings के साथ proportionally scale हों, सभी typography, padding, और margins के लिए rem units इस्तेमाल करें।
- Accepted values:
- 1rem — root html font-size के बराबर (default रूप से 16px)
- 1.5rem — root font-size का 1.5 गुना
- 0.5rem — root font-size का आधा
उदाहरण: Flexible Root em Units (rem)
<style>
html { font-size: 16px; }
p { font-size: 1.5rem; }
</style>
<p>1.5rem = 1.5 times the root font size</p>
Contextual em Units (em)
em unit एक relative unit है जो element के parent container के font size के relative values calculate करता है। यह em units को उस specific component के text size के साथ proportionally spacing (जैसे padding) scale करने के लिए बेहद उपयोगी बनाता है।
- buttons के अंदर padding और margins के लिए em units इस्तेमाल करें ताकि spacing text size के proportional रहे।
- Accepted values:
- 1em — element के अपने font-size के बराबर (या parent का, font-size के लिए खुद)
- 1.5em — उस font-size का 1.5 गुना
- 0.5em — उस font-size का आधा
उदाहरण: Contextual em Units (em)
<style>
.parent { font-size: 20px; }
.child { padding: 1em; }
</style>
<div class="parent">
<span class="child">1em padding = 20px, matching the parent's font-size</span>
</div>
Viewport Width और Height (vw/vh)
Viewport units browser window size के relative होते हैं: 1vw viewport width का 1% होता है, और 1vh viewport height का 1% होता है। यह इन्हें full-screen layouts बनाने के लिए perfect बनाता है।
- जल्दी full-screen landing pages या hero section grids बनाने के लिए height: 100vh इस्तेमाल करें।
- Accepted values:
- 1vw — viewport width का 1%
- 1vh — viewport height का 1%
- 100vw / 100vh — पूरी viewport width या height
- 1dvh — dynamic viewport height का 1% (mobile-friendly)
उदाहरण: Viewport Width and Height (vw/vh)
<style>
.hero {
height: 100vh;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<div class="hero">Always fills the full viewport height</div>
Percentage Units (%)
percentage (%) unit एक relative unit है जो parent container element के size के relative values calculate करता है, इसे fluid multi-column grids बनाने के लिए perfect बनाते हुए।
- padding को percentage column grids तोड़ने से रोकने के लिए globally box-sizing: border-box set करें।
- Accepted values:
- 50% — reference size का आधा (आमतौर पर parent)
- 100% — पूरा parent size
- <number>% — property जो भी percentage allow करे
उदाहरण: Percentage Units (%)
<style>
.parent {
width: 500px;
background: #ddd;
padding: 8px;
}
.child {
width: 50%;
background: coral;
color: white;
padding: 8px;
}
</style>
<div class="parent">
<div class="child">50% of its 500px parent</div>
</div>
- font sizes के लिए absolute pixel units इस्तेमाल करना, जो users को browser settings में text scale करने से रोकता है और web accessibility guidelines violate करता है।
- कई em-sized elements को recursively nest करना, जिससे font sizes exponentially multiply होकर control से बाहर बढ़ जाते हैं।
- mobile screens पर viewport height units (vh) का ज़्यादा इस्तेमाल, जो content को native system status bars के साथ overlap करा सकता है।
- rigid pixels (px) सिर्फ उन properties के लिए इस्तेमाल करें जिन्हें absolute, unyielding precision चाहिए (जैसे border widths)।
- accessible typography के लिए rem units इस्तेमाल करें, और component padding को text size के proportional scale करने के लिए em units।
- percentage units (%) इस्तेमाल करके fluid columns बनाएँ, और viewport width (vw) और height (vh) units इस्तेमाल करके full-screen layouts।
Standard CSS units (px, rem, em, %, vw, vh) हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: