ThomasReggi
ThomasReggi

Reputation: 59345

Get width of element with no css or width attribute

Scenario: You have a div with some text, this div has no css width, no jquery width, no width attribute assigned to it in any way. However it has a width due to the content that is inside it, this is undefined, reports as "null" in jQuery. My question is: is there any way to retireve the width of this div?

Upvotes: 0

Views: 1234

Answers (3)

Joseph Markle
Joseph Markle

Reputation: 1

You can use the offsetWidth like this:

element.offsetWidth

or with a ref in React

elementRef.current.offsetWidth

Upvotes: 0

kobe
kobe

Reputation: 15835

with jquery you can do below

$('#id').width()

Upvotes: 1

eflorico
eflorico

Reputation: 3629

$(element).width() should give you the actual width. With $(element).outerWidth(includeMargin) you can even get the width including padding, border, and, if desired, the margin.

Upvotes: 3

Related Questions