Jump to content

Align Div in the center...


wispas

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.