How to get flexbox to include padding in calculations?
To have padding included in your flex item sizes, set box-sizing: border-box;
This ensures that the element's total width and height include padding and border:
What this piece of code does is apply the W3C specification in flexbox calculations by taking both padding and border into account. This act of setting box-sizing: border-box;
is a game-changer. It transforms the way widths and heights are calculated, meaning padding increase results in the shrinking of content space rather than the expansion of the total element.
box-sizing
101: Getting to know content-box
and border-box
Default vs custom box sizing
By default, elements use box-sizing: content-box;
, which means padding and borders get piled onto the element's width and height. The result? Possible misalignments and the risk of items spilling over when sizes are set directly:
Padding and item alignment in a flexbox
Including padding can potentially disrupt flex item alignment and justify-content settings. Since available space shrinks with added padding, flex items can be kicked out of their expected positions:
In such a case, wrapper elements can come to the rescue. Wrap the flex item's content in another element and apply padding there to prevent misalignment.
Customizable flex item sizing with padding
flex-grow
and flex-basis
combine for precise control over the sizing of flex items. flex-basis
takes care of the initial size, and flex-grow
allocates additional space based on the total box size:
Picking the right approach: To flexbox or to CSS Grid?
While flexbox has its strengths, CSS Grid may be a more suitable option when the precise control over item alignment and spacing is a priority:
Best practices for padding in flex children
Margin for breathing room
Applying margins on child elements can create a similar visual spacing as padding on flex items without interfering with flex calculations:
The Padding Imposter
Alternatively, you can make use of a pseudo-element to conjure up the effect of padding while keeping the size in check:
Keeping flex items responsive with padding
Adopting a combo of relative units like em
or vw
for padding and media queries can keep your content traffic under control on smaller screens:
Was this article helpful?