JavaScript: ottenere la larghezza di un elemento compresi i margini

JavaScript offre un semplice modo per ottenere la larghezza esterna di un elemento compresi i margini.

La soluzione รจ la seguente:


'use strict';

const outerWidth = el => {

  const width = el.offsetWidth;
  const style = getComputedStyle(el);

  return width + parseInt(style.marginLeft,10) + parseInt(style.marginRight,10);

};

Torna su