skip to Main Content

So I expected the icons to move to keep the gap the same when an icon grows on hover, but as it appears transform does not work with flexbox. What should i do to implement this feature? I can code in React if its needed.

Expected flexbox items to move to keep spacing the same.

Items did not change position

:root {
  --bg-color: rgb(20, 20, 20);
}

body {
  align-items: center;
  background-color: var(--bg-color);
  display: flex;
  height: 100vh;
  justify-content: center;
  margin: 0px;
  padding: 0px;
}

#icons {
  display: flex;
  flex-wrap: wrap;
  width: calc(100% -20px);
  gap: 70px;
  max-width: 1500px;
  white-space: nowrap
}

.icon {
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  cursor: pointer;
  height: 150px;
  width: 150px;
  transition: transform 400ms cubic-bezier(.05, .43, .25, .95);
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 20px;
}

#icons .icon:hover {
  transform: scale(1.5);
}

#icons .icon:not(:hover) {
  flex-grow: 1;
}
<div class="group-icon" id="icons">
  <div class="icon">
    <img src="https://images.pexels.com/photos/2269872/pexels-photo-2269872.jpeg?auto=compress&cs=tinysrgb&w=1600">
  </div>


  <div class="icon">
    <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
  </div>

  <div class="icon">
    <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
  </div>

3

Answers


  1. I am not sure if I completely understood your question but from what I read I think you want to grow the image but keep the gap between the icons the same. It does not matter if you are using the flexboxes or not. You can add overflow: hidden on the image wrapper and instead of scaling the wrapper scale up the image inside the wrapper. Hope this helps.

    :root {
      --bg-color: rgb(20, 20, 20);
    }
    
    body {
      align-items: center;
      background-color: var(--bg-color);
      display: flex;
      height: 100vh;
      justify-content: center;
      margin: 0px;
      padding: 0px;
    }
    
    #icons {
      display: flex;
      flex-wrap: wrap;
      width: calc(100% -20px);
      gap: 70px;
      max-width: 1500px;
      white-space: nowrap
    }
    
    .icon {
      background-color: rgba(255, 255, 255, 0.1);
      border-radius: 20px;
      cursor: pointer;
      height: 150px;
      width: 150px;
      transition: transform 400ms cubic-bezier(.05, .43, .25, .95);
      overflow: hidden;
    }
    
    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      border-radius: 20px;
      transition: all 0.5s;
    }
    
    #icons .icon:hover img {
      transform: scale(1.3);
    }
    
    #icons .icon:not(:hover) {
      flex-grow: 1;
    }
    <div class="group-icon" id="icons">
      <div class="icon">
        <img src="https://images.pexels.com/photos/2269872/pexels-photo-2269872.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    Login or Signup to reply.
  2. Instead of using the transform property, I think you’ll need to declare specific dimensions with width and height.

    #icons .icon:hover {
      /* transform: scale(1.5); */
      width: 225px;
      height: 225px;
    }
    

    You’ll also likely want to declare align-items: center for the Flexbox container.

    Additionally, your width calculation is invalid (and therefore ignored) because there is not a space after the minus - character. Instead of "100 percent minus 20 pixels", you have "100 percent negative 20 pixels". However, "100% – 20px" will introduce other issues, so I won’t use it in my answer.

    #icons {
      align-items: center;
      width: calc(100% - 20px);
    }
    
    :root {
      --bg-color: rgb(20, 20, 20);
    }
    
    body {
      align-items: center;
      background-color: var(--bg-color);
      display: flex;
      height: 100vh;
      justify-content: center;
      margin: 0px;
      padding: 0px;
    }
    
    #icons {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      width: calc(100% -20px); /* invalid */
      gap: 70px;
      max-width: 1500px;
      white-space: nowrap
    }
    
    .icon {
      background-color: rgba(255, 255, 255, 0.1);
      border-radius: 20px;
      cursor: pointer;
      height: 150px;
      width: 150px;
      transition: transform 400ms cubic-bezier(.05, .43, .25, .95);
    }
    
    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      border-radius: 20px;
    }
    
    #icons .icon:hover {
      /* transform: scale(1.5); */
      width: 225px;
      height: 225px;
    }
    
    #icons .icon:not(:hover) {
      flex-grow: 1;
    }
    <div class="group-icon" id="icons">
      <div class="icon">
        <img src="https://images.pexels.com/photos/2269872/pexels-photo-2269872.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    </div>
    Login or Signup to reply.
  3. The problem is that transform only changes things visually, not size wise.

    So this snippet alters the actual width and height on hover and transitions them.

    It also translates the image upwards by half the difference in height but you will have to decide whether that is the effect you want if there are multiple lines of images as it won’t move the rows above and below.

    :root {
      --bg-color: rgb(20, 20, 20);
    }
    
    body {
      align-items: center;
      background-color: var(--bg-color);
      display: flex;
      height: 100vh;
      justify-content: center;
      margin: 0px;
      padding: 0px;
    }
    
    #icons {
      display: flex;
      flex-wrap: wrap;
      width: calc(100% -20px);
      gap: 70px;
      max-width: 1500px;
      white-space: nowrap
    }
    
    .icon {
      background-color: rgba(255, 255, 255, 0.1);
      border-radius: 20px;
      cursor: pointer;
      height: 150px;
      width: 150px;
      transition: all 400ms cubic-bezier(.05, .43, .25, .95);
    }
    
    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      border-radius: 20px;
    }
    
    #icons .icon:hover {
      Rtransform: scale(1.5);
      width: 225px;
      height: 225px;
      transform: translateY(-37.5px);
    }
    
    #icons .icon:not(:hover) {
      flex-grow: 1;
    }
    <div class="group-icon" id="icons">
      <div class="icon">
        <img src="https://images.pexels.com/photos/2269872/pexels-photo-2269872.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    
      <div class="icon">
        <img src="https://images.pexels.com/photos/2385044/pexels-photo-2385044.jpeg?auto=compress&cs=tinysrgb&w=1600">
      </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search