Web Technology Basics
.card {
box-sizing: content-box;
width: 200px;
padding: 10px;
border: 2px solid #333;
margin: 20px;
}For the element styled with the CSS above, which of the following is the width from the outer edge of the left border to the outer edge of the right border?
- A200px
- B224px
- C264px
Show answer and explanation
Answer: B. 224px
In the CSS box model, an element consists of the content, the padding, the border and the margin, layered from the inside out. With box-sizing: content-box (the default), width is the width of the content only, so the width to the outside of the border is 200 + 10 × 2 + 2 × 2 = 224px. The margin is the space outside the border and forms the gap to other elements. With box-sizing: border-box, width includes everything up to the border, and the content width becomes 200 − 24 = 176px.
- A. This is the content width only. With content-box, the padding and border are added outside width.
- B. Correct. 200 + 20 + 4 = 224px.
- C. This also adds the left and right margins, 20 × 2 = 40px. The margin is the space outside the border.