wispas Posted May 25, 2010 Share Posted May 25, 2010 Anyone know how to use javascript to align div in the center? Quote Link to comment Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 25, 2010 Share Posted May 25, 2010 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.