2

* {margin: 0;}
   
div {
  background-color:#bad455;
  width: 200px;
  height: 200px;
  top:50%;
  left:50%;
  position:fixed;
  transform: translate(-50%, -50%);
  text-align:center;
}

div:hover {
  box-shadow: 3px 5px 50px 5px;
  display:inline-block;
  border-radius:10%;
  transform:skewX(50deg);
}
div p {
  top:50%;
  position:relative;
}
<div>
  <p>oooo</p>
</div>

When I did top:50% and set the position:relative , I was expecting this text to go in the middle. But now I can see it isn't exactly in the middle of it. If someone could explain I would be grateful!

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
edga9966
  • 77
  • 1
  • 2
  • 8

2 Answers2

2

Use transform: translateY(-50%); to center it correctly

* {
  margin: 0;
}

div {
  background-color: #bad455;
  width: 200px;
  height: 200px;
  top: 50%;
  left: 50%;
  position: fixed;
  transform: translate(-50%, -50%);
  text-align: center;
}

div:hover {
  box-shadow: 3px 5px 50px 5px;
  display: inline-block;
  border-radius: 10%;
  transform: skewX(50deg);
}

div p {
  top: 50%;
  transform: translateY(-50%);
  position: relative;
}
<div>
  <p>oooo</p>
</div>
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35
0

Another way would be to use flexbox (CSS3). So adding

display: flex;
align-items: center;
justify-content: center;

To div centers the text.

* {margin: 0;}
   
div {
  background-color:#bad455;
  width: 200px;
  height: 200px;
  top:50%;
  left:50%;
  position:fixed;
  transform: translate(-50%, -50%);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

div:hover {
  box-shadow: 3px 5px 50px 5px;
  display:inline-block;
  border-radius:10%;
  transform:skewX(50deg);
}
<div>
  <p>oooo</p>
</div>

See this discussion for comparison

IiroP
  • 1,102
  • 2
  • 10
  • 14