You don't need javascript now that we have min()
, max()
and clamp()
.
While these weren't around in 2012 they've been well supported for several years now.
Based on your original question:
I want height of 40px or 11%, whichever is greatest:
First of all it's usually better to use vw
or vh
(instead of %) if you want to refer to a proportional size of the screen, since 11% will just be 11% of the containing element and can be unpredictable.
So with min / max you can use:
height: max(40px, 11vw);
Another alternative (and frankly I find this easier to read) you can use clamp:
height: clamp(100px, 50vh, 900px);
This will 'prefer' to be a height of half the viewport with a minimum of 100px and a maximum of 900px (for really tall screens). So you get really nice fluid motion when you resize the screen with no coding.
Of course these can be used in conjunction with min-height
or max-height
as well if that is more appropriate for your needs.
Wow I'm amazed nobody added this in over ten years!