2

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.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
BigName
  • 1,018
  • 2
  • 14
  • 29

1 Answers1

1

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>
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
  • Might be worth separating the padding property to be the only one within the `.cnr>div:not(.closed)` selector, and still having the `overflow`, `border` and `box-sizing` within the original selector, just incase there's ever a time where the closed one still needs access to those properties. – Sam Willis May 24 '17 at 23:46