Attempting to retrieve div measurements in vh/vw units rather than pixels

I'm currently working on an app where I want the divs to be resizable. In order to achieve this, I need to store the user's changes in my database.

My main concern is how can I convert the units provided in pixels (px) to a different unit of measurement? For instance, when I use:

console.log(event.target.style.width)

it displays the width in px. Is there a way to retrieve it in, let's say, %, vh, or vw instead?

Answer №1

Let's try this:

(event.target.offsetWidth / window.innerWidth) * 100

To calculate the percentage width of an element, divide its actual width by the total window width and then multiply by 100.

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

Express js is unable to retrieve the page at /page

I'm facing a persistent routing error on express.js. Despite multiple attempts to fix it by referring to the documentation and making changes, I'm still stuck. Here's the basic content of the link pointing to the '/sell' route: i ...

What is the best way to avoid having identical entries in a row when using jQuery's multiple select feature?

I am facing the challenge of working with multiple rows containing select boxes that have similar content. Each row has several select boxes and each item in a row can only be used once due to specific classifications assigned to each column. I want the se ...

How can you modify a hyperlink URL before it is activated?

I'm facing an issue with a URL structure that needs updating before it is clicked. The current URL looks like this: https://url.com/a b c which is causing redirection problems because the actual link has underscores instead of spaces in its URL. Is ...

What is the best way to send props to a styled component without needing to convert them to transient props beforehand

Recently, I designed a custom Text component that accepts several props. These props are then forwarded to the styled component where specific styles are applied. However, I am facing an issue where I do not want these props to be passed down to the DOM, b ...

An error has been caught in the Angular JS application while using the highcharts-ng module: [$injector:modulerr]

I'm encountering an issue while trying to integrate Highcharts into my Angular/Node/Express application. I am utilizing the highcharts-ng directive available at https://github.com/pablojim/highcharts-ng Every time I run my app, I consistently receive ...

Passing data from the server to the HTML page for manipulation

I need assistance in retrieving and manipulating the value stored in the variable $result[] from a PHP file to my HTML file for further processing. Can you provide guidance or reference code on how to return data from server side to client side? Here is a ...

The latest update of next.sj includes the enhanced GTM version 12.3 feature

https://i.stack.imgur.com/z8cYT.pngI successfully integrated GTM into Next.js (version 12.3.0) by adding it to the _app.tsx file using the Next.js Script tag and the _document.tsx body tag. _app.tsx import Script from "next/scri ...

Compass in Three.js that responds to the rotation of the camera

I have been inspired to create a compass similar to the one featured on Facebook. Take a look at the image here. I have the x, y, and z rotations of the camera and I want to use them to rotate the FOV (the central slice of the compass) in a 2D circle view ...

Consistently encountering the error message "(0 , _reactDom.h) is not a function" or "h is not defined"

I'm currently in the process of developing an app that makes use of electron, react, redux, and several other technologies. At the moment, I have included electron, react, electron-compile, and babel in the project. Redux is installed but has not been ...

Is @babel the solution to transpile Array.prototype.flat?

I accidentally caused a backward compatibility issue in my React application by utilizing Array.prototype.flat. I was quite surprised that this problem persisted even after transpiling - I had assumed it would generate es2015-compatible code. Is there a w ...

Removing unexpected keys during validation using Joi

Within my server-side JavaScript code, I am utilizing Joi for validating a JavaScript object. The schema being used is structured as follows: var schema = Joi.object().keys({ displayName: Joi.string().required(), email: Joi.string().email(), e ...

Encountered node-gyp build error during npm install in React.js project

https://i.sstatic.net/I31OP.pngI encountered a frontend project repository built on reactjs, and when I tried to run the npm i command, it threw a bunch of errors. Firstly, I attempted to delete the package.lock.json file and then rerun the command. Next, ...

Tips for achieving JSON formatting with JavaScript or jQuery

To achieve the desired output format, I am required to transform the provided input json. input json: [ { "key a": "value alpha" "key b": "value beta" "key c": "value gamma& ...

Managing Time Before Browser Refresh in AngularLearn how to monitor and examine the time remaining before

In my Angular web application, I am facing an issue where the user's login status is lost every time the browser is refreshed or reloaded. I want to implement a feature that allows the login status to remain active for a short period of time after the ...

Capturing authorization issues in Firebase using ngrx effects

I am currently working on an angular5/firebase/ngrx project and I'm encountering an issue when trying to handle errors during the signup process. When I attempt to submit the signup form with an email that already exists in the system, I receive the ...

Step-by-step guide on integrating a custom JS file into an Angular component

Trying to grasp Angular, I embarked on adding some JavaScript logic to one of my components from a separate JS file. While following advice from a similar query (How to add custom js file to angular component like css file), it seems I missed something cru ...

What is the best way to achieve rounded table borders without duplicating them or relying on border-collapse?

After discovering a helpful Stack Overflow answer regarding rounding table corners, I attempted to implement the provided CSS code: /* table { border-collapse: collapse; }*/ table, tr, td, th{ border: 1px solid; text-align: center; /*paddin ...

Rearrange the position of the package.json file to the parent directory within the react application

Recently, I began learning React and used npx create-react-app frontend to set up my React app. This gave me the following directory structure: -- demo -- frontend -- node_modules -- public -- src -- .gitignore - ...

Tips for extracting a specific segment from a URL string

Take a look at the outcome of the console.log below: console.log('subscribe:', event.url); "https://hooks.stripe.com/adapter/ideal/redirect/complete/src_1E2lmZHazFCzVZTmhYOsoZbg/src_client_secret_EVnN8bitF0wDIe6XGcZTThYZ?success=true" I need to ...

Ways to utilize the ::after pseudo class with ElementRef within Angular

Is it possible to manipulate the background-color of the ::after pseudo selector for the specified element using Angular? @ViewChild('infobubble', { read: ElementRef }) infoBubbleElement: ElementRef; ngAfterViewInit(): void { const el ...