CSS - margin fusion - example 5
Here, margin declared on both elements
by Julien Roche
HTML
<div class="main">
<div class="main__child" style="margin-bottom: 30px;">Height: 75px + margin bottom 30px</div>
<div class="main__child" style="margin-top: 20px;">Height: 75px + margin top 20px</div>
</div>
<br />
<p>
<span>Element height: </span>
<span id="heightResult"></span>
<span>px !== with expected 200px</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;
margin: 0 0 0 0;
padding: 0 0 0 0;
width: 350px;
}
.main__child {
background-color: black;
color: white;
height: 75px;
}
Babel + JSX
let heightResultElement = document.querySelector('#heightResult');
let mainElement = document.querySelector('.main');
function updateHeight() {
let bounds = mainElement.getBoundingClientRect();
heightResultElement.innerText = bounds.height;
}
let ro = new ResizeObserver(updateHeight);
ro.observe(mainElement);