CSS - margin fusion - example 4

Here, margin declared on both elements

by Julien Roche

HTML

<div class="main">
  <div class="main__float" style="margin-right: 30px;">Width: 150px</div>
  <div class="main__float" style="margin-left: 30px;">Width: 150px</div>
</div>
<br />
<p>
  <span>Element width: </span>
  <span id="widthResult"></span>
  <span>px</span>
</p>

CSS

html, body {
  height: 100%;
}

body {
  align-items: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.main {
  background-color: orange;
  border: none;
  display: inline-block;
  height: 60px;
  margin: 0 0 0 0;
  padding: 0 0 0 0;
}

.main__float {
  background-color: black;
  color: white;
  height: 35px;
  float: left;
  width: 150px;
}

Babel + JSX

let widthResultElement = document.querySelector('#widthResult');
let mainElement = document.querySelector('.main');

function updateWidth() {
	let bounds = mainElement.getBoundingClientRect();
  widthResultElement.innerText = bounds.width;
}

let ro = new ResizeObserver(updateWidth);
ro.observe(mainElement);