← Back to CSS Course | Chapter 5: Layout & Display | Lesson 8 of 14

CSS Float

एक physical newspaper publish करने की कल्पना करें। जब आप किसी article में एक illustration डालते हैं, तो आप नहीं चाहते कि paragraph text उसके नीचे खाली lines पर अलग रह जाए। आप चाहते हैं कि text flow करे और picture के left या right side के चारों ओर साफ़ तरीके से wrap हो। CSS float वही newspaper layout rule है। यह आपको किसी element को left या right margin पर धकेलने देता है, आस-पास के inline text को उसके चारों ओर naturally wrap होने का instruction देते हुए।
Syntax
css
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 की गई थी।

Note:
  • 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
Warning: Floated elements को normal page flow से हटा दिया जाता है, जो अगर clear न किया जाए तो parent containers को collapse करा सकता है।

उदाहरण: The Floating Concept

css
<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 के नीचे चले जाते हैं।

Note: बिना कोई extra code markup जोड़े floated children को जल्दी clear करने के लिए parent containers पर overflow property इस्तेमाल करें।
Warning: floated children को contain करने में fail होने से parent elements पर borders और backgrounds पूरी तरह collapse हो जाएँगे।

उदाहरण: The Collapsed Parent Problem

css
<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 के नीचे धकेल दे।

Note:
  • बाद के 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
Warning: बिना clear किए गए statically positioned elements आपके page पर floated blocks के साथ overlap हो सकते हैं।

उदाहरण: Clearing Floats with clear

css
<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 दे सकते हैं।

Note: किसी भी floated containers को आसानी से clear करने के लिए अपनी stylesheet में clearfix नाम की एक utility class define करें।
Warning: सुनिश्चित करें कि अगर आपकी site legacy systems को support करती है तो आपकी clearfix styles पुराने browsers द्वारा supported हों।

उदाहरण: The Modern clearfix Hack

css
<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 इस्तेमाल करना चाहिए।

Note: layout columns के लिए modern Flexbox या Grid इस्तेमाल करें, और floats को सख़्ती से text-wrapping designs के लिए बचाएँ।
Warning: complex, responsive grids बनाने के लिए floats इस्तेमाल करना tedious है और layout-breaking errors के लिए prone है।

उदाहरण: Floats vs. Modern Layout Grids

css
<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>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. floated elements को clear करना भूल जाना, जिससे parent containers collapse हो जाते हैं और बाद का content overlap हो जाता है।
  2. modern Flexbox या Grid के बजाय complex, multi-column website layouts बनाने के लिए floats इस्तेमाल करना।
  3. 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 है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.