Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG height incorrectly calculated in Webkit browsers

Tags:

css

webkit

svg

I have a issue specific to Webkit browsers (Safari & Chrome, on Mac & PC).

I'm using Raphael JS to render SVG data and using a responsive layout to scale the SVGs with the browser window. The SVGs are set to 100% width/height using JQuery. The containing elements have their widths set in percentages to maintain the ratios of the layout as the page resizes.

Trouble is Webkit doesn't calculate the height correctly, it adds extra pixels (sometimes hundreds) around the SVG image; which breaks the layout.

If you open the following link in a Webkit browser you'll see the green extra pixel areas. If you use the developer inpspector in safari you'll see the reported size for the SVG is bigger than the SVG displayed.

http://e-st.glam.ac.uk/simulationgames/svgheightbug/index.html

If you open the link in Firefox or Opera you'll see the layout as it should work (the green here is caused by margins I have deliberately set).

IE8 was having a similar problem which using height:auto fixed, but this won't work in Webkit.

Has anybody else had this problem? Anybody have a solution?

I think it may be a Webkit bug (checked the nightly build already, issue persists), but before I log it I thought check to make sure nobody else has a solution first.

like image 438
Barry Avatar asked Sep 27 '11 14:09

Barry


People also ask

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 .

Does SVG have width and height?

SVG's exported from Illustrator CC are 'responsive' by default, in other words there is no height or width attributes, no dimensions. This can be great, but sometimes you may want to force dimensions. Say for example you want to use an SVG for a logo on your website, but you want it to be a set size.

Can SVGs be any size?

Infinite Scalability It's right there in the name: SVGs can be expanded or shrunk down to any size without a loss of quality. Image size and display type don't matter with SVGs — they always look the same.


2 Answers

svg { max-height: 100%; } 

WebKit bug documented here: https://bugs.webkit.org/show_bug.cgi?id=82489

I also added the workaround to the bug tracker.

like image 81
zachleat Avatar answered Oct 06 '22 00:10

zachleat


I had a similar problem for Safari. Case was that the svg width and height were rendered as dom element attributes (in my case width="588.75px" height="130px"). Defining width and height in css could not overwrite this dimension setting.

To fix this for Safari I removed the width and height information from the SVG file while keeping viewBox intact (you can edit .svg files with any text editor).

Git diff snippet of my .svg file:

    xmlns:svg="http://www.w3.org/2000/svg"     xmlns="http://www.w3.org/2000/svg"     viewBox="0 0 588.75 130" -   height="130" -   width="588.75"     xml:space="preserve"     version="1.1" 
like image 34
Sebastiaan Ordelman Avatar answered Oct 05 '22 23:10

Sebastiaan Ordelman