Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox item widths are affected by padding

Tags:

css

flexbox

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;
}
like image 964
exclipy Avatar asked Aug 19 '15 15:08

exclipy


People also ask

Does padding work with Flexbox?

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.

How do you control flex item width?

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.

Does Flex override width?

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.

Which properties can control the size of Flex items?

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.


1 Answers

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>
like image 90
Paulie_D Avatar answered Sep 19 '22 21:09

Paulie_D