NEW 196910
If the source of an <img> element is SVG image, the aspect ratio of the SVG image is preserved
https://bugs.webkit.org/show_bug.cgi?id=196910
Summary If the source of an <img> element is SVG image, the aspect ratio of the SVG i...
cathiechen
Reported 2019-04-14 19:55:10 PDT
When the ratio is different, Chrome and Firefox will stretch the svgImage to fit the ratio of <img>, WebKit will position the svgImage in the middle. E.g. ``` <img style="width: 400px; height: 500px;" src="./green.svg"/> ``` ``` <svg xmlns="http://www.w3.org/2000/svg" width="100" height="50"> <rect fill="lime" width="100" height="50"/> <text x="0" y="15" fill="red">text</text> </svg> ```
Attachments
test case (766 bytes, text/html)
2019-04-17 10:05 PDT, Said Abou-Hallawa
no flags
Said Abou-Hallawa
Comment 1 2019-04-17 10:05:41 PDT
Created attachment 367639 [details] test case
Said Abou-Hallawa
Comment 2 2019-04-17 10:11:22 PDT
I am not sure which one is correct: preserving the aspect ratio or stretching the SVG image inside the <img> element. I personally like the WebKit behavior. I am not sure if there is specs addressing this issue. Does this cause any known bug?
cathiechen
Comment 3 2019-04-18 02:22:50 PDT
No known bug, I just met this by accident. :) I think the benefit of stretching the SVG image is that for the ratio insensitive SVG image, it could be reused by more scenario. For instance, we need green images with different sizes and different ratio. By stretching the SVG image, we could reuse one SVG image, otherwise we need to create different SVG images for every ratio. PS: the ratio insensitive SVG image: <svg xmlns="http://www.w3.org/2000/svg" width="100" height="50"> <rect fill="lime" width="100" height="50"/> </svg>
Frédéric Wang (:fredw)
Comment 4 2019-04-18 23:55:18 PDT
(In reply to Said Abou-Hallawa from comment #2) > I am not sure if there is specs addressing this issue. I'm curious about this too. Cathie, can you please investigate about this? (and raise the issue to the proper standardization group if necessary).
cathiechen
Comment 5 2019-04-24 01:43:35 PDT
(In reply to Frédéric Wang (:fredw) from comment #4) > (In reply to Said Abou-Hallawa from comment #2) > > I am not sure if there is specs addressing this issue. > > I'm curious about this too. Cathie, can you please investigate about this? > (and raise the issue to the proper standardization group if necessary). Not found in the Spec, but seems it has been disscussed in https://github.com/w3c/svgwg/issues/249#issuecomment-253092701.
Nikolas Zimmermann
Comment 6 2019-10-21 08:51:21 PDT
We synthesize a viewBox if width or height properties are supplied on the outermost <svg> element and if we're embedded through SVGImage. See SVGSVGElement::currentViewBoxRect(): if (!downcast<RenderSVGRoot>(*renderer()).isEmbeddedThroughSVGImage()) return { }; Length intrinsicWidth = this->intrinsicWidth(); Length intrinsicHeight = this->intrinsicHeight(); if (!intrinsicWidth.isFixed() || !intrinsicHeight.isFixed()) return { }; // If no viewBox is specified but non-relative width/height values, then we // should always synthesize a viewBox if we're embedded through a SVGImage. return { 0, 0, floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0) }; } If you add viewBox="0 0 100 50" manually to Saids testcase the other browser will behave as the current WebKit rendering without the viewBox. I added the code in 1adc559cd690b1bff7b5886ddee9e7112d238a92, 7 years ago. Back then this matched Opera behavior and my understanding of the specs -- that could be different now. If that is the case (and there are WPT tests proving this) let's change it :-)
Karl Dubost
Comment 7 2022-12-26 17:07:17 PST
This test is failing http://wpt.live/css/css-ui/box-sizing-016.html ``` img { box-sizing: border-box; width: auto; height: auto; background: white; padding-bottom: 30px; max-height: 100px; } ``` and ``` <svg style="background: green" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> <g id='testmeta' xmlns:html='http://www.w3.org/1999/xhtml'> </g> </svg> ``` and ``` <img src="support/r1-1.svg"> ``` As soon as we set height to 100px. It is working. So I guess the current code doesn't take into account the max-height for the aspect ratio.
Radar WebKit Bug Importer
Comment 8 2024-02-08 15:15:08 PST
Note You need to log in before you can comment on or make changes to this bug.