0

I want to change my h1 margin through javascript but its not working

my code:

<script>
  document.getElementById("h1").style.marginLeft = "250px";
</script>

<h1 id="h1" style="position: absolute; text-align: center; top: 100px;  color: #E95928;  font-family:Helvetica; font-weight: bold; font-size:6vw; ">Shop till<br>you drop</h1>
tacoshy
  • 10,642
  • 5
  • 17
  • 34
  • 1
    you also need to add a trigger to actually execute the script. PS: Pay attention of the description fo the `tags` you are using -> `script: DON'T USE THIS TAG! Every SO question is about scripts one way or another` – tacoshy May 26 '21 at 20:31
  • Off topic: The correct abbreviation of `until` is `'til`, or just `til`. Otherwise you're working in your garden. – isherwood May 26 '21 at 20:39

1 Answers1

5

You can see that your function does work if it is placed after the element it acts upon.

h1 {
  position: absolute;
  text-align: center;
  top: 10px;
  color: #E95928;
  font-family: Helvetica;
  font-weight: bold;
  font-size: 6vw;
}
<h1 id="h1">Shop 'til<br>you drop</h1>

<script>
  document.getElementById("h1").style.marginLeft = "250px";
</script>
isherwood
  • 58,414
  • 16
  • 114
  • 157