My idea is to get the child object (@example the ".moon") to have the equal values of height and width, and not exceed the width or the height of the parent (@example the ".sun").
So when the parent width is bigger than its height, the child's value for width and height is the smaller one, the parent height.
In the same way when the parent height is bigger than its width, the child's value for width and height is the smaller one, the parent width.
I mean inscribe an object inside the container but only using CSS.
The "padding-top" trick only works when the parent width is bigger than its height. And the "object-fit" only work for images I guess.
I did reference the circles, just to make it clear that the sides have to have the same value (height and width), also that the child can contain elements.
I know it can be perfectly done with JavaScript getting the parent height and width, comparing the smaller one and set it to the child height and width, I made it with jQuery, but wanna know if its possible to achieve it without jQuery or JavaScript, pure CSS.
Try the example and resizing the screen.
var pw = ""; var ph = ""; var cw = ""; var ch = "";
$( window ).resize(function() {
pw = $('.sun').width();
ph = $('.sun').height();
cw = $('.moon').width();
ch = $('.moon').height();
if(pw > ph){
$('.moon').css({
'width': ph + 'px',
'height': ph + 'px'
});
}
else {
$('.moon').css({
'width': pw + 'px',
'height': pw + 'px'
});
}
});
function mySUNction() {
pw = $('.sun').width();
ph = $('.sun').height();
cw = $('.moon').width();
ch = $('.moon').height();
if(pw > ph){
$('.moon').css({
'width': ph + 'px',
'height': ph + 'px'
});
}
else {
$('.moon').css({
'width': pw + 'px',
'height': pw + 'px'
});
}
}
* {
padding: 0;
margin:0;
}
.sky {
position: absolute;
width: 100%;
height: 100%;
background: purple;
display: flex;
justify-content: center;
align-items: center;
}
.sun {
position: relative;
width: 33%;
/*padding-top: 33%;*/
height: 33%;
background: yellow;
display: flex;
justify-content: center;
align-items: center;
}
.moon {
background: blue;
/*object-fit: fill;*/
/*border-radius: 50%;*/
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div class="sky">
<div class="sun">
<div class="moon"></div>
</div>
<div>
<button onclick="mySUNction()">Click me</button>