Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dimensions of element including drop shadow

This seems like a suspiciously straight-forward question but having searched StackOverflow and Google and used the usual tricks (getBoundingClientRect, clientWidth, offsetWidth) I've yet to find an answer.

Simply, is there a way to find the width/height of an element including not only border, padding etc, but also the shadow?

See: jsfiddle for an example of how everything returns the width of the element without the shadow.

EDIT: Ideally, I'd prefer not to have to investigate the CSS attribute for the shadow and parse out the dimensions, though perhaps that's the only way.

like image 815
Rhys Avatar asked Jan 15 '16 14:01

Rhys


People also ask

How do box shadows affect Element box sizing?

Padding increases the size when using the content-box sizing model. However, margins are outside of the element and create space between elements but do not affect the size of the element itself. Effects like box-shadow don't affect the size or the spacing.

How do I get shadow element in CSS?

In CSS, shadows on the boxes of elements are created using the box-shadow property (if you want to add a shadow to the text itself, you need text-shadow ). The box-shadow property takes a number of values: The offset on the x-axis. The offset on the y-axis.

How do you find the width of an element?

To find the width and height of an element, width() and height() methods are used. The width() method is used to check the width of an element. It does not check the padding, border and margin of the element.

How do you find the width of an element in pixels?

To get the width of a specific HTML Element in pixels, using JavaScript, get reference to this HTML element, and read the clientWidth property of this HTML Element. clientWidth property returns the width of the HTML Element, in pixels, computed by adding CSS width and CSS padding (top, bottom) of this HTML Element.


1 Answers

You're right, I agree it's a pretty straight forward question. Here's the problem, when you give an element a box-shadow, the box-shadow is treated like a sub-element with absolute positioning properties to it's parent element. So automatically the placement of that object under it's parent becomes a relative positioning question. They are essentially now two separate objects and need calculated separately.

enter image description here

My only suggestion would be to calculate the box-shadow's x/y positioning and add them to the width/height of the parent element. For example, in your jsfiddle, the box shadow is protruding 10px along the x-axis, and below 10px along the y-axis. With the 5px blur, add 2.5px to either side and then add the height/width to those values:

104px (width) + 10px (x-axis shadow extension) + 2.5 px (blur) = 116.5px width

104px (height) + 10px (y-axis shadow extension) + 2.5px (blur) = 116.5px height

like image 175
Collarbone Avatar answered Oct 14 '22 05:10

Collarbone