← Back to CSS Course | Chapter 9: Modern CSS Features | Lesson 25 of 25

CSS Interview Prep

एक job interview में महीनों coding practice करके जाने की कल्पना करें, लेकिन किसी ने कभी आपसे अपनी reasoning ज़ोर से explain करने के लिए नहीं कहा, सिर्फ चुपचाप code लिखने के लिए। CSS interviews अलग होते हैं, वे अक्सर test करते हैं कि क्या आप pressure में concepts को साफ़ तरीके से explain कर सकते हैं, सिर्फ यह नहीं कि क्या आप working code produce कर सकते हैं, box model, specificity, और layout systems के बीच अंतर जैसी चीज़ें। यह chapter सबसे ज़्यादा पूछे जाने वाले CSS interview questions को concise, confident answers और छोटे runnable examples के साथ collect करता है, ताकि आप सिर्फ syntax recite करने के बजाय अपनी thinking explain करने के लिए तैयार होकर जा सकें।

CSS Box Model Explain करें

हर element अंदर से बाहर तक चार layers से बना एक rectangular box है: content, padding, border, और margin। box-sizing: content-box, जो default है, width और height को सिर्फ content area पर लागू करता है, जबकि box-sizing: border-box specified width और height के अंदर padding और border शामिल करता है।

Note: एक strong interview answer explicitly box-sizing का ज़िक्र करता है, क्योंकि यह बदल देता है कि width और height practice में असल में कैसे calculate होती हैं।
Warning: यह भूल जाना कि margin box model की width calculation से पूरी तरह बाहर बैठता है, इस concept को live explain करते समय एक common mistake है।

उदाहरण: Explain the CSS Box Model

css
<style>
.content-box {
  box-sizing: content-box;
  width: 200px;
  padding: 20px;
  background: lightblue;
  border: 1px solid #666;
}
.border-box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  background: lightgreen;
  border: 1px solid #666;
  margin-top: 10px;
}
</style>
<div class="content-box">content-box: renders 240px total</div>
<div class="border-box">border-box: stays 200px</div>

Specificity कैसे Calculate होती है

Specificity counts के एक tuple के रूप में calculate होती है, inline styles, फिर ID selectors, फिर class, attribute, और pseudo-class selectors, फिर element और pseudo-element selectors, higher categories हमेशा किसी भी संख्या की lower categories से भारी पड़ती हैं, और !important normal specificity को पूरी तरह override कर देता है।

Note: interviews के लिए एक quick trick: specificity को चार buckets के रूप में describe करें, inline, IDs, classes, elements, और एक single score add up करने की कोशिश करने के बजाय buckets को left से right compare करें।
Warning: कई लोग गलती से मानते हैं कि ज़्यादा selectors का मतलब हमेशा ज़्यादा specificity होता है, दस element selectors chained करके भी एक अकेले class selector से हार जाते हैं।

उदाहरण: How Does Specificity Get Calculated

css
<style>
a { color: black; }
.link { color: blue; }
#nav-link { color: red; }
</style>
<a href="#" id="nav-link" class="link">ID beats class beats element: red</a>

Flexbox बनाम Grid, और Elements को Center कैसे करें

Flexbox items को एक समय में एक axis के साथ arrange करता है, navigation bars और button groups के लिए ideal, जबकि Grid items को rows और columns के आर-पार एक साथ arrange करता है, पूरे page layouts के लिए ideal।

किसी element को center करना एक common follow-up question है, जिसका सबसे अच्छा जवाब है display: flex को align-items: center और justify-content: center के साथ combine करना।

Note: जब पूछा जाए कि किसी div को कैसे center करें, तो confidently पहले flexbox centering demonstrate करें, फिर एक छोटे alternative के रूप में grid के place-items: center का ज़िक्र करें।
Warning: यह दावा करना कि flexbox कई lines में wrap नहीं हो सकता एक common factual mistake है, flex-wrap: wrap flex items को grid rows की तरह ही नई lines में flow होने देता है।

उदाहरण: Flexbox vs Grid, and How to Center Elements

css
<style>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 150px;
  background: #eee;
}
.center div {
  background: coral;
  padding: 10px;
}
</style>
<div class="center">
  <div>Centered with flex + align-items + justify-content</div>
</div>

position Values और Cascade को Explain करें

static कोई offset behavior नहीं वाला default है, relative किसी element को उसकी जगह बनाए रखते हुए उसकी normal position से offset करता है, absolute इसे flow से हटा देता है और सबसे नज़दीकी positioned ancestor के against position करता है, fixed viewport के against position करता है, और sticky scroll के आधार पर relative और fixed के बीच toggle करता है।

Cascade वह algorithm है जो origin, specificity, और source order का उसी priority में इस्तेमाल करके conflicting rules resolve करता है।

Note: cascade explain करते समय, तीन tiebreakers को order में mention करें: origin और importance पहले, फिर specificity, फिर source order अंतिम tiebreaker के रूप में।
Warning: एक common mistake यह कहना है कि absolute positioning हमेशा viewport के relative होती है, यह सिर्फ तभी viewport के relative होती है जब कोई ancestor static के अलावा कोई position न रखता हो।

उदाहरण: Explain the position Values and the Cascade

css
<style>
.relative {
  position: relative;
  top: 10px;
  background: #eef;
  padding: 20px;
}
.absolute {
  position: absolute;
  top: 0;
  right: 0;
  background: coral;
  padding: 8px;
}
</style>
<div class="relative">
  <div class="absolute">Positioned against the relative ancestor</div>
</div>

Pseudo-Class बनाम Pseudo-Element, और Inline बनाम Block बनाम Inline-Block

:hover या :first-child जैसा एक pseudo-class किसी element को एक particular state या structural position में target करता है, एक single colon से लिखा जाता है, जबकि ::before या ::after जैसा एक pseudo-element किसी element के generated sub-part को target करता है, एक double colon से लिखा जाता है।

display: block elements एक पूरी नई line लेते हैं और width व height accept करते हैं, inline elements text के साथ flow करते हैं और width व height को नज़रअंदाज़ करते हैं, और inline-block elements text के साथ flow करते हैं लेकिन width व height accept करते हैं।

Note: pseudo-elements के लिए double-colon syntax का confidently ज़िक्र करें, ::before, यह एक common detail है जो interviewers सुनते हैं भले ही backward compatibility के लिए single-colon अभी भी accepted है।
Warning: यह दावा करना कि inline elements कभी size नहीं किए जा सकते सिर्फ आधा सच है, वे width और height को नज़रअंदाज़ करते हैं लेकिन फिर भी horizontal padding और margin पा सकते हैं, जो कई candidates को हैरान कर देता है।

उदाहरण: Pseudo-Class vs Pseudo-Element, and Inline vs Block vs Inline-Block

css
<style>
li:first-child { font-weight: bold; }
li::before { content: "→ "; }
</style>
<ul>
  <li>First, bold, with an arrow prefix</li>
  <li>Second</li>
</ul>
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. definitions को word for word याद करना बिना यह दिखाने में सक्षम हुए कि elaborate करने के लिए कहे जाने पर उन्हें एक live example से demonstrate किया जा सके।
  2. specificity को source order के साथ confuse करना, एक highly specific selector हमेशा जीतता है चाहे वह stylesheet में कहीं भी दिखे, जब तक !important या equal specificity शामिल न हो।
  3. flexbox versus grid questions का जवाब ऐसे देना जैसे एक strictly बेहतर हो, interviewers सुनना चाहते हैं कि सही choice इस पर निर्भर करती है कि layout one-dimensional है या two-dimensional।
चैप्टर सारांश
  • box model, specificity, cascade, और positioning को words और एक quick live example दोनों से explain करने के लिए तैयार रहें।
  • one-dimensional layouts के लिए flexbox और two-dimensional layouts के लिए grid के बीच practical difference समझें।
  • :hover जैसे pseudo-classes जो states target करते हैं, और ::before जैसे pseudo-elements जो generated content target करते हैं, के बीच अंतर जानें।
ब्राउज़र सपोर्ट

इस chapter में covered सभी concepts, box model, specificity, flexbox, grid, positioning, और pseudo-classes व elements, standard CSS features हैं जो हर modern browser द्वारा supported हैं।

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.