Ways to retrieve the bounding rectangle of an element PRIOR to applying a transformation?

Is there a way to obtain the dimensions and position of an element using Element.getBoundingClientRect(), but without taking into consideration any transformations applied through the CSS property transform? I am looking for a method that provides the rectangle values without the transformation effects.

In this scenario, the desired output should be 10 10 100 100.

const rect = div.getBoundingClientRect()
document.write(`${rect.left} ${rect.top} ${rect.width} ${rect.height}`)
body {
    margin: 10px;
}

div {
    background: red;
    width: 100px;
    height: 100px;
    transform: translate(1px, 1px) scale(0.5)
}
<div id="div"></div>

Answer №1

There is already a solution available, you can find more information on this topic in the post: How do I retrieve an HTML element's actual width and height?

Therefore, to obtain the original dimensions "before" any transformations, you need to adjust your code as shown below:

document.write(`${div.offsetLeft} ${div.offsetTop} ${div.offsetWidth} ${div.offsetHeight}`)
body {
    margin: 10px;
}

div {
    background: red;
    width: 100px;
    height: 100px;
    transform: translate(1px, 1px) scale(0.5)
}
<div id="div"></div>

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

Ways to incorporate react suspense with redux toolkit

I am currently using Redux toolkit to display data from an API, and I want to show the user a "loading data..." message or a spinner before displaying the city information. I have been attempting to use React Suspense, but it doesn't seem to be worki ...

How to dynamically insert elements into the HTML page using Angular

When my page first loads, it looks like this <body> <div class="col-md-12" id="dataPanes"> <div class="row dataPane"> Chunk of html elements </div> </div> <div class"col-md-12 text-right"> <input type="butt ...

Implement a new feature on a jQuery element

I'm currently exploring ways to incorporate a function into an object using jQuery, but unfortunately I am facing unexpected challenges. You can find a minimal example illustrating my issue on this jsfiddle page. I have recently started learning jQue ...

CSS: Adjusting color when hovering

I've been working on a website featuring clickable images, but I'm struggling with the ...:hover function. My goal is to have the image overlayed with a white color at 0.1 opacity upon hovering. <html> <head> <style> ...

The Hover Effect fails to function properly on Internet Explorer 6

I have successfully implemented a CSS hover effect that works perfectly in all browsers except for IE6. The issue arises when I add a link to the page - the hover effect functions as expected. However, if I remove the link, the hover effect stops working. ...

Angular JS Form's Pristine feature is malfunctioning when attempting to reset

I implemented a login form on my website. After submitting the form, I clear it and set it to Pristine mode. However, the error message still persists. Below is the code for my form: <form name="loginForm" ng-submit="loginForm.$valid && login( ...

Exploring the Use of data- Attributes in SVG Circle Elements

Looking for a way to dynamically update the color of a Circle element in your SVG when it is clicked? You can achieve this by using jQuery's .css() method in conjunction with the data-* attribute. CHECK OUT AN EXAMPLE: STYLING IN CSS svg { height ...

Are there any methods available to ensure that images maintain consistent quality on an HTML webpage without being stretched?

Within a card-group container, I am utilizing pre-built components from Bootstrap. The 'products' variable holds an array of JSON objects representing products for an online shopping store. <div class="card-group"> <% p ...

The initial connection displays an unsupported MIME type, but after refreshing the browser, everything functions correctly

Upon deploying the application on App Engine, the Chrome client initially encounters the following error. "Refused to apply style from 'https://tournypoker.wl.r.appspot.com/styles/styles.css' because its MIME type ('text/html') is ...

Matching a string literal with a hyphen in Express Router using Regex - How do I do it?

My dilemma involves two routes. When attempting to access the route test, it also matches with example-test due to the presence of a hyphen. Even after trying to escape it using \-, the issue persists. Is there a way to accurately match the exact rout ...

Div controls the margins of all other elements

I am encountering an issue when trying to position #slider, where the navigation and elements above it also move. It seems as though they are contained within #slider, even though they are not. HTML: <div id="slider" class="clearfix"> <d ...

Accordions housing Bootstrap 3 pills in a sleek design

I'm working on integrating pills and an accordion in Bootstrap 3. Everything seems to be functioning correctly, but there are a couple of issues I am encountering: When the accordion is open, clicking on another tab closes the accordion. I would li ...

The MUI Module is missing: Unable to locate '@emotion/react'

Attempted this solution, but unfortunately it did not work Currently using the latest version of Material-UI Error: Module not found: Can't resolve '@emotion/react' Let's try installing it then.. npm i @emotion/react @emotion/style ...

Using jQuery .sortable() to reorder list items and update their IDs based on their new positions

My apologies for my limited English skills, but here is what I am trying to achieve. I have a list structured like this: <ul id="sortable" class="sortable"> <li id="1">1</li> <li id="2">2</li> <li id="3">3</li> &l ...

The template failed to load on AngularJS post-compilation

Following the build of my Angular app, I encountered this error message: [$compile:tpload] Failed to load template: app/app.html (HTTP status: 404 Not Found). Seeking assistance from you all!!!!! app.html <div class="root"> <div ui-view> ...

The error occurred when trying to hydrate my custom stroke text component after the 14th attempt

I recently embarked on developing an app using Next 14, and I came across the need for text with a stroke. However, the CSS stroke feature was not behaving as expected, creating an inner stroke effect instead of an outer stroke. To address this, I devised ...

Despite following all the correct steps, the tailwind CLI remains unresponsive. Additionally, the default button fails to display any content

view the document and my packages (image) Although it worked with the cdn, I'm puzzled why I can't use it properly with the cli ...

Best Practices for Implementing the 960 CSS Clear Class

I'm puzzled by the necessity of using the clear class in the 960 css framework. Every time I remove a div with the clear class, everything seems to function properly. Can you explain to me the significance and purpose behind this class? ...

JavaScript: utilizing 'this' and onClick events

Recently, I created a basic slideshow with 4 images where upon clicking, the selected image displays in a larger div box. The functionality is quite straightforward, but if you have any queries, feel free to ask. As of now, with only 4 images in the slides ...

Managing messaging broadcasts for messenger bots by creating and retrieving unique identifiers

As a beginner using a starter project from glitch, I have a project set up at this link: I need help understanding how to obtain the message_broadcast_id and how to create it. This is how I usually create a normal message: function callSendAPI(messageDa ...