CSS में पोजिशनिंग मेथड्स
एलिमेंट्स को पोजिशन करने के लिए CSS में निम्नलिखित पोजिशनिंग मेथड्स हैं:
1. रिलेटिव पोजिशनिंग (Relative Positioning)
रिलेटिव पोजिशनिंग में, एलिमेंट को उसकी सामान्य पोजिशन के सापेक्ष पोजिशन किया जाता है। इसके लिए position: relative का उपयोग करें।
उदाहरण
<!DOCTYPE html>
<html>
<head>
<style>
div.demo {
position: relative;
color: white;
background-color: orange;
border: 2px dashed blue;
left: 50px;
}
</style>
</head>
<body>
<h2>डेमो हेडिंग</h2>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<div class="demo"> position: relative; </div>
<p>यह एक और डेमो टेक्स्ट है।</p>
</body>
</html>
आउटपुट
डिव अपनी सामान्य पोजिशन से 50px बाईं ओर खिसक जाएगा, लेकिन अन्य एलिमेंट्स पर कोई प्रभाव नहीं पड़ेगा।
2. एब्सोल्यूट पोजिशनिंग (Absolute Positioning)
एब्सोल्यूट पोजिशनिंग में, एक एलिमेंट को निकटतम पोजिशन्ड एन्सेस्टर (positioned ancestor) के सापेक्ष पोजिशन किया जाता है।
उदाहरण
<!DOCTYPE html>
<html>
<head>
<style>
div.demo1 {
position: relative;
color: white;
background-color: orange;
border: 2px dashed blue;
width: 600px;
height: 200px;
}
div.demo2 {
position: absolute;
color: white;
background-color: orange;
border: 2px dashed blue;
top: 50px;
right: 0;
width: 300px;
height: 100px;
}
</style>
</head>
<body>
<h2>डेमो हेडिंग</h2>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<div class="demo1">position: relative;
<div class="demo2"> position: absolute; </div>
</div>
<p>यह एक और डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
</body>
</html>
आउटपुट
बच्चा एलिमेंट (demo2) पैरेंट एलिमेंट (demo1) के सापेक्ष top: 50px और right: 0 पर पोजिशन होगा।
3. फिक्स्ड पोजिशनिंग (Fixed Positioning)
position: fixed वाला एलिमेंट पेज स्क्रॉल होने पर भी उसी स्थान पर रहता है।
उदाहरण
<!DOCTYPE html>
<html>
<head>
<style>
div.demo1 {
position: relative;
color: white;
background-color: orange;
border: 2px dashed blue;
width: 600px;
height: 200px;
}
div.demo2 {
position: absolute;
color: white;
background-color: orange;
border: 2px dashed blue;
top: 50px;
right: 0;
width: 300px;
height: 100px;
}
div.demo3 {
position: fixed;
bottom: 0;
right: 20px;
width: 500px;
border: 3px solid orange;
color: blue;
}
</style>
</head>
<body>
<h2>डेमो हेडिंग</h2>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<p>यह डेमो टेक्स्ट है।</p>
<div class="demo1">position: relative;
<div class="demo2"> position: absolute; </div>
<div class="demo3"> position: fixed; </div>
</div>
<p>यह एक और डेमो टेक्स्ट है।</p>
</body>
</html>
आउटपुट
फिक्स्ड एलिमेंट (demo3) व्यूपोर्ट के सापेक्ष नीचे दाईं ओर स्थिर रहेगा, स्क्रॉल करने पर भी नहीं हिलेगा।
सारांश:
| पोजिशनिंग प्रकार | व्यवहार |
|---|---|
| relative | सामान्य फ्लो में रहता है, offset प्रॉपर्टीज से खिसकाया जा सकता है |
| absolute | निकटतम positioned ancestor के सापेक्ष, सामान्य फ्लो से बाहर |
| fixed | व्यूपोर्ट के सापेक्ष, स्क्रॉल पर भी स्थिर |
| static | डिफ़ॉल्ट, कोई पोजिशनिंग नहीं |