The Wayback Machine - https://web.archive.org/web/20211123192236/https://www.geeksforgeeks.org/how-to-center-a-popup-window-on-screen/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to center a popup window on screen?

  • Last Updated : 30 May, 2019

JavaScript window.open() method is used to open a popup window. This popup window will place into the center of the screen.

  • popupWinHeight: The height of pop up window on the screen.
  • popupWinWidth: The width of pop up window on the screen.

Example 1: This example creates the pop up window without placing it into center.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




<!DOCTYPE html>
<html>
      
<head>  
    <title>  
        Non-centerd popup window
        on the screen
    </title> 
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <p>
        Non-centerd popup window
        on the screen
    </p>
      
    <script>
        function createPopupWin(pageURL, pageTitle,
                    popupWinWidth, popupWinHeight) {
            var left = (screen.width ) ;
            var top = (screen.height ) ;
            var myWindow = window.open(pageURL, pageTitle, 
                    'resizable=yes, width=' + popupWinWidth
                    + ', height=' + popupWinHeight + ', top='
                    + top + ', left=' + left);
        }
    </script>
      
    <button onclick = "createPopupWin('https://www.geeksforgeeks.org',
            'GeeksforGeeks Website', 1200, 650);">
        GeeksforGeeks
    </button>
</body>
  
</html>

Output:

  • Before Clicking the button:
  • After Clicking the button:

Center popup window: To center the popup window we are changing the values of parameters of open() method as follows:

  • left = (screen.width – popupWinWidth) / 2
  • top = (screen.height – popupWinHeight) / 4

Example 2: This example creates the pop up window and placing it into center.




<!DOCTYPE html>
<html>
      
<head>  
    <title>  
        Centerd popup window
        on the screen
    </title> 
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <p>
        Centerd popup window
        on the screen
    </p>
      
    <script>
        function createPopupWin(pageURL, pageTitle,
                    popupWinWidth, popupWinHeight) {
            var left = (screen.width - popupWinWidth) / 2;
            var top = (screen.height - popupWinHeight) / 4;
              
            var myWindow = window.open(pageURL, pageTitle, 
                    'resizable=yes, width=' + popupWinWidth
                    + ', height=' + popupWinHeight + ', top='
                    + top + ', left=' + left);
        }
    </script>
      
    <button onclick = "createPopupWin('https://www.geeksforgeeks.org',
            'GeeksforGeeks Website', 1200, 650);">
        GeeksforGeeks
    </button>
</body>
  
</html>

Output:

  • Before Clicking the button:
  • After Clicking the button:



My Personal Notes arrow_drop_up
Recommended Articles
Page :