CSS3 & Modern Layouts (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is the CSS Box Model (`content-box` vs. `border-box`)?
junior LevelCode ExamplecssBasic
In Simple Words: Every element in CSS is represented as a rectangular box consisting of four concentric layers:
- Content: The inner text, image, or child elements.
- Padding: Transparent space between content and border.
- Border: The line surrounding padding and content.
- Margin: Transparent space outside the border separating the element from neighbors.
lua
+-------------------------------------------------------+
| Margin |
| +-----------------------------------------------+ |
| | Border | |
| | +---------------------------------------+ | |
| | | Padding | | |
| | | +-------------------------------+ | | |
| | | | Content (width x height) | | | |
| | | +-------------------------------+ | | |
| | +---------------------------------------+ | |
| +-----------------------------------------------+ |
+-------------------------------------------------------+
content-box (Legacy Browser Default):
widthapplies only to the content area. Addingpadding: 20pxandborder: 5pxmakes the rendered element wider than the declared width ($200 + 40 + 10 = 250\text{px}$).
border-box (Universal Modern Standard):
widthincludes content + padding + border. An element withwidth: 200pxstays exactly 200px wide regardless of padding or border values.
css
/* Universal production reset */
*, *::before, *::after {
box-sizing: border-box;
}