See http://jsfiddle.net/56qwuz6o/3/:
<div style="display:flex">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
</div>
div div {
flex: 1 1 0;
border:1px solid black;
box-sizing: border-box;
}
#a {
padding-right: 50px;
}
When I have padding set on a flex item (#a
), its width (in the border-box
sense) is affected. How do I make its computed width ignore the padding? ie. I want each of my boxes to take up 33% of the document width.
Edit:
Thanks for the answers so far. But in reality, I actually have more boxes in the row that have a fixed width: ie. at http://jsfiddle.net/56qwuz6o/7/, I want #a
, #b
and #c
to all have the same width.
<div style="display:flex; width: 400px">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
</div>
div div {
flex: 1 1 33%;
border:1px solid black;
box-sizing: border-box;
}
#a {
padding-right: 100px;
}
#d {
flex-basis: 200px;
width: 200px;
}
You do have to make sure that the padding on the ::after pseudo-element is half that of the padding used on the flex container, because padding is applied all around the pseudo-element. But if you go with padding-left or padding-right , then you can use the same value as the padding on the flex container.
A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.
flex-basis is limited by both max-width/max-height and min-width/min-height. When declared, flex-basis will override the width / height property set on a flex container.
The flex-shrink property. The flex-shrink property specifies the flex shrink factor, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when negative free space is distributed.
A proper `flex: declartion seems to work.
div div {
flex: 0 0 33%;
/* don't grow, don't shrink, be 33% wide */
/* flex:1 0 33% works too */
border: 1px solid black;
box-sizing: border-box;
}
#a {
padding-right: 100px;
}
<div style="display:flex">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With