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

In Bootstrap 4, the positioning of :after is different compared to Bootstrap 3

Our bootstrap nav-tabs have a unique default look with an 'indicator' icon below the selected tab. This is achieved by styling the active anchor element as follows: https://i.sstatic.net/UQos5.png However, when migrating to bootstrap 4, we noti ...

css - Showcasing images with a horizontal scrollbar

I'm having an issue with displaying multiple images in a fixed-width div horizontally. I want to have a horizontal scroll bar for the images that exceed the width of the div. Currently, the images are appearing vertically instead of side-by-side. Is ...

Infinite loop using jQuery function endlessly

I'm trying to create a jQuery Animation that loops indefinitely, but my current code doesn't seem to be working. Here's what I have: $(document).ready(function() { var i = 0; document.write(i); function runAnimatio ...

Error: The Google Translate key is not found in the Node.js AJAX request

I have a basic Node.js script that functions properly when executed locally in the terminal: exports.google_translate = function (translate_text, res) { var Translate = require('@google-cloud/translate'); var translate = new Trans ...

Steps for adding an npm dependency as a peer dependency:

Is there a way in npm to install dependencies as peer-dependencies similar to the yarn option --yarn, rather than manually adding them? For instance: "peerDependencies": { "@angular/core": "^7.0.0" } Update: Further clarifi ...

How should one go about creating an npm package out of a vuejs component and testing it locally?

Initially, I created a vuejs project as a test container using vue-cli. Next, I developed an npm package named "vue-npm-example" from a Vuejs component in my local environment and then imported it into the aforementioned testing project. Within the packag ...

The jQuery .ajax() function encountered a 405 error stating "Method Not Allowed" when attempting a cross

Despite searching extensively on SO, I am unable to pinpoint the issue in my code. To avoid using JSONP, I am implementing CORS. I understand that this is a preflighted request and believe I have included the correct headers. The problem lies in the web ...

Tips for updating JS and CSS links for static site deployment

As I develop my static site using HTML files served from a simple web server, I want to use local, non-minified versions of Javascript code and CSS files. However, when it comes time to deploy the site, I would like to refer to minified versions of these s ...

Is there a way to ensure that a method is always called in JavaScript after JQuery has finished loading

We recently integrated jquery load into our website to dynamically load content into a div without refreshing the whole page. However, we've noticed that in the complete function, we have to constantly re-apply various bindings. Is there a way to set ...

Visualization of pie charts in Angular2 using Google library

Currently, I am in the process of building a web application using Angular2 that includes a Google pie chart. One of the components of the application is a Directive specifically designed for the pie chart. Below is the code snippet for the Directive: @D ...

Differences in vertical font positioning between Mac and Windows systems

One element on my page is a quantity balloon for a basket, but I'm struggling to get it looking right. The font and vertical position don't seem to be cooperating for such a simple element: Despite trying solutions from stackoverflow (like setti ...

What is the process for attaching an on-click event listener by utilizing an argument that is passed to a function in JavaScript?

These are the functions I have created: function type(message) { document.getElementById("body").innerHTML="<div id=\"textbox\" class=\"textbox\"></div><div id=\"oc\&q ...

Arranging Bootstrap dropdown and buttons on a single line

Check out my code snippet here: http://jsfiddle.net/52VtD/7462/ I'm facing an issue where I want to align my dropdown and buttons on the same line, but the dropdown being a div, it stays on the line below. How can I ensure they are on the same line? ...

Discover the parent DOM element of a specified element using jQuery

When using jQuery, I am currently exploring methods to navigate through the DOM until it locates a specific selector. For instance: Consider this structure: <span data-type="contact" data-filter="4" id="buyer-lookup" class="uneditable-input contact-lo ...

The primeVue menubar's active/focused item highlighting feature is not functioning correctly

Currently, we are in the process of developing an electron-based application with the front end primarily coded using Vue.js and primeVue. As a novice, I am encountering issues with the menubar component from primeVue. The problem is that no matter which i ...

Using npm as a build tool to merge files together

Upon discovering the flexibility of using npm as a task runner instead of gulp or grunt, I have successfully implemented various tasks such as linting, stylus processing, jade compilation, uglifying, and even watching files. However, I am facing difficulti ...

Utilize jQuery autocomplete to limit user input in a text box

I'm currently implementing jQuery UI's autocomplete feature to populate a form input with accurate data from a list of over 1000 values. The autocomplete functionality is functioning correctly, but I am unsure how to restrict the field's va ...

ms-card malfunctioning due to data issues

I'm facing difficulties in transferring the data to the template. Although I can access the data in HTML using vm.maquinas and maquina, I am unable to pass it to the TEMPLATE through ng-model. Information about ms-cards was not abundant. Module ang ...

Avoid displaying identical items when rendering a page from JSON data

I am using ajax and json to render a page. The structure of my json is as follows: {"status":"ok","rewards":[{"id":201,"points":500},{"id":202,"points":500}]}. I want to load the data using ajax only once if 'points' have duplicates in any of the ...

challenges arise when using star icons to rate items visually, as the corresponding hidden values for backend processing are linked to radio input types

Hello, I am trying to retrieve the value of an input type radio by clicking on a star image visually. I have been successful in this, but I am having trouble resetting the star image in another row. You can view a demo of this here. I will explain what I ...