Jump to content

Recommended Posts

You would have to use getElementById and the Div would have to have a set id. Then you would need to call some element properties related to div, ie the "Style" tag or "class" tag and assign it to a class that aligns center or type the style manually.

 

IE:

var x = document.getElementById('dividhere');
x.style = 'align:center;';

 

Un tested and I am not a JS guy, I just know the theory here. You may have to do some reading on it to implement it but at least you now know where to look / what to look for.

The following will center a div horizontally, but not vertically.  That's harder to do, because you need to calculate the height of the containing element.

 

<!DOCUMENT html>
<html lang="en">
   <head>
      <title>Blah</title>
   </head>

   <body>
   </body>

   <script type="text/javascript">
      var oDiv = document.createElement('div');
      oDiv.style.margin = "auto";
      oDiv.style.border = "1px solid black";
      oDiv.style.width = "400px";
      oDiv.innerHTML = "Hi";

      var oBody = document.getElementsByTagName('body')[0];
      oBody.appendChild(oDiv);
   </script>

</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.