Place the Javascript pop-up in the center of the screen

Hey there! I was able to create a popup on my screen but it's currently positioned in the center of the page instead of the screen itself.

Is there a way for me to make sure it appears in the center of the screen and not just the page?

I'm using a dual-screen setup but viewing the page in a single view mode.

CSS /STYLES FOR CSS POPUP/


    #blanket {
        background-color: #111;
        opacity: 0.60;
        *background: none;
        position: absolute;
        z-index: 9001;
        top: 0px;
        left: 0px;
        width: 100%;
    }

    #popUpDiv {
        position: absolute;
        background-color: #dddddd;
         opacity: 0.90;
        width: 400px;
        height: 300px;
        border: 5px solid #000;
        z-index: 9002;
        border: 2px solid #a1a1a1;
        border-radius: 10px;
        -webkit-box-shadow: 1px 2px 21px 6px rgba(0,0,0,0.75);
        -moz-box-shadow: 1px 2px 21px 6px rgba(0,0,0,0.75);
        box-shadow: 1px 2px 21px 6px rgba(0,0,0,0.75);
    }      
</style>
    

Javascript


     <script>
         function toggle(div_id) {
             var el = document.getElementById(div_id);
             if (el.style.display == 'none') { el.style.display = 'block'; }
             else { el.style.display = 'none'; }
         }
         function blanket_size(popUpDivVar) {
             if (typeof window.innerWidth != 'undefined') {
                 viewportheight = window.innerHeight;
             } else {
                 viewportheight = document.documentElement.clientHeight;
             }
             // More code here
         }
        
     </script>
    

Part of HTML


       <div id="popUpDiv" style="display: none;">
           <div style="float: right; text-align: right;">
               <center><p style="color:black;z-index: 9003!important; font-size: x-large;"> <b>Price Change</b> </center>
                    
                // More HTML code here
                    
            </div>

        </div>
        <a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a>
        <!-- / POPUP-->


        <!-- end #mainContent -->

    </div>
    

Thank you!

Answer №1

#centeredDiv {
  position: fixed;

  width: 400px;
  height: 300px;

  left: 50%;
  top: 50%;

  margin-top: -150px;
  margin-left: -200px;

}

This code will center a div with fixed width and height.

Check out an example here: http://codepen.io/ryanjgill/pen/gbPZev

Answer №2

Using pseudocode: set the positioning of a div element with div.style.top = (window.height - div.height) / 2 + window.scrollTop

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some methods for utilizing the data received through this.props.history?

Whenever I need to navigate from one route to another using this.props.history.push("/"), I also want to pass along some extra data. For example: this.props.history.push('/postDetail', {data: item}). However, I am uncertain about where to define ...

The application appears differently on a phone compared to how it looks on the ripple platform

Recently started experimenting with Apache Cordova, attempting to create a basic application. Here is how it looks in Google Chrome using Ripple for PhoneGap/Cordova emulation: https://i.sstatic.net/2MSwl.png When I click on the button, this is the resul ...

Exploring the power of Node.js and EJS through the art of

Recently delving into the world of node.js, I encountered a puzzling issue with my EJS template. It seems that my for loop is not executing properly within the EJS template while attempting to create a basic todo app. Below is the structure of the project ...

Passing JSON data from a Ruby controller to JavaScript in Rails 6.1 - Tips and Tricks

I am attempting to transfer JSON data from my database to the front-end in order to manipulate it using JavaScript. Initially, I created some temporary JSON data in my home.html.erb file like this: <%= javascript_tag do %> window.testJSON_data = ...

Promise - rapidly access data from a function that resolves a Promise

Could someone suggest a technique for instantly extracting data from a function that returns a Promise? For example, in my simplified scenario, I have an AJAX preloader like this: loadPage("index.html").then(displayPage); If the page being loaded is lar ...

How to toggle classes on specific items generated with Vue JS's v-for directive

I have a list rendering using the v-for directive in Vue.js. <li v-for="group in groupList" :key="group.id" @dragenter="toggleClass ...."@dragleave="toggleClass ...." > Content </li> My goal is to apply a class to the li el ...

The modal window pops up immediately upon the first click

Experience a dynamic modal element that springs to life with just the click of a button or an image. The magic lies in the combination of HTML, CSS, and jQuery code: <div id="modal-1" class="modal"> <div class="button modal-button" data-butto ...

Tips for adjusting the width of a div to accommodate word wrapping

Can a div element be resized when word wrap occurs? To illustrate, take a look at this example from a similar query: .mypost { border: 1px solid Peru; margin: auto; min-width: 200px; display: inline-block; background: red; } .test { margin: ...

What is the proper technique for looping through an array with an unknown level of nesting?

As I delve into the world of destructuring, there's a specific challenge that has arisen. I'm struggling with how to dynamically display an array of objects depending on their contents... const items={ title: 'Production', subTasks: ...

I attempted to write an AngularJS code, however, it seems to be malfunctioning

Here is a snippet from my controller.js file: angular.module("ngcribs").controller("cribsController", function($scope) { $scope.message = "Hello, world!"; }); And here is a section of my index.html file: <!DOCTYPE html> <html lang="en"&g ...

What is the best way to retrieve a message from a resource file using JavaScript?

I have both English and Japanese resource files. When my website switches to Japanese language, I need the messages to display in Japanese just like they do in English. This will require dynamically displaying messages in JavaScript. Thank you. ...

Token Fields malfunctioning

I attempted to use a sample from bootstrap, but I am not having any luck with it. Is there anyone who can assist me in resolving this issue and understanding it better? Code Snippet: <input type="text" class="form-control" id="tokenfield" value="red, ...

Differences between variable scope in Node.js and web browsers when running JavaScript code

When it comes to browser, different JavaScript files share one scope: a.js: var a=1; //by adding "var", we prevent it from becoming a global variable. b.js: console.log(a) //even though a=1, b.js can still access this variable! In Node.js: a.js .... b ...

Unable to substitute a sub-string containing brackets in a string array using TypeScript/JavaScript

So I have this string that says: My column name is Start Date (GMT). What I want to do is replace the portion "Start Date (GMT)" with "start_date". I've been attempting to achieve this using the following code: let a= "My column name is Start D ...

Unlocking numerical input solely by executing the specified command within the most up-to-date iteration of the Chrome Extension of Selenium IDE

After executing the command in Selenium IDE, I successfully extracted the sentence, "Your booking ID is 1234", and saved it in a variable named myText. <table> <tr> <th>Command</th> <th>Target</th> < ...

If the URL matches a specific path, then append a parameter

I've created a script that adds a parameter to a URL based on specific subfolders. For example, if the URL contains /de, it will add ?_sft_language=german. However, I'm encountering an issue where the code is running multiple times instead of jus ...

Change the background color of the selectInput component in Shiny to fit your

Take a look at this sample shiny app: library(shiny) ui <- fluidPage(tags$head(includeCSS("www/mycss.css")), selectInput("foo", "Choose", width = '20%', multiple = F, selected = "red1", choices = list(red = c ...

The Combobox event does not trigger properly on a change

My issue arises when trying to display the JDatePicker of jQuery UI in a different format upon an onchange event in a Combobox. The code I have does not work as expected when the second onchange event is triggered. Javascript Code $(document).ready(funct ...

Implementing JavaScript functionality to read and interact with a Boolean field in a

Within my models.py file, I've defined the following: urgency = models.BooleanField(blank=True, default=False, verbose_name="Hitno otklanjanje") I am attempting to execute a javascript function that will modify a text field based on whether ...

Substitute the main node with the subordinate node within a json file

Here is a JSON object structure: { "coach": { "Errors": { "items": [ { "errorMessage": "You must select a date that is in the future.", "errorBOPath": "twl.date" ...