How to detect DIV’s dimension changed?

How to detect DIV’s dimension changed?

I’ve the following sample html, there is a DIV which has 100% width. It contains some elements. While performing windows re-sizing, the inner elements may be re-positioned, and the dimension of the div may change. I’m asking if it is possible to hook the div’s dimension change event? and How to do that? I currently bind the callback function to the jQuery resize event on the target DIV, however, no console log is outputted, see below:


Solution 1:

There is a very efficient method to determine if a element’s size has been changed.

This library has a class ResizeSensor which can be used for resize detection.
It uses an event-based approach, so it’s damn fast and doesn’t waste CPU time.


new ResizeSensor(jQuery('#divId'), function(){ 
    console.log('content dimension changed');

Please do not use the jQuery onresize plugin as it uses setTimeout() loop to check for changes.

Disclosure: I am directly associated with this library.

Solution 2:

A new standard for this is the Resize Observer api, available in Chrome 64.

function outputsize() {
 width.value = textbox.offsetWidth
 height.value = textbox.offsetHeight

new ResizeObserver(outputsize).observe(textbox)
Width: <output id="width">0</output><br>
Height: <output id="height">0</output><br>
<textarea id="textbox">Resize me</textarea><br>

Resize Observer



Firefox Issue:

Safari Issue:

Current Support:

Solution 3:

You have to bind the resize event on the window object, not on a generic html element.

You could then use this:

$(window).resize(function() {

and within the callback function you can check the new width of your div calling


So, the answer to your question is no, you can’t bind the resize event to a div.

Solution 4:

Long term, you will be able to use the ResizeObserver.

new ResizeObserver(callback).observe(element);

Unfortunately it is not currently supported by default in many browsers.

In the mean time, you can use function like the following. Since, the majority of element size changes will come from the window resizing or from changing something in the DOM. You can listen to window resizing with the window’s resize event and you can listen to DOM changes using MutationObserver.

Here’s an example of a function that will call you back when the size of the provided element changes as a result of either of those events:

var onResize = function(element, callback) {
  if (!onResize.watchedElementData) {
    // First time we are called, create a list of watched elements
    // and hook up the event listeners.
    onResize.watchedElementData = [];

    var checkForChanges = function() {
      onResize.watchedElementData.forEach(function(data) {
        if (data.element.offsetWidth !== data.offsetWidth ||
            data.element.offsetHeight !== data.offsetHeight) {
          data.offsetWidth = data.element.offsetWidth;
          data.offsetHeight = data.element.offsetHeight;

    // Listen to the window's size changes
    window.addEventListener('resize', checkForChanges);

    // Listen to changes on the elements in the page that affect layout 
    var observer = new MutationObserver(checkForChanges);
    observer.observe(document.body, { 
      attributes: true,
      childList: true,
      characterData: true,
      subtree: true 

  // Save the element we are watching
    element: element,
    offsetWidth: element.offsetWidth,
    offsetHeight: element.offsetHeight,
    callback: callback

Solution 5:

ResizeSensor.js is part of a huge library, but I reduced its functionality to THIS:

function ResizeSensor(element, callback)
    let zIndex = parseInt(getComputedStyle(element));
    if(isNaN(zIndex)) { zIndex = 0; };

    let expand = document.createElement('div'); = "absolute"; = "0px"; = "0px"; = "0px"; = "0px"; = "hidden"; = zIndex; = "hidden";

    let expandChild = document.createElement('div'); = "absolute"; = "0px"; = "0px"; = "10000000px"; = "10000000px";

    let shrink = document.createElement('div'); = "absolute"; = "0px"; = "0px"; = "0px"; = "0px"; = "hidden"; = zIndex; = "hidden";

    let shrinkChild = document.createElement('div'); = "absolute"; = "0px"; = "0px"; = "200%"; = "200%";


    function setScroll()
        expand.scrollLeft = 10000000;
        expand.scrollTop = 10000000;

        shrink.scrollLeft = 10000000;
        shrink.scrollTop = 10000000;

    let size = element.getBoundingClientRect();

    let currentWidth = size.width;
    let currentHeight = size.height;

    let onScroll = function()
        let size = element.getBoundingClientRect();

        let newWidth = size.width;
        let newHeight = size.height;

        if(newWidth != currentWidth || newHeight != currentHeight)
            currentWidth = newWidth;
            currentHeight = newHeight;



    expand.addEventListener('scroll', onScroll);
    shrink.addEventListener('scroll', onScroll);

How to use it:

let container = document.querySelector(".container");
new ResizeSensor(container, function()
    console.log("dimension changed:", container.clientWidth, container.clientHeight);

Solution 6:

I found this library to work when MarcJ’s solution didn’t:

It’s very lightweight and detects even natural resizes via CSS or simply the HTML loading/rendering.

Code sample (taken from the link):

<script type="text/javascript" src="detect-element-resize.js"></script>
<script type="text/javascript">
  var resizeElement = document.getElementById('resizeElement'),
      resizeCallback = function() {
          /* do something */
  addResizeListener(resizeElement, resizeCallback);
  removeResizeListener(resizeElement, resizeCallback);