I'm developing a bunch of custom elements for an API using a web components polyfill and I've hit a snag.
One of the elements may contain an <img>
or <canvas>
element. If no dimensions are specified for the custom element, it should be the default size of the child element. If one or more dimensions are specified, they should be inherited by the child element.
For my first effort, I thought the CSS inherit
value would be good enough:
my-el img, my-el canvas {
width: inherit;
height: inherit;
max-width: inherit;
min-width: inherit;
max-height: inherit;
min-height: inherit;
}
However, this doesn't work when a percentage is applied to the width or height for <my-el>
. Instead, the child element takes up the same percentage of its parent.
I've tried various other attempts at a solution and searched far and wide to no avail. I think a pure CSS solution may be impossible but I'm hoping someone can prove otherwise.
To clarify, the end result should be that <my-el>
behaves like an inline-replaced element itself, as if it were an <img>
with its own internal dimensions. When you don't set width or height on those elements, they default to their internal dimensions. When you do set width or height, that takes precedence and any "internal" content is resized accordingly. (At least, I hope that clarifies!)
The percentage width and height are relative to the parent element, so you could probably just use this I think.
Feel free to play around by setting any width and height to
my-el
, see if it works for you.http://jsfiddle.net/6afdbfck/