Retrieve the width of an element once the browser has finalized its dimensions

I am facing an issue with centering a pop-up box perfectly within the window using CSS properties. The code for the pop-up box styling is as follows:

#pop_up {
    position: fixed;
    display: inline-block;
    border: 2px solid green;
    box-shadow: 0px 0px 8px 5px white;
    width: -webkit-max-content;
    width: -moz-max-content;
    width: max-content;
    border-radius: 5px;
    background-color: grey;
}

Additionally, I have implemented some jQuery functions to set the left and top properties of this element.

$(document).ready(function() {
    window_height = $(window).height();
    window_width = $(window).width();
    pop_up_height = $('#pop_up').outerHeight();
    pop_up_width = $('#pop_up').outerWidth();
    pop_up_left = (window_width / 2) - (pop_up_width / 2);
    pop_up_top = (window_height / 2) - (pop_up_height / 2);
    $('#pop_up').css('left', pop_up_left);
    $('#pop_up').css('top', pop_up_top);
});

During testing, I noticed that the variables for pop_up_height and pop_up_width were indicating values of '4', which indicates it's only capturing the border. Changing it to .innerHeight(); and .innerWidth(); returns '0'. This suggests that it retrieves the width before the browser applies the width: max-content; property adjustment. I am seeking a solution to account for the width after this browser rendering process.

Furthermore, when setting a left value for positioning, does it reference the border or the actual interior of the element? In other words, if an element has a 2px border and is positioned at left: 20px, will the distance be from the border or the inner content edge?

Answer №1

function centerPopUp()
{
  $('#pop_up').css({'top':($(window).height()-$('#pop_up').height())/2,'left':($(window).width()-$('#pop_up').width())/2});
}

$(document).ready(function() {
 centerPopUp();
 })

$(window).resize(function() { //adjust the position of pop-up element to keep it centered on window resize;
 centerPopUp(); 
 })

Answer №2

The border size will not be 20 pixels. The element will remain aligned to the left.

Answer №3

For retrieving the width of an element with ID "pop_up", you can utilize $("#pop_up").width(); and .height() as well.

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

Do we always need to incorporate components in Vue.js, even if there are no plans for reuse?

I've been pondering this question for some time now: is it necessary for every component to be reusable? Consider a scenario where we have HTML, CSS, and JavaScript that cannot be reused. For instance, a CRUD table designed specifically for managing u ...

Performing an HTTP GET Request in Node.js or Express.js

Currently, I am working with express.js and require assistance in making an HTTP GET request to retrieve JSON data. Can anyone recommend some reliable node js/express js modules or libraries that can help me with performing get/post requests? ...

Adjust the width of the dropdown menu in Twitter Bootstrap's typeahead feature depending on the search

Not entirely certain if this issue is related to jQuery, but there's a strong suspicion that it might be. The problem at hand can best be described by the picture provided: The query output exceeds the width and doesn't resize or scale according ...

I want to trigger the opening and closing of an accordion by clicking on an arrow

I am encountering an issue with the Material UI accordion. When I click on the arrow, the accordion opens but clicking again does not close it. I would like to make it so that when the user clicks on the arrow, the accordion will toggle between open and cl ...

CSS: Position an element based on the size of the screen

I'm currently developing a program that will dynamically resize elements based on the viewer's screen size. The program is being built using JSP/SQL/XHTML/CSS, and I have a couple of queries. Is there a way to select a CSS file by storing the sc ...

What methods and technologies are accessible for executing JavaScript through PHP?

Are there any frameworks or tools available to execute JavaScript from PHP? Is there a similar project like the Harmony project for PHP? I am interested in running JS unit tests (or even BDD) directly from PHP, as demonstrated in this post (for Ruby). Am ...

Combining package.json commands for launching both an Express server and a Vue app

I recently developed an application using Vue.js and express.js. As of now, I find myself having to open two separate terminal windows in order to run npm run serve in one and npm start in the other. My ultimate goal is to streamline this process and have ...

Exploring the possibility of utilizing the talks.js library to develop a chat feature within a React application

I'm currently working on integrating the talks.js library to set up a chat feature in my React project. I've followed all the instructions provided at , but unfortunately, it's not functioning as expected. I'm not quite sure what I migh ...

Continuously converting methods recursively until the array is fully processed

My current code has a method that is not very efficient and does not scale well. The object y is an array consisting of key/value pairs, each containing two properties: 1. A unique string property called name. This value is identified by the childre ...

Is there a way to prevent the slide-out bar from automatically hiding when selecting an item from its list?

I am facing an issue with my dashboard that has a slideout sidebar. Whenever I press the toggle button, the sidebar slides out as expected. However, when I click on any tab within the sidebar, it hides again. I only want it to hide when toggled. My code is ...

What is the best way to iterate through a JSON file?

Looking at my JSON file: { "stats": { "operators": { "recruit1": { "won": 100, "lost": 50, "timePlayed": 1000 }, "recruit2": { "won": 200, ...

What's the best way to determine which of the two forms has been submitted in Django?

On my homepage, I have both a log_in and sign_up form. Initially, the log_in form is displayed by default, but when a user clicks on the Sign Up button, the sign_up form appears. These toggles switch depending on which button the user clicks. from django ...

What is causing the malfunction with this JQuery/AJAX request?

Currently in the process of setting up an autocomplete feature. Following the guidance from php academy at the moment. My goal is to display "suggestions go here" below the input field whenever something is typed in. I have two files for this task: home.ph ...

Problem Encountered When Using jQuery addClass on Associated Element

In my code, clicking on an 'a.mainSubMenuLinkA' element successfully triggers a page change. After this, I need to apply the 'active' class to the previously clicked 'a.mainSubMenuHeadingLinkA'. <li><a class="mainSub ...

What is the best way to import JSON functionality into Javascript specifically for use with Internet Explorer

I am facing an issue with loading JSON feature in JavaScript for IE8 while in compatibility mode. After receiving advice, I decided to utilize douglascrockford/JSON-js to enable JSON support on outdated browsers. To tackle this problem, I created a new f ...

The code "Grunt server" was not recognized as a valid command in the

I recently set up Grunt in my project directory with the following command: npm install grunt However, when I tried to run Grunt server in my project directory, it returned a "command not found" error. Raj$ grunt server -bash: grunt: command not found ...

Is it possible to incorporate a jQuery widget within a table?

Can the jQuery sortable widget be utilized within a table, or is it designed to function separately? Here is the link to the jQuery widget for reference - http://jqueryui.com/sortable/#default ...

Script - Retrieve the content of the code element

I am currently developing an extension for VS Code that will enhance Skript syntax support. One challenge I am facing is the inability to select the body of the code block. Skript syntax includes various blocks such as commands, functions, and events, eac ...

JQuery continuously refreshing the page causes browser memory overload

I've run into an issue with my jQuery code that fetches a generated HTML page every second, eventually causing an out-of-memory error. How can I address this problem? $(document).ready(function() { setInterval(function() { $.g ...

Having difficulty extracting data from certain tables with Beautiful Soup

I have been utilizing BeautifulSoup for data scraping from the website here. Although there are a total of 9 tables with the same class name, I am only able to extract information from 5 of them. What modifications should I implement in my code to ensure t ...