Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

css: how to prevent absolutely positioned :after element from scaling during transition?

I have an absolutely positioned button on the bottom left cornor. The size is fixed at 15px width and height.

The button has an :after element to increase the clickable area size.

When you hover over the button, the button expands. But this also increases size of the :after element.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do you prevent the :after element from scaling scale(1.5) with the button element?

I tried playing around with width and height properties but this messes up the positioning.

body {
position: relative;
height: 100vh;
padding:0;
margin: 0;
}

p {
margin: 0;
padding: 0;
}

button {
      cursor: pointer;
      height: 15px;
      width: 15px;
      border-radius: 50%;
      color: white;
      border: none;
      background: #ffd86e;
      position: absolute;
      bottom: 15px;
      left: 15px;
      z-index: 10000;
    }

    button::after {
      content: '';
      /* z-index: -100000; */
      position: absolute;
      bottom: 50%;
      left: 50%;
      height: 50px;
      width: 50px;
      transform: translate(-50%, 50%);
      background: transparent;
      border: 1px solid black;
    }

    button:hover {
      transform: scale(1.5);
      transition: transform 0.2s;
    }
<p>element is left bottom corner</p>


<button></button>

>Solution :

Invert the styles applied I.E. apply the styles applied to the button to the :after element and vice-versa. because otherwise the :after element will also expand when hovered over the button

button {
  position:relative;
  border:1px solid #000;
  display: block;
  padding: 1.5rem 2.5rem;
}

button::after {
  content: '';
  z-index: -100000;
  position: absolute;
  bottom: 50%;
  left: 50%;
  transform: translate(-50%, 50%);
  
  display: block;
  cursor: pointer;
  height: 15px;
  width: 15px;
  border-radius: 50%;
  color: white;
  border: none;
  background: red;
  z-index: 10000;   
}

button:hover:after {
  transform: translate(-50%, 50%) scale(1.5);
  transition: transform 0.2s;
}
<p>element is left bottom corner</p>
<button></button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading