cssflexboxborder-boxbox-sizing

flex-box box-sizing: border-box not working


As title says:

.cnr{
  display: flex;
  height: 100%;
  background-color: grey;
  position: absolute;
  border: 1px red solid;
}
.cnr > div{
  box-sizing: border-box;
  padding: 1em;
  border: 1em solid;
  overflow: hidden;
}
.cnr > .closed{
  width: 0;
  flex-basis: 0;
  flex-shrink: 1;
  flex-grow:   0;
  min-width:   0;
  min-height:  0;
}
<div class="cnr">
  <div>
    open
  </div><div class="closed">
    closed
  </div>
</div>

Can't closed the second div using width: 0 because of padding and border.

What do?

I don't know what more details can I add. It's a container set to display: flex. Each flex item has a border and padding and some (text) content. There are 2 of them in total. Then some of them will be closed using width: 0, and to closed the border and padding as well. box-sizing: border-box is used.

But it doesn't work. Thank you.

Edit: ok, sorry for not explaining clearly, but width needs to be animated, that's why the padding needs to be there when the animation is playing, and I would like to not animate the padding as well if possible.


Solution

  • you could use :not:

    .cnr>div{
      box-sizing: border-box;
      overflow: hidden;
    }
    
    .cnr>div:not(.closed) {
      border: 1em solid;
      padding: 1em;
    }
    

    $('.cnr').on('click', function(){
      $(this).find('div').toggleClass('closed');
    })
    .cnr {
      display: flex;
      height: 100%;
      background-color: grey;
      position: absolute;
      border: 1px red solid;
    }
    
    .cnr>div{
      box-sizing: border-box;
      overflow: hidden;
    }
    
    .cnr>div:not(.closed) {
      border: 1em solid;
      padding: 1em;
    }
    
    .cnr>.closed {
      width: 0;
      flex-basis: 0;
      flex-shrink: 1;
      flex-grow: 0;
      min-width: 0;
      min-height: 0;
      overflow: hidden;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="cnr">
      <div>
        open
      </div>
      <div class="closed">
        closed
      </div>
    </div>