Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
How to apply width ease-in-out transition to element?

Attempting to apply a transition to this image, I can't understand why this does not work and the width styling is applied instantly versus transitioning out from its current width as expected by the function.

<!DOCTYPE html>
<html>
<body>

<img style="" id="myImg" src="https://ae01.alicdn.com/kf/S9c1b79c0b59442d0b36ea6aea9cd266fO/Super-Cat-Bed-Warm-Sleeping-Cat-Nest-Soft-Long-Pluh-Best-Pet-Dog-Bed-For-Dogs.jpg.jpg" height="98">

<p>Click the button to change the width of the image to 300px.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  document.getElementById("myImg").style.display = "flex";
  document.getElementById("myImg").style.transition = "width 2s";
  document.getElementById("myImg").style.width = "300px";
}
</script>

</body>
</html>

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

The problem that you are having is that the attribute you are transitioning on, should be declared initially. No matter if its inline or in CSS.

So I changed your code with a random width instead of height, and it transitions now.

function myFunction() {
  document.getElementById("myImg").style.display = "flex";
  document.getElementById("myImg").style.transition = "width 2s";
  document.getElementById("myImg").style.width = "300px";
}
<img style="" id="myImg" src="https://ae01.alicdn.com/kf/S9c1b79c0b59442d0b36ea6aea9cd266fO/Super-Cat-Bed-Warm-Sleeping-Cat-Nest-Soft-Long-Pluh-Best-Pet-Dog-Bed-For-Dogs.jpg.jpg" width="105">

<p>Click the button to change the width of the image to 300px.</p>

<button onclick="myFunction()">Try it</button>

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda