Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How to prevent a rotated div from moving when changing its width?

I have a reproduction of the issue I'm facing below:

.rect {
  transform: rotateZ(45deg);
  background-color: #ddd;
  position: absolute;

  width: 300px;
  height: 200px;

  animation: 3s linear 0s infinite normal none running widen;
}

@keyframes widen {
  from { width: 300px; }
  to { width: 500px; }
}
<div class="rect"></div>

Notice how as the rectangle gets wider, its position also changes. It is true that the element's programmatic position isn't changing, but visually on the screen it appears it is.

I am only looking to widen it without having it visually appear to move. That is, the right side of the rectangle should simply extrude out an additional 200 pixels. Is this possible?

Looking around, it seems like setting the transform-origin from center to top left accomplishes this effect, but unfortunately my rectangle's origin of rotation is around its center. Perhaps the solution is to temporarily set it in some way while the effect is occurring..

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

An easy way to do this is to wrap the grey rectangle div in another div that does the rotating. That way the width and rotation can be controlled independently.

If other transformations need to be applied as well, this approach gives you control over the order the transformations will be applied.

.rect {
  transform: rotateZ(45deg);
}

.widen {
  background-color: #ddd;
  width: 300px;
  height: 200px;
  animation: 3s linear 0s infinite normal none running widen;
}

@keyframes widen {
  from {
    width: 300px;
  }
  to {
    width: 500px;
  }
}
<div class="rect">
  <div class="widen" />
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

That squish effect is expected, but for this scenario I'd suggest not animating the size properties anyway since it's going to push your natural DOM flow around with if there's other elements. Instead try scale transform, see below.

.rect {
  transform: rotate(45deg);
  background-color: #ddd;
  position: absolute;

  width: 300px;
  height: 200px;

  transform-origin: center left;
  animation: 3s linear widen infinite;
}

/*
@keyframes widen {
  from { width: 300px; }
  to { width: 500px; }
}
*/

@keyframes widen {
  to { transform: rotate(45deg) scaleX(2);
}
<div class="rect"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

It looks like it is moving because of the angle. If you want it to grow proportionally, add height to the animation.

Example 1:

.rect {
  transform: rotateZ(45deg);
  background-color: #ddd;
  position: relative;

  width: 300px;
  height: 200px;

  animation: 3s linear 0s infinite normal none running widen;
}

@keyframes widen {
  from { width: 300px; height: 200px; }
  to { width: 500px; height: 400px; }
}
<div class="rect"></div>

If we remove the rotation, it won't look like it is moving.

Example 2:

.rect {
  background-color: #ddd;
  position: relative;

  width: 300px;
  height: 200px;

  animation: 3s linear 0s infinite normal none running widen;
}

@keyframes widen {
  from { width: 300px; }
  to { width: 500px; }
}
<div class="rect"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda