Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: transform scale with position absolute

I have a little problem with position: absolute. Let me explain.

enter image description here

After hover I have something like this:

enter image description here

CSS code:

.option {
    width: 50px;
    height: 50px;
    box-shadow: 1px 1px 1px 1px  #888;
}

.option:hover {
    transform: scale(2.25, 2.25);
    transition-duration: 0.01s;
    z-index:10;
    position: absolute;
}

I used transform: scale to enlarge the element after hovering over the option. However, to achieve the desired effect I have to add the position: absolute attribute - with that, the option goes out the div after hover. Hover works fine but the rest 3 opitons goes left - is there any way to fix that? I want to still display the 4 options after hover, not to cut the rest.

Without position:absolute the element doesn't go out of div.

enter image description here

like image 776
Patrick1870 Avatar asked May 17 '26 15:05

Patrick1870


1 Answers

By using transform-origin, you can remove the need to use absolute positioning. Move the origin of the transformation from the default (center) to the upper left corner (0 0):

.option {
  width: 50px;
  height: 50px;
  box-shadow: 1px 1px 1px 1px #888;
  display: inline-block;
}

.option:hover {
  transform: scale(2.25, 2.25);
  transition-duration: 0.01s;
  z-index: 1;
  transform-origin: top left;
}
<div>
  <div class="option"></div>
  <div class="option"></div>
  <div class="option"></div>
  <div class="option"></div>
</div>
like image 145
Turnip Avatar answered May 19 '26 10:05

Turnip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!