I was searching for similar solution, I think here it is better to fiddle with Javascript since we are dealing with dynamic changes.
I rendered two divs, one outside the other, set Height/Width of outer div based on screen width (based on 1500px width 100px outer div. For the inner div I calculated based on percentage of the outer div.
- HTML
<div class="wrapper">
<div class="box outer">
<div class="box inner"></div>
</div>
</div>
- CSS
.wrapper{
/* not important just to make the box appear in center for visibility*/
display: block;
margin: 200px auto;
width: fit-content;
}
/*Only selecting background colors and making inner box adjust in the center of outer box*/
.box.outer {
background: rgb(204, 75, 25);
position: relative;
}
.box.inner {
background: green;
position: absolute;
top:50%; left:50%; transform: translate(-50%, -50%);
}
- Javascript
document.addEventListener("DOMContentLoaded", function (event) {
let outerBox = document.querySelector('.box.outer');
let innerBox = document.querySelector('.box.inner');
// multiplier is used to calculate width of inner div based on outer div, so they always look the same shape
let multiplier = 0.8;
function renderBox() {
//let outerbox Dimension based on window width assuming 1500px width as base
outerBox.style.height = window.innerWidth / 15 + 'px';
outerBox.style.width = outerBox.style.height;
innerBox.style.height = outerBox.offsetHeight * multiplier + 'px';
innerBox.style.width = outerBox.offsetHeight * multiplier + 'px';
}
window.addEventListener("load", function (e) {
renderBox();
})
window.addEventListener('resize', function (e) {
renderBox();
})
})