|
|
|
|
|
by ollerac
1661 days ago
|
|
I ran into so many issues with content-box a decade back I never thought about it again and always defaulted to border-box. I think it's absurd that I can set a div to a specific width (100px, 200px, whatever) and the padding or border will make the div larger than that width. It makes it so hard to think about designing a page when you have to continuously add together padding, margin, and border with the width to get the actual width. You're right about flexbox and grid solving most of these issues -- and responsive design has pretty much done away with using exact pixel widths for anything, but I still run into some cases (especially around creating grid layouts that automatically reflow) where CSS columns and floats are the best approach (not often, but it still happens...) and then you need to rely on the width being the actual width, you know? |
|
It’s about the difference between layout and content concerns.
Margin and padding should generally be layout concerns.
Explicitly-set width and height should generally be content concerns. This means it should exclude margin and padding, so content-box is the most appropriate model.
Before flexbox came along, it was necessary to use width and height for layout concerns quite extensively, which is why people started recommending border-box. But now, the root elements are pretty much the only places that may (or should) use width and height for layout concerns. So I say specify border-box for them if you need, but leave it at content-box everywhere else.
Just like gap (flex/grid/column) isn’t part of width or height—it’s a layout rather than content concern.
I’m saying that you’re thinking the wrong way: don’t try to squeeze your content into the layout, but define your content first of all and let the layout exist around it.