1. What is the CSS Box Model and how do content, padding, border, and margin interact?
❓ Context: Explain the four layers of the CSS Box Model and how box-sizing: border-box changes width calculation.
Every HTML element rendered by the browser is wrapped inside an invisible rectangular CSS Box Model consisting of four concentric layers:
width and height.border-width, border-style, and border-color.box-sizing Property:
content-box (default): width applies only to the content area. Padding and border are added on top, making the total rendered width larger.border-box: width includes content + padding + border. This is the preferred modern standard because the element's rendered size matches the declared width./* Universal border-box reset (recommended) */
*, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 300px; /* Total rendered width = 300px */
padding: 20px; /* Included in the 300px */
border: 2px solid #3d2c2e; /* Included in the 300px */
margin: 16px; /* External spacing, not included */
}Always set box-sizing: border-box globally so that width and height include padding and border.