skip to Main Content

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

2

Answers


  1. 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>
    Login or Signup to reply.
  2. You can transite from 0fr to 1fr but restrict their min-width to 255px

    .test123 {
      grid-template-columns: 0fr 0fr;
      transition: 200ms;
      display: grid;
      gap: 30px;
      
      .card {
        border-radius: 50px;
        border: 1px solid #BBD0FB;
      }
      .c1 {
        background-color: red;
        height: 100px;
        min-width: 255px;
      }
      .c2 {
        background-color: blue;
        height: 100px;
        min-width: 255px;
      }
    }
    
    .test123:has(:nth-child(1):hover) {
      grid-template-columns: 1fr 0fr;
    }
    
    .test123:has(:nth-child(2):hover) {
      grid-template-columns: 0fr 1fr;
    }
    <div class="test123">
      <div class="card c1"></div>
      <div class="card c2"></div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search