2

Hi can someone please tell me how to position an image in the centre of the screen using javascript?

Would I get the screen height and divide by 2? How would I get the screen height?

Thanks!

apaderno
  • 28,547
  • 16
  • 75
  • 90
Mr Cricket
  • 483
  • 3
  • 10
  • 14

4 Answers4

3

Centering with CSS:

http://www.bluerobot.com/web/css/center1.html

http://www.spanish-translator-services.com/espanol/t/007/center.html

http://simplebits.com/notebook/2004/09/08/centering/

Centering with javascript (jQuery):

Using jQuery to center a DIV on the screen

Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
  • No part of your answer addresses a pure javascript solution (you mention jquery, but the OP didn't ask for that). *However*, CSS is probably the best way to do this, and if javascript is being used jQuery is probably the best option. So +1 :) – Cam Aug 23 '10 at 04:42
  • 1
    The jQuery version is far more elegant than a pure Javascrip one, because all browser compatibility issues are already solved by jQuery. My advice is always use jQuery if possible. Thanks for the upvote. – Sebastián Grignoli Aug 23 '10 at 04:59
  • 1
    jQuery was the best option in 2010... Now in 2020 it's probably not the best advice. – Sebastián Grignoli Dec 16 '20 at 04:44
3

Really, CSS is the best way to do this (as per Sebastián's answer), and if you have to use JS then go for jQuery. However you asked for a javascript solution so you'll find one below.

Really the only two reaons I can see js being necessary are:

  1. If the image is to be centered as a result of user interaction or
  2. If the image has to be centered once, and then should remain static (instead of remaining centered, as would happen with a CSS solution).

Anyways... enjoy:

Usage:

imgToMiddle('imageid');

Note that 'imageid' is the id of the image you want to place in the screen's center. The function modifies the image's css properties to place it in the middle of the screen.

Code:

    //viewport width/height code from here: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

    function imgToMiddle(imgid){
        function viewportWidth(){
            var viewportwidth;

            if (typeof window.innerWidth != 'undefined'){
              viewportwidth = window.innerWidth;
            }

            else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
               viewportwidth = document.documentElement.clientWidth;
            }
            else{
               viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
            }
            return viewportwidth;
        }

        function viewportHeight(){
            var viewportheight;

            if (typeof window.innerWidth != 'undefined'){
              viewportheight = window.innerHeight;
            }

            else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
               viewportheight = document.documentElement.clientHeight;
            }
            else{
               viewportheight = document.getElementsByTagName('body')[0].clientHeight;
            }
            return viewportheight;
        }
        var img=document.getElementById(imgid);

        img.style.position="absolute";
        img.style.left=viewportWidth()/2-img.width/2;
        img.style.top=viewportHeight()/2-img.height/2;
    }
Cam
  • 14,930
  • 16
  • 77
  • 128
  • Your code works well, but I've modified it slightly, and will post it as one more answer, but with reference to your answer. – TARKUS Oct 23 '13 at 14:04
2

This is a modification of Cam's answer (which I got to work with some slight modification). This answer features a little more jQuery, and most importantly, the position:fixed, so that the resulting div will always be squarely in the middle of your viewport, no matter how far down or up you have to scroll.

function imgToMiddle(imgid){

    function viewportWidth(){
        var viewportwidth;

        if (typeof window.innerWidth != 'undefined'){
          viewportwidth = window.innerWidth;
        }

        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
           viewportwidth = document.documentElement.clientWidth;
        }
        else{
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
        }
        return viewportwidth;
    }

    function viewportHeight(){
        var viewportheight;

        if (typeof window.innerWidth != 'undefined'){
          viewportheight = window.innerHeight;
        }

        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
           viewportheight = document.documentElement.clientHeight;
        }
        else{
           viewportheight = document.getElementsByTagName('body')[0].clientHeight;
        }
        return viewportheight;
    }
    var img=document.getElementById(imgid);

    $(img).css("position","fixed");//note: position:"fixed" will keep the div exactly in the middle of your browser viewport. This can be useful as a modal dialog box.
    $(img).css("left",parseInt(viewportWidth() / 2) - 100 );//note: "100" is half the width of the target div.
    $(img).css("top",parseInt(viewportHeight() / 2) - 100 );//note: "100" is half the height of the target div.
}
TARKUS
  • 2,170
  • 5
  • 34
  • 52
0

Please refer to my following answer

Community
  • 1
  • 1
cetnar
  • 9,357
  • 2
  • 38
  • 45