-1

I am trying to rotate a span created using JavaScript on a button click, the class gets added to the span but the rotate doesn't effect the span.

<p>Click the button to create a SPAN element.</p>

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

<script>
function myFunction() {
  var x = document.createElement("SPAN");
  var t = document.createTextNode("This is a span element.");
  x.appendChild(t);
  document.body.appendChild(x);
  x.setAttribute("id","firstPracPara");
}
function myFunction2() {
var element = document.getElementById("firstPracPara");
element.classList.add("rotate");
}
</script>

<style>
.rotate{
transform: rotate(20deg);
}
</style>
Asn
  • 113
  • 6
  • Does this answer your question? [How can I use CSS3 transform on a span?](https://stackoverflow.com/questions/24961795/how-can-i-use-css3-transform-on-a-span) – Parsa Arvaneh May 16 '22 at 12:59

1 Answers1

1

With your current code it is impossible because a span is a inline element.

If you want to complete what you asking you need to put display: block on your span element.

<style>
span { display: block };
.rotate{
transform: rotate(20deg);
}
</style>```
KolaCaine
  • 2,037
  • 5
  • 19
  • 31