0

I want to be able to click on a <div> (with some text in it) and be able to scale it properly. I scale the <div> using transform: scale(<x>).

The problem with the below code is that the scaling pushes the <div> "forward" and sort of spazzes out on hovering. I effect I would like is that it pops-out, such that, it stays in the middle, but, it expands its width and height.

Example (notice the font size increases):

enter image description here

Sample code with the problem:

div {
  position: absolute;
  width: 300px;
  height: 150px;
  border: 1px black solid;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
}

div:hover {
  transform: scale(1.5);
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
</head>

<body>
  <div>This is a div.</div>
</body>

</html>
user12055579
  • 694
  • 11
  • 22

1 Answers1

0

The translate should also be adjusted for the scaled version:

div {
  position: absolute;
  width: 300px;
  height: 150px;
  border: 1px black solid;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
}

div:hover {
  transform: translate(-50%, -50%) scale(1.5);
}
<div>This is a div.</div>
junvar
  • 11,151
  • 2
  • 30
  • 46