liveBook · Manning

Box-sizing

As explained in Acing the Frontend Interview

The CSS box-sizing property determines whether an element's declared width and height include padding and borders.

If an element declared as 100px wide occupies more space than expected, check `box-sizing`. With the default `content-box`, width and height describe only the content; padding and borders increase the element’s total dimensions. With `border-box`, the declared dimensions include content, padding, and borders. Margins remain outside the box. section 2.2

Account for the extra pixels

A `div` with `width: 100px`, `height: 50px`, padding of 30px left and 10px right, and borders of 5px left and 3px right totals 148px wide under `content-box`. Its padding and borders likewise bring its total height to 81px. Set `box-sizing: border-box`, and its total dimensions stay 100px by 50px. In an interview, explain which measurements the declared dimensions include before adjusting the numbers. section 2.2

Figure 2.6 CSS box model, where the content box is at the center, surrounded by padding. The border wraps around the padding, and the margin forms the outermost layer, creating space between elements.

The box-model diagram locates content, padding, border, and margin—the layers to distinguish when explaining an element’s size. section 2.2

How Acing the Frontend Interview covers it

How deep it goes
Defines both sizing modes and works through width and height calculations for one element.
Examples
Compares a 100px-by-50px element measuring 148px by 81px under `content-box` with its declared size under `border-box`.
What it assumes
Introduces content, padding, border, and margin through basic definitions before applying `box-sizing`.
What you can do after
Operate/configure it in CSS to include padding and borders within an element’s declared dimensions.
How it connects
Connects CSS dimensions to the box model, content, padding, borders, and margins.

Also explained in

Compare how 2 books explain box-sizing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.