Questions
3 of 29
1What is the box model in HTML?
2Can you explain the components of the Box Model?
3What’s the difference between margin, border, padding, and content?
4How does padding affect the size of an element?
5How does margin differ from padding?
6What happens when you set negative margins?
7Does padding accept negative values? Why or why not?
8How can you visualize the box model in browser dev tools?
9What is the default box-sizing value for HTML elements?
10What does box-sizing: border-box do?
11Compare box-sizing: content-box and border-box.
12How can you make two boxes align perfectly side by side with consistent spacing?
13How do margin collapsing rules work in CSS?
14Give an example of when margin collapsing might cause layout issues.
15How can you prevent margin collapsing between parent and child elements?
16What is the difference between inline, inline-block, and block elements in terms of the box model?
17Do replaced elements (like <img>, <input>) follow the same box model rules?
18How do width and height interact with padding and border in the box model?
19Why might a layout break when switching from content-box to border-box?
20How would you fix an element that overflows its container due to box model miscalculations?
21How do you use the calc() function to compensate for padding or border widths?
22How does the box model interact with flexbox and grid layouts?
23Does the box model behave differently for absolutely positioned elements?
24How can you ensure consistent box sizing across all elements on a webpage?
25What’s the role of outline in the box model, and how is it different from border?
26How can you use the box model to achieve responsive layouts?
27How do you debug box model-related issues in a complex layout?
28Explain how min-width, max-width, and overflow affect the box model.
29How do borders affect element dimensions?
03 / 29

What’s the difference between margin, border, padding, and content?

Difficulty: 3/10
box model, spacing, layout

Understanding the CSS Box Model

The CSS box model describes how every element on a web page is represented as a rectangular box that consists of four parts: content, padding, border, and margin. It defines how the size of an element and the space around it are calculated in a web layout.

Box Model Structure
Parts of the Box Model
  1. 1

    Content – The actual text or image inside the box.

  2. 2

    Padding – The space between the content and the border.

  3. 3

    Border – The edge surrounding the padding and content.

  4. 4

    Margin – The outermost space that separates the element from others.

Scenario Questions

0-2 years experience

  1. 1You need to build a card component that has a 10px space between the text and the border, and a 5px solid border. Which CSS properties would you set for margin, padding, and border to achieve that layout?
  2. 2If you apply padding:20px to a div and then give it margin:10px, what will be the distance between this div and the element next to it?
  3. 3Assuming the default box-sizing:content-box, what will be the total rendered width of a box that has width:200px, padding:15px, and a 2px border?

2-5 years experience

  1. 1A modal dialog is spilling over its container by a few pixels after you added padding to the inner content. Walk me through how you'd debug whether the overflow is caused by padding, margin collapse, or the border.
  2. 2Your button looks visually larger because of a 3px border, but the clickable area is still the original size. How would you adjust the CSS so the interactive region matches the visual size without redesigning the button?
  3. 3When you changed a list of items from display:inline-block to flex, the spacing between items changed unexpectedly. Explain how margin, border, and padding behave in a flex container and what you would tweak to keep the original spacing.

5-8 years experience

  1. 1Our design system defines spacing tokens for margin and padding. How would you structure the CSS (or CSS‑in‑JS) so that every component consistently follows the box model while still allowing per‑component border overrides?
  2. 2Adding a 1px border to thousands of server‑rendered cards caused a noticeable layout shift and extra paint time. Discuss strategies to mitigate reflow and paint costs related to box‑model changes at this scale.
  3. 3A third‑party widget injects its own padding, causing its content to overflow the surrounding layout. How would you isolate or contain that widget using CSS cascade and specificity so the rest of the page remains unaffected?

8+ years experience

  1. 1We are migrating a legacy codebase that uses box-sizing:content-box everywhere to a new design system that prefers border-box. What risks do you anticipate and what steps would you take to transition globally without breaking existing margin and padding calculations?
  2. 2Our product needs a unified design language across a web app (CSS) and a native mobile app (e.g., SwiftUI). How would you abstract margin, border, and padding so they can be expressed consistently across both platforms while respecting each platform's rendering model?
  3. 3A cross‑team initiative wants to expose spacing as design tokens in a JSON file consumed by multiple front‑ends. Explain how you’d map those tokens to the CSS box model, maintain backward compatibility, and avoid layout regressions during rollout.

Follow-up Questions

  • How does switching to box-sizing:border-box change the way you calculate width and height?
  • Can you give an example where margin collapse caused an unexpected layout issue?
  • When would you prefer adding a border over extra padding for visual separation?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.