Illustration showing CSS container queries adjusting a UI card layout based on parent container width rather than viewport size
Tutorials

CSS Container Queries (@container) क्या है? Responsive Design का नया तरीका

M
Maahi
27 November 2026 5 min read

Media Queries से आगे बढ़ें और सीखें कि CSS Container Queries (@container) कैसे आपके UI components को truly reusable और modular बनाते हैं। इस guide में हम @container के practical examples और use-cases को detail में समझेंगे।

CSS Container Queries (@container) क्या है? Responsive Design का नया तरीका

आज के modern web development में component-based architecture (जैसे React, Vue, या Angular) सबसे ज़्यादा popular है। हम छोटे-छोटे reusable UI components बनाते हैं और उन्हें website के अलग-अलग हिस्सों में use करते हैं। लेकिन एक पुरानी problem हमेशा developers को परेशान करती थी: एक ही component sidebar में छोटा दिखना चाहिए और main section में बड़ा, पर CSS Media Queries सिर्फ पूरी screen (viewport) की width देखती हैं।

इसी problem को solve करने के लिए CSS में Container Queries (@container) आया है। आज Vivaa Technologies के इस tutorial में हम detail में समझेंगे कि @container क्या है, यह कैसे काम करता है, और आप इसे अपने projects में कैसे use कर सकते हैं।

Media Queries vs Container Queries: असली फर्क क्या है?

अब तक हम responsive design के लिए @media queries का इस्तेमाल करते आए हैं। Media query browser की पूरी window (viewport) के size के हिसाब से CSS apply करती है।

मान लीजिए आपने एक Product Card बनाया है। जब आप इस card को main page पर रखते हैं, तो यह horizontal (image left में, text right में) अच्छा लगता है। लेकिन जब आप इसी card को एक narrow sidebar में डालते हैं, तो viewport तो अभी भी desktop size का ही है, इसलिए media query trigger नहीं होती और आपका card टूट जाता है।

Container Queries इस सोच को पूरी तरह बदल देती हैं। यह viewport की जगह parent container के size को देखती हैं। इसका मतलब है कि आपका component अब खुद decide कर सकता है कि उसे किस तरह दिखना है, चाहे उसे page पर कहीं भी रखा जाए।

CSS Container Queries कैसे काम करती हैं?

Container queries को use करने के लिए आपको दो simple steps follow करने होते हैं:

  1. Containment Context Define करना: सबसे पहले parent element को बताना पड़ता है कि उसके size को track किया जाए।

  2. @container Rule लिखना: उसके बाद child element पर parent के size के हिसाब से styles apply करना।

Step 1: Parent Container Setup करना

Parent element पर हम container-type property का use करते हैं। सबसे common value inline-size होती है, जो container की width को track करती है।

CSS

.card-wrapper {
  container-type: inline-size;
  container-name: product-card; /* यह optional है, पर best practice है */
}

आप चाहें तो दोनों properties को एक साथ shorthand में भी लिख सकते हैं:

CSS

.card-wrapper {
  container: product-card / inline-size;
}

Step 2: @container Query लिखना

अब जब parent container set हो गया है, तो हम उसके अंदर के child elements को style कर सकते हैं।

CSS

/* Default style (छोटे container के लिए) */
.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
}

/* जब parent container की width 400px या उससे ज़्यादा हो */
@container product-card (min-width: 400px) {
  .card {
    flex-direction: row;
    align-items: center;
  }

  .card-image {
    width: 150px;
    height: 150px;
  }
}

इस code में, जैसे ही .card-wrapper की width 400px से ज़्यादा होगी, card automatically column layout से row layout में बदल जाएगा। इससे कोई फर्क नहीं पड़ता कि user mobile पर है या desktop पर।

Container Query Units क्या हैं?

सिर्फ layout ही नहीं, आप container के size के हिसाब से font-size या padding भी set कर सकते हैं। इसके लिए CSS नए units देता है:

  • cqw: Container की width का 1% (1% of container width)

  • cqh: Container की height का 1% (1% of container height)

  • cqi: Container के inline size का 1%

  • cqb: Container के block size का 1%

Example के लिए, अगर आप चाहते हैं कि card का heading size container की width के हिसाब से fluidly बढ़े:

CSS

.card-title {
  font-size: clamp(1.1rem, 4cqw, 1.8rem);
}

Container Queries Use करने के फायदे

  • Truly Modular Components: आपके UI components अब 100% independent बन सकते हैं। एक बार code लिखें और उसे grid, sidebar, modal, या footer में बिना किसी extra CSS hack के use करें।

  • Clean Codebase: आपको अलग-अलग जगह के लिए extra modifier classes (जैसे .card--sidebar या .card--large) बनाने की ज़रूरत नहीं पड़ती।

  • Better Maintainability: Future में अगर layout change होता है, तो components खुद को automatically adjust कर लेते हैं।

  • Full Browser Support: आज के समय में सभी modern browsers (Chrome, Firefox, Safari, Edge) @container queries को support करते हैं, इसलिए आप इसे production projects में बिना किसी डर के use कर सकते हैं।

Conclusion

CSS Container Queries modern front-end development का एक game-changer feature है। यह हमें page-centric design से निकाल कर component-centric design की तरफ ले जाता है। अगर आप एक full-stack developer या front-end learner हैं, तो अपने अगले project में @media की जगह @container को ज़रूर try करें।

ऐसे ही practical web development tutorials और industry-ready coding skills सीखने के लिए Vivaa Technologies के साथ जुड़े रहें। हम लगातार ऐसे ही आसान और उपयोगी guides आपके लिए लाते रहते हैं।

Tags
#CSS Container Queries#Responsive Web Design#Frontend Development#CSS Tutorials#Web Development Hindi

Share this article