-2

I am trying to set mutliple attributes to a form. I want the form to be centered and the background to be orange. But only the last attribute is executed. So in the code below the background color would be orange but the form would not be centered.

Here is my code:

<!DOCTYPE html>
<html>
 <body>

   <form id="form">
       First Name:<br>
       <input type="text" name="firstname">
         <br>
         Last name:<br>
         <input type="text" name="lastname">
       </form>
    <script>  alert("ahhhhhh");
     var form = document.getElementById("form");
     form.setAttribute("style", "background-color: orange");
     form.style.align = "center";
     form.style.backgroundColor = "orange";


    </script>
 </body>
</html>

Could someone tell me why this happens and how set mutliple attributes on one element? Thanks!

zach
  • 27
  • 7

2 Answers2

2

align is not a valid CSS property. You would need something like textAlign or margin set to 0 auto in this case

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
1

There is no CSS align property. That's why you think the code is not executed. You are probably trying to set the textAlign property.

form.style.textAlign = "center";

Note that styling elements using JavaScript is a bad practice as it makes the code less maintainable. If the styles should be applied conditionally via JavaScript you can add and remove CSS classes instead.

Ram
  • 143,282
  • 16
  • 168
  • 197