csscss-gridcss-transitions

transition on grid-template-columns fr to px value


I have this HTML / CSS snippet:

.test123 {
  grid-template-columns: 255px 255px;
  transition: 200ms;
  display: grid;
  gap: 30px;
  .card {
    border-radius: 50px;
    border: 1px solid #BBD0FB;
  }
  .c1 {
    background-color: red;
    width: 100%;
    height: 100px;
  }
  .c2 {
    background-color: blue;
    width: 100%;
    height: 100px;
  }
}
.test123:has(:nth-child(1):hover) {
  grid-template-columns: 1fr 255px;
}
.test123:has(:nth-child(2):hover) {
  grid-template-columns: 255px 1fr;
}
<div class="test123">
        <div class="card c1"></div>
        <div class="card c2"></div>
</div>

I want the blue and red cards to have some transition when they are hovered, so they would smoothly grow and shrink.

I tried to allow transition-behaviour: allow-discrete but it did not help, so I am kind of stuck with this issue.

Thank you


Solution

  • Well, the easiest way is to do the browser's job. Looking at your code, I would advise you to keep it simple.

    .test123 {
      grid-template-columns: 255px 255px;
      transition: 200ms;
      display: grid;
      gap: 30px;
      .card {
        border-radius: 50px;
        border: 1px solid #BBD0FB;
      }
      .c1 {
        background-color: red;
        width: 100%;
        height: 100px;
      }
      .c2 {
        background-color: blue;
        width: 100%;
        height: 100px;
      }
    }
    
    .test123:has(:nth-child(1):hover) {
      grid-template-columns: calc(100% - 255px - 30px) 255px;
    }
    
    .test123:has(:nth-child(2):hover) {
      grid-template-columns: 255px calc(100% - 255px - 30px);
    }
    <div class="test123">
      <div class="card c1"></div>
      <div class="card c2"></div>
    </div>