01 / 05
You set it to 100% and it still spills out
Fit any screen width
INPUT · Slides
01 / 05
You set a width of 100%, added some space, and the box went outside — that is not a typo.
Once you know what CSS means by width, the reason falls neatly into place.
.card { width: 100%; padding: 16px;}02 / 05
Ordinarily width is the width of the contents only. The space (padding) and the border (border) get added outside it.
So the size you see comes to:
Which is always bigger than the box outside.
03 / 05
Write box-sizing: border-box; and the meaning of width changes to "the size including the space and the border".
The space is no longer added outside; the contents shrink to make room. So 100% fits exactly.
.card { width: 100%; padding: 16px; box-sizing: border-box;}04 / 05
This is not something you vary box by box. Mix two ways of counting on one page and you will struggle to find the cause of any breakage.
List selectors with commas and you can set them all together. Writing it at the top of your CSS is a good habit.
.inner, .card, .panel { box-sizing: border-box;}05 / 05
Widths as percentages, restacking when narrow, and now counting the same way everywhere. Those three see off most breakage.
Have a go.