Hack Forums
Centering Javascript Images HELP! :) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: Centering Javascript Images HELP! :) (/showthread.php?tid=3079978)



Centering Javascript Images HELP! :) - Pernicious - Dec 8, 2012

I have this code, and just want to center the image objects being displayed from my arrays.

PHP Code

<?php 
    
function redeal(){

        var 
centered = document.createElement('div');
        
centered.className = 'centered';

            for(
x=0;x<playerarray.length;x++){
                
document.body.appendChild(playerarray[x].image);
            }

            
document.body.appendChild(layer); //this just adds a 'br', declared globally

            
for (x=0;x<opponentarray.length;x++) {
                
document.body.appendChild(opponentarray[x].image);
            }    
    } 

And in the CSS I have:

PHP Code

<?php 
.centered {display: block; margin-left: auto; width: 500px; margin-right: auto } 

Just not sure how it works...or what it should look like..

Thanks Guys!


* - ifconfig - Dec 9, 2012

I'm not sure if I'm right here, as I hardly ever do webpage stuff, but couldn't you use HTML to center your image when you call the function? Sorry if I'm wrong, not my cup of tea.


* - rswinges - Dec 10, 2012

You want to center the images in the <body> or in the specific <div>?

If inside the div, ignore all the other CSS you have for class ".centered. Just set "text-align: center; width: 500px" for the class .centered and that should do it. A block by default has display: block.

If inside the body, you have to set "text-align: center" to the body, then "display: inline-block" for the div.