Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG viewbox height issue on ios Safari

I have noticed a strange thing on ios when using svg. The svgs seem to work fine in all other browsers, but on Safari ipad/iphone the viewbox has some weird space at the top and bottom of the svg. Has anyone else come across this and have you been able to fix it? Using some simple svg code such as:

<svg width="100%" viewBox="0 0 20 10">
    <polygon fill=red stroke-width=0 points="0,10 20,10 10,0" />
</svg>

On ipad/iphone if I put a border on the svg there strange space above and below the svg...??

fiddle goodness looks normal on desktop but if you look at it on an ipad etc you'll see the issues.

http://jsfiddle.net/InVAMPED/hck5gg1a/

like image 301
user2541153 Avatar asked Sep 29 '14 03:09

user2541153


People also ask

Does mobile Safari support SVG?

SVG (basic support) is Fully Supported on Safari 12, which means that any user who'd be accessing your page through Safari 12 can see it perfectly.

How do I scale SVG viewBox?

We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.

How do I fix SVG size?

Just set the viewBox on your <svg> , and set one of height or width to auto . The browser will adjust it so that the overall aspect ratio matches the viewBox .

How do I make SVG viewBox smaller?

Values of width and height: With the width and height values you can change the size of the SVG vector. So, if we want to change the size and make it larger, then set the value for width and height, in viewBox, smaller then the width and height properties of the SVG element.


2 Answers

The problem is that you are only setting width, not height of the SVG layout box. The viewBox is then being fit inside this layout box using the default xMidYMid meet setting, which scales it just to fit in the more constrained dimension and centers it in the other direction.

Firefox and the latest versions of Chrome (and I guess desktop Safari as well) will scale the SVG to match the viewBox aspect ratio when you leave one dimension as auto. However, other browsers will apply a default height/width, and then scale the image to fit:

  • IE applies the 150px height/300px width that is the default for embedded objects.
  • Safari mobile must be applying the old webkit default of 100vh (the height of the browser window).

It's not really a "bug" in the browsers, just a feature that was never clearly defined in the specifications.

Search for information about the "padding bottom aspect ratio hack" for a way of forcing the browser to respect an aspect ratio while still allowing the width to be responsive.

like image 64
AmeliaBR Avatar answered Sep 24 '22 07:09

AmeliaBR


AmeliaBR is entirely right, a big thanks for leading me in the right direction!

Here's what google showed me: The padding-bottom hack

Because a percentage value for padding-bottom gets it's height from the width of the element and not the height itself we can leverage this to create responsive elements without a specified height.

On the SVG container:

.container {
    padding-bottom: 70%; /*this is your height/width ratio*/
    height: 0;
  }

On the SVG element itself:

.container svg {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}
like image 32
undervers Avatar answered Sep 26 '22 07:09

undervers