So I've seen many solutions for making square divs, but I have yet to find a simple one that behaves exactly like a background-size:contain; background-position:center center;
The goal:
This approach is almost perfect, but it doesn't center the square vertically: https://jsfiddle.net/8s4chau9/
#wrapper {
width: 50%;
height: 300px;
border: 1px solid blue;
}
#square {
aspect-ratio: 1/1;
max-width: 100%;
max-height: 100%;
margin: auto;
box-sizing: border-box;
background-color: red;
}
<div id=wrapper>
<div id=square></div>
</div>
I thought that making the wrapper a flex would work but it does not: display:flex; align-items:center; justify-content:center;
Hopefully someday none of this will be necessary when someone will finally create the css units pw (parent width), ph (parent height), pmin and pmax (like vmin/vmax but for parent rather than viewport).
If I've understood well, your current problem is that the square doesn't align vertically. This can be easily fixed with the translate CSS function:
#wrapper {
width: 50%;
height: 300px;
border: 1px solid blue;
}
#square {
position: relative;
top: 50%;
aspect-ratio: 1/1;
max-width: 100%;
max-height: 100%;
margin: auto;
box-sizing: border-box;
background-color: red;
transform: translateY(-50%);
}
<div id=wrapper>
<div id=square></div>
</div>
First, you assign a position to your square other than static, so that you can assign a top value to it. In this case we'd prefer it to be positioned relatively.
Then, you assign a top: 50%
, that will be tweaked to give a centered result with transform: translateY(-50%)
.
Here you have your edited fiddle: https://jsfiddle.net/Bettylex/6ncks2fx/2/