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

CSS Resize

एक whiteboard की कल्पना करें जो एक fixed size पर wall से bolted है, अगर आपको लिखने के लिए ज़्यादा जगह चाहिए, तो आप बस असहाय हैं। अब उसी whiteboard को wheels और एक expandable frame के साथ सोचें, आप corner पकड़कर जब भी ज़्यादा जगह चाहिए उसे बड़ा stretch कर सकते हैं, फिर बाद में वापस छोटा कर सकते हैं। resize property web page पर boxes में वह expandable frame जोड़ती है, सबसे common रूप से textareas में, visitors को corner में एक छोटा drag handle देकर किसी element को अपनी ज़रूरत के हिसाब से बड़ा या छोटा करने देती है।
Syntax
css
selector {
  resize: none | both | horizontal | vertical;
  overflow: auto;
}

resize Property Values

resize none, both, horizontal, और vertical accept करता है, control करते हुए कि एक user-visible drag handle किन directions में किसी element को बढ़ने या सिकुड़ने देती है।

Note:
  • comment boxes पर resize: vertical इस्तेमाल करें ताकि users layout को गलती से wide किए बिना लिखने के लिए ज़्यादा जगह बना सकें।
  • Accepted values:
  • none — resizable नहीं (default)
  • both — दोनों directions में resize
  • horizontal — सिर्फ width
  • vertical — सिर्फ height
  • block | inline — direction-aware resizing
  • visible के अलावा overflow चाहिए
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
Warning: resize: both किसी box को इतना wide होने दे सकता है कि वह एक fixed-width layout तोड़ दे जब तक आप एक max-width constraint भी लागू न करें।

उदाहरण: The resize Property Values

css
<style>
.box {
  resize: both;
  overflow: auto;
  width: 150px;
  height: 100px;
  background: lightblue;
  border: 1px solid #666;
}
</style>
<div class="box">Drag the corner to resize</div>

overflow क्यों ज़रूरी है

resize property सिर्फ तभी active होती है जब overflow को hidden, scroll, या auto set किया जाए, क्योंकि browser को यह जानना ज़रूरी है कि box का size उसकी default से मेल न खाने पर content कैसे manage करे, default overflow: visible इसके लिए कोई mechanism नहीं देता।

Note: resize के साथ overflow: auto आमतौर पर सबसे अच्छी choice है, यह सिर्फ तभी scrollbars दिखाता है जब content असल में resized box से ज़्यादा हो जाए।
Warning: overflow को उसकी default visible value से बदले बिना resize: both set करना चुपचाप कुछ नहीं करता, यह सबसे common resize mistakes में से एक है।

उदाहरण: Why overflow Is Required

css
<style>
.box {
  resize: both;
  overflow: auto;
  width: 150px;
  height: 80px;
  background: lightgreen;
  border: 1px solid #666;
}
.broken {
  resize: both;
  width: 150px;
  height: 80px;
  background: lightpink;
  border: 1px solid #666;
  margin-top: 10px;
}
</style>
<div class="box">Works: overflow is set</div>
<div class="broken">Does nothing: overflow is still visible</div>

Textarea Resize Behavior

Browsers automatically textarea elements पर resize: both लागू करते हैं क्योंकि उनकी already default रूप से overflow: auto है, textareas को वह अकेला element बनाते हुए जहाँ resize बिना extra CSS के out of the box काम करता है।

Note:
  • किसी resizable textarea पर एक sensible min-height set करें ताकि users गलती से इसे किसी unusable sliver तक न सिकोड़ सकें।
  • Accepted values:
  • none — कोई resizing नहीं
  • both — दोनों directions में resizable
  • horizontal — सिर्फ width
  • vertical — सिर्फ height
  • block / inline — logical directions
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
Warning: resize: none से किसी textarea की resize को पूरी तरह restrict करना उन users को frustrate कर सकता है जिन्हें लंबे messages type करने के लिए ज़्यादा जगह चाहिए।

उदाहरण: Textarea Resize Behavior

css
<style>
textarea { width: 240px; height: 80px; border: 2px solid teal; padding: 6px; }
</style>
<textarea>Resizable by default, no extra CSS needed</textarea>

एक Custom Resize Handle बनाना

क्योंकि native resize handles को restyle नहीं किया जा सकता, कुछ designs resize: none को एक custom-styled corner element और JavaScript के साथ combine करते हैं ताकि resizing के visual appearance पर पूरा control मिले।

Note: अपने custom handle element पर cursor: nwse-resize इस्तेमाल करें ताकि mouse pointer फिर भी resize direction सही तरीके से बताए।
Warning: एक pure CSS custom handle सिर्फ decorative है, किसी custom handle के लिए असली resizing behavior के लिए फिर भी drag movement track करने के लिए JavaScript चाहिए।

उदाहरण: Building a Custom Resize Handle

css
<style>
.box {
  resize: none;
  position: relative;
  width: 150px;
  height: 100px;
  background: lightblue;
  border: 1px solid #666;
}
.handle {
  cursor: nwse-resize;
  position: absolute;
  bottom: 0;
  right: 0;
}
</style>
<div class="box">
  <span class="handle">◢</span>
</div>

Resize Direction को Restrict करना

both और none से आगे, horizontal और vertical dragging को एक single axis तक restrict करते हैं, जो तब उपयोगी है जब किसी layout का सिर्फ एक dimension आस-पास के content को तोड़े बिना safely flex कर सकता है।

Note:
  • resizable sidebar panels के लिए resize: horizontal इस्तेमाल करें ताकि users page के vertical flow को असर किए बिना उन्हें wide या narrow कर सकें।
  • Accepted values:
  • none — resize नहीं किया जा सकता
  • horizontal — सिर्फ width
  • vertical — सिर्फ height
  • both — width और height
  • overflow visible के अलावा होनी चाहिए
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
Warning: non-textarea elements के लिए resize: horizontal और resize: vertical हर जगह identically supported नहीं हैं, अगर precise control मायने रखता है तो हमेशा target browsers पर test करें।

उदाहरण: Restricting Resize Direction

css
<style>
.sidebar {
  resize: horizontal;
  overflow: auto;
  width: 150px;
  height: 80px;
  background: lightyellow;
  border: 1px solid #666;
}
</style>
<div class="sidebar">Only widens or narrows, not taller</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. किसी element पर overflow को visible के अलावा किसी value पर set किए बिना resize set करना, जब तक overflow hidden, scroll, या auto न हो resize का कोई असर नहीं होता।
  2. यह उम्मीद करना कि resize inline elements पर काम करेगा, resize सिर्फ उन elements पर लागू होता है जो block-level हैं या जिनकी block या inline-block जैसी defined display है।
  3. यह मान लेना कि resize default रूप से सभी form elements पर supported है, browsers द्वारा out of the box सिर्फ textarea पर resize enabled है।
चैप्टर सारांश
  • resize users को एक handle drag करके किसी element के dimensions manually बदलने देता है, both, horizontal, vertical, और none values के साथ।
  • resize को लागू होने के लिए overflow को visible के अलावा किसी value पर set होना चाहिए।
  • textarea elements पर browsers default रूप से resize: both लागू करते हैं, जिसे आप ज़रूरत के हिसाब से override या restrict कर सकते हैं।
ब्राउज़र सपोर्ट

resize हर modern browser में supported है सिवाय इसके कि कुछ पुराने versions में Safari पर textarea के अलावा elements पर इसका कोई असर नहीं होता; both, horizontal, और vertical values Chrome, Firefox, और Edge में broadly काम करती हैं।

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.