Hidde text in the backface of a div that rotate

Viewed 62

I have a div element that rotate which contains a text but we see the text in the backface of this div:

div {
  position: absolute;
  background-color: lightgreen;
  height: 240px;
  width: 150px;
  border-radius: 3px;
  transform: rotate3d(-0.3, 1, 0, 30deg);
  animation: rotate 1s infinite;
}

@-webkit-keyframes rotate {
  100% {
    transform: rotate3d(-0.3, 1, 0, 390deg);
  }
}
<div>
  <p>
    A text...
  </p>
</div>

So I tried to use the backface-visibility css property on the div itself with hidden as value but then the entire background disappear:

div {
  position: absolute;
  background-color: lightgreen;
  height: 240px;
  width: 150px;
  border-radius: 3px;
  transform: rotate3d(-0.3, 1, 0, 30deg);
  animation: rotate 1s infinite;
  backface-visibility: hidden;
}

@-webkit-keyframes rotate {
  100% {
    transform: rotate3d(-0.3, 1, 0, 390deg);
  }
}
<div>
  <p>
    A text...
  </p>
</div>

So I tried to just use this property on the text itself by using:

div p {
  backface-visibility: hidden;
}

But nothing change and the text is still visible. So is there an simple way to hide this text section (maybe also without an :after element)?

2 Answers

I propose a solution without a wrapper in div and using pseudo-class :before. This pseudo-class will be the background of the back of the animated div.

And for div add rule transform-style: preserve-3d.

div {
    position: absolute;
    background-color: lightgreen;
    height: 240px;
    width: 150px;
    border-radius: 3px;
    transform: rotate3d(-0.3, 1, 0, 30deg);
    animation: rotate 1s infinite;
    transform-style: preserve-3d;
}

div:before {
    content: '';
    background-color: inherit;
    transform: translateZ(-1px);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

@-webkit-keyframes rotate {
    100% {
        transform: rotate3d(-0.3, 1, 0, 390deg);
    }
}
<div>
    <p>
        A text...
    </p>
</div>

Also, this solution will allow you to set any color for the back of the animated div. For example, set a specific color for background-color instead of the inherited color as in the first snippet.

div {
    position: absolute;
    background-color: lightgreen;
    height: 240px;
    width: 150px;
    border-radius: 3px;
    transform: rotate3d(-0.3, 1, 0, 30deg);
    animation: rotate 1s infinite;
    transform-style: preserve-3d;
}

div:before {
    content: '';
    background-color: grey;
    transform: translateZ(-1px);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

@-webkit-keyframes rotate {
    100% {
        transform: rotate3d(-0.3, 1, 0, 390deg);
    }
}
<div>
    <p>
        A text...
    </p>
</div>

Wrap it in other div

div {
  position: absolute;
  background-color: lightgreen;
  height: 240px;
  width: 150px;
  border-radius: 3px;
  transform: rotate3d(-0.3, 1, 0, 30deg);
  animation: rotate 1s infinite;
}
div>div {
  backface-visibility: hidden;
}

@-webkit-keyframes rotate {
  100% {
    transform: rotate3d(-0.3, 1, 0, 390deg);
  }
}
<div>
<div>
  <p>
    A text...
  </p>
  </div>
</div>

Related