15

Hello i'm trying to change the offsetHeight of an element. i used the following

document.getElementById('id').style.offsetHeight = 0;

but i saw no visible change. Can anyone help me please?

1
  • And make sure element is not inline , use block or inline-block instead Commented Feb 7, 2011 at 19:02

3 Answers 3

21

The offsetHeight property indicates the height of the visible area for an element. It's a shorthand that contains the sum of dimensions from padding, scrollbars and borders.

However, it can't be used to change the actual size and as noted in comments, offsetHeight is a property of an element, not style.

To modify the actual size use height, padding or border.

Sign up to request clarification or add additional context in comments.

1 Comment

And offsetHeight is a property of element, not its style object.
9

You should set style.height to a string ending in px.

Comments

3

You should set style.height and remember to add the unit at the end like 'px' , in the case you get it from offsetHeightfor example (well you know what unit you need). It's style and you have all the different units ('px','%','em', 'vh', ...etc). Here is an example:

myHeightInPx = 200;
DomElement.style.height = myHeightInPx + 'px';

Also to note is that offsetHeight return the height as a number, an integer. The unit is px. And if you get a value using it. you need always to add the unit 'px' when setting style.height, just like in above and the bellow example:

DomElement.style.height = AnotherDOMelment.offsetHeight() + 'px';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.