CSS Float
In this page:
selector {
float: left | right | none;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
Floating Concept
float property किसी element को उसके container के left या right पर धकेलती है, आस-पास के inline text और inline elements को उसके चारों ओर wrap होने देते हुए। यह property originally news articles में images के चारों ओर text wrapping format करने के लिए design की गई थी।
- media illustrations के चारों ओर text को साफ़ तरीके से wrap करने के लिए float: left या float: right इस्तेमाल करें।
- Accepted values:
- left — element को left पर float करता है
- right — element को right पर float करता है
- none — कोई float नहीं (default)
- inline-start | inline-end — direction-aware floats
- clear — left | right | both | none floats के चारों ओर wrapping रोकता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: The Floating Concept
<style>
img {
float: left;
margin-right: 10px;
}
</style>
<div>
<img src="https://placehold.co/100x100" alt="Floated image">
<p>Text wraps around the floated image</p>
</div>
Collapsed Parent Problem
क्योंकि floated elements को normal page flow से हटा दिया जाता है, parent containers उनकी height calculate नहीं करते, 0 पर collapse हो जाते हैं और बाद के elements floated items के नीचे चले जाते हैं।
उदाहरण: The Collapsed Parent Problem
<style>
.parent {
border: 2px solid red;
}
.child {
float: left;
background: lightblue;
padding: 8px;
}
</style>
<div class="parent">
<div class="child">Floated child collapses the parent's height</div>
</div>
clear से Floats को Clear करना
clear property elements को पहले वाले floated elements के चारों ओर wrap होने से रोकता है। clear: left, clear: right, या clear: both लागू करके, आप browser को instruct करते हैं कि element को किसी भी floated items के नीचे धकेल दे।
- बाद के headers या footers को floated columns के चारों ओर wrap होने से रोकने के लिए उन पर clear: both इस्तेमाल करें।
- Accepted values:
- none — floats के बगल में बैठ सकता है (initial value)
- left — किसी भी left floats के नीचे
- right — किसी भी right floats के नीचे
- both — दोनों तरफ़ के floats के नीचे
- inline-start | inline-end — direction-aware versions
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Clearing Floats with clear
<style>
.box {
float: left;
background: lightblue;
padding: 8px;
}
.footer {
clear: both;
background: lightgray;
padding: 8px;
}
</style>
<div class="box">Floated box</div>
<div class="footer">Pushed below the floated box</div>
Modern clearfix Hack
clearfix hack floated elements को clear करने के लिए इस्तेमाल होने वाला एक standard CSS design pattern है। parent container पर एक pseudo-element (:after) लगाकर, आप इसे अपने HTML में extra, खाली div tags जोड़े बिना floats को automatically clear करने का instruction दे सकते हैं।
उदाहरण: The Modern clearfix Hack
<style>
.clearfix {
border: 2px dashed #999;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.child {
float: left;
background: lightblue;
padding: 8px;
}
</style>
<div class="clearfix">
<div class="child">Floated</div>
</div>
Floats बनाम Modern Layout Grids
Flexbox और Grid introduce होने से पहले, developers को multi-column website layouts बनाने के लिए float: left इस्तेमाल करना पड़ता था। जबकि floats अभी भी text-wrapping designs के लिए बेहद उपयोगी हैं, modern layouts को column grids के लिए Flexbox या Grid इस्तेमाल करना चाहिए।
उदाहरण: Floats vs. Modern Layout Grids
<style>
.old-way {
float: left;
width: 50%;
background: lightblue;
padding: 8px;
box-sizing: border-box;
}
.modern-way {
display: flex;
}
.modern-way div {
background: lightgreen;
padding: 8px;
}
</style>
<div class="old-way">Legacy float column</div>
<div class="modern-way">
<div>Flexbox column</div>
</div>
- floated elements को clear करना भूल जाना, जिससे parent containers collapse हो जाते हैं और बाद का content overlap हो जाता है।
- modern Flexbox या Grid के बजाय complex, multi-column website layouts बनाने के लिए floats इस्तेमाल करना।
- floated block elements पर width settings छोड़ देना, जिससे वे float होने के बजाय 100% तक फैल जाते हैं और vertically stack हो जाते हैं।
- float property elements को उनके container के left या right पर धकेलती है, आस-पास के text को उनके चारों ओर wrap होने देते हुए।
- Floated elements को natural page flow से हटा दिया जाता है, जो parent containers को collapse करा सकता है।
- floated elements को clear करने और parent heights को साफ़ तरीके से contain करने के लिए overflow: auto या clearfix hack इस्तेमाल करें।
float और clear properties को सभी modern और legacy web browsers में पूरा native support है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: