I am currently developing a website where I wish an image to be 25% of it's parent height.
I have looked at the following answers on SO and a few other similar answers on other sites:
- CSS square based on height
- Css div responsive square dependent on height
- https://css-tricks.com/aspect-ratio-boxes/
The problem with the above answers is it either uses view-port units (vh
), or it is aspect ratio based on the width. In my case, I would like to set an element to 25% of it's parent height. I have tried the below code, however it just sets the width of it to 100%.
main {
--main-height: calc(100vh - var(--nav-height) - var(--body-top-padding));
min-height: var(--main-height);
}
section {
border: 1px solid red;
width: 100%;
min-height: calc(var(--main-height) / 2);
}
#profile > img {
border: 1px solid black;
height: 25%;
padding-left: 100%;
}
Here, #profile is a section with an id of profile.
Edit
Here is what I feel like hack to do this:
#profile > img {
border: 1px solid black;
--width: calc(0.25 * var(--main-height));
min-height: var(--width);
width: var(--width);
}
Is there a better way which doesn't use CSS properties and calculations?