Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 19, 2023 22:06
Show Gist options
  • Save danielhaim1/5731470 to your computer and use it in GitHub Desktop.
Save danielhaim1/5731470 to your computer and use it in GitHub Desktop.
The adjustHeight function is a JavaScript function that adjusts the height of the elements with the class name starting with "breakit" to match the height of their child element with the class name starting with "brokeit".
<div class="breakit" style="position: relative;">
<div class="brokeit" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0;">
<img src="..." style="height:375px;">
</div>
</div>
'use strict';
const adjustHeight = () => {
const breakItElems = document.querySelectorAll('[class^="breakit"]');
if (!breakItElems) {
console.error('No elements found with class "breakit"');
return;
}
breakItElems.forEach((breakItElem) => {
const brokeItElem = breakItElem.querySelector('[class^="brokeit"]');
if (!brokeItElem) {
console.error('No element found with class "brokeit" inside a "breakit" element');
return;
}
const height = brokeItElem.clientHeight;
breakItElem.style.height = `${height}px`;
});
};
window.addEventListener('resize', adjustHeight);
document.addEventListener('DOMContentLoaded', adjustHeight);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment