Suppose I'm developing a reusable component, rendered as an HTML element. It can be rendered by anybody in some container. I don't know the styles of the container in advance: it could have a fixed height, or no defined height at all (in which case the container is flexible adjusts to its content).
In other words, my element should be flexible when the container is fixed, and it should be fixed when the container is flexible.
I want my component to look nice in both cases. Basically, the requirements are:
- When the container has a fixed height, my element should adjust to it, as if it had
height: 100%. - When the container has no fixed height, my element should take the lead and render with some default height, as if it had its own fixed
height. - I do not control the styles of the outer container. I only control the inner element, which anybody can render where they want.
- I want to do it using only CSS, without JS.
I found some solutions that suggest using min-height to set the default height. But it won't work, because when the container has fixed height, I want my element to adjust to it, even if it will be smaller than the default.
Default height means neither minimal nor maximal height - it's just some reasonable default value. It can be static or provided from outside as a CSS variable - doesn't matter.