Inconsistent SVG scaling in HTML between browsers - html

I'm experimenting with using SVG in HTML, something I'm not too familiar with. It's embedded as an object in the html.
The SVG is scalable through CSS. When the window is at full size (i.e. large enough that the SVG is at its maximum allowed width through CSS) it looks normal in Firefox, Chrome, and Safari. When the browser window is small, the actual SVG image and what I assume is its container scales down appropriately in both Firefox and Chrome. However, in Safari (desktop and iOS) the SVG image itself scales down but its "container" stays at its original, maximum height. In other words there's too much space on the top and bottom of the actual image in narrow widths in Safari.
I currently have width, max-width, and max-height values assigned via CSS. Applying a height value other than "auto" gives me the problem across all browsers. I've removed the width and height values from the SVG itself to make it adjust to the browser size.
I want to know how to make Safari scale the SVG image in the same manner as Firefox and Chrome.
Thanks for any input you can offer!
Please check out this example: http://jay-bee-why.com/demos/svg-scaling.html
And to see the actual site: http://twowordsfor.com (edit: now deprecated)
Here's how I've embedded the SVG in the HTML:
<div class="container">
<object data="image.svg" type="image/svg+xml" id="logo">
<img src="image.png">
</object>
</div>
This is the CSS:
.container {width:99.8%; -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; padding:30px;}
#logo {width:100%; max-width:974px; max-height:300px; display:block; margin:0px auto 0px auto;}
And this is the top of the SVG file:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 967 135" enable-background="new 0 0 967 135" xml:space="preserve">

Here is how this looks now:
.container {width:99.8%; -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; padding:30px;}
#logo {width:100%; max-width:974px; max-height:300px; display:block; margin:0px auto 0px auto;}
<div class="container">
<object data="https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg" type="image/svg+xml" id="logo">
</object>
</div>

Related

SVG won't scale

Good Afternoon,
I know this question has been asked over and over but I can't seem to find the answer to fix my problem in any of the post i've searched.. which is a lot!
So I have a container Div which I want my SVG to scale to (100%).
I currently have HTML of
<div class="svg-lrg">
<object data="../images/sodium-sync-1.svg" width="416px" height="164px" type="image/svg+xml"></object>
</div>
I have also changed the HTML to try width="100%" as well as no width specified at all.
Div CSS is
.svg-lrg
{ width:100%;
height: auto;
margin: auto;
text-align: center;}
SVG info (or at least what I think is the important bit)
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
preserveAspectRatio="xMinYMin meet" version="1.1" viewBox="0 0 416 164">
I have the width and height set to 100% within the SVG.
It weirdly will be the correct size if changes are made to on the google console but on initial load or refreshing the page it's too small!
I've tried clear all css styles putting it within a Div, incase something on the template is upsetting it
.clear
{all: unset;}
Still no joy...
Any ideas?
Thanks :)
Just use this:
HTML:
<div class="svg-lrg">
<object data="../images/sodium-sync-1.svg" width="100%" height="100%" type="image/svg+xml"></object>
</div>
Setting both the width and height to 100% will scale the image to the parent (<div>).
PS.: Your code wasn't working because you were trying to scale the <div>, not the <object>.

The position of SVG is off on Chrome. How do I align it to how it is displayed on Firefox?

I don't know why this is happening. Ideally, I would like the svg to be displayed over the text on all browsers, but Chrome is not making this happen. This is how it looks like on Firefox:
And this is how it looks like on Chrome:
As you can tell, the svg is on the text. I don't know if it's the margin or the svg but I would like to understand how I can fix this.
Here is my CSS:
svg{
display:block;
}
#rect{
margin-top:-150px; /*helps svg display over text on Firefox. Nothing happens on Chrome*/
margin-left:200px;
height:500px;
}
And this is the SVG code:
<div class="circle-border">
<div class="svg">
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg id="rect" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 960 560" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:white;stroke-miterlimit:10;}
</style>
<polygon class="st0" points="224.5,150.5 224.5,477.5 649.5,477.5 649.5,444.5 682.5,444.5 682.5,412.5 715.5,412.5 715.5,379.5
748.5,379.5 748.5,346.5 780.5,346.5 780.5,150.5 "/>
</svg>
</div><!--border-->
Feel free to let me know if you need additional information.

Scale svg image to fit parent as css background

I want to have an svg image to be scaled as follows:
The right arrow (painted in red) should always scale to the height of the blue box, which can have dynamic height.
I have tried implementing it using an svg image:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 50 100" preserveAspectRatio="xMidYMid meet">
<polygon points="0,0 50,50 0,100" style="fill:white" />
</svg>
I tried different versions of preserveAspectRatio and viewBox.
My css implementation is like
#before-main section header.section-header .header-arrow {
position:absolute;
right:0;
height:100%;
width:4.1%;
background:url("../img/headlineArrow_bg.svg") no-repeat right center;
background-size:100% 100%;
}
HTML looks like
<header class="section-header">
<div class="header-arrow">
</div>
</header>
Is there a way to implement this using the svg image as css background?
Edit: Width of The arrow should always stay the same.
To make your red arrow stretch to match the height, try preserveAspectRatio="none".

How to fix SVG top and left stroke truncation?

I have created an ellipse in SVG with a stroke-width and stroke but the top and left sides of the stroke are truncated. They appear to be overflowing outside of the containing svg tag. I tried using overflow:visible; property on the SVG but it still doesn't work.
Following is my HTML:
<div id="div1">
<svg id="svg1">
<ellipse id="oval1" cx="164.041666625656" cy="96.1319444204114" rx="164.041666625656" ry="96.1319444204114"></ellipse>
</svg>
</div>
And this is its CSS:
#div1 {
padding:10px;
margin:10px;
}
#svg1 {
overflow:visible;
margin-left:86.4305555339479;
margin-top:-4.409722221119791;
}
#oval1 {
fill:hsl(50.7692307692305,27.6595744680847%,68.0882352941177%);
stroke-width:5.291666665343749;
stroke:hsl(79.5918367346938,41.8803418803418%,40.5882352941176%);
}
You can see the running code here as well.
I have had a similar problem with rectangles in the past and they got fixed after using overflow:visible; for the containing svg. But it doesn't seems to be working for ellipses now.
Can anyone help me fix the issue?
Thnx in advance!
UPDATE: Looks like there are active bug for this behaviour in at least Chrome (https://code.google.com/p/chromium/issues/detail?id=231577) and has been for Firefox as well (https://bugzilla.mozilla.org/show_bug.cgi?id=378923). So depending on version you might be out of luck. As of writing this is not fixed in Chrome (32.0.1700.6 beta) and there is a fiddle you can use to test with here: http://jsfiddle.net/HRsvX/36/ all three triangles should be fully visible if the browser implements the current SVG 1.1 spec. Fiddle reproduced below.
The circle inner area is bordering the SVG element. Before HTML5t the SVG element itself is like an image or flash movie, it can't overflow into the html document, it has it's own canvas so to speak. When you add stroke (that by default is outside the area you defined) the stroke will end up outside the SVG canvas. You'll have to account for that in the centering of the circle:
The center has to be the radius+stroke width so your center x for example would have to be 164.041666625656 + 5.291666665343749 minimum to fully fit inside the SVG.
If you specify the HTML5 doctype and use an inline SVG as in your example it should show the overflowing content since the default value for overflow is visible and HTML5 allows for overflow on inline SVG elements.
So either check your doctype or reposition the center to account for the stroke width.
More on the overflow of SVG elements can be found in Mozilla developer documents and a nice piece on this MSDN blog that explains the default overflow.
HTML
<div><svg height="100" width="100" viewbox="00 0 100 100">
<path d="M210 10 L90 10 L90 90 " fill="red"/>
</svg></div>
<div><svg id="clip1" height="100" width="100" viewbox="00 0 100 100">
<path d="M210 10 L90 10 L90 90 " fill="red"/>
</svg></div>
CSS
div {
height:100px; width:100px;
margin:1em auto;
border: solid 1px black;
}
svg { overflow:visible }
#clip1 {clip: rect(-10px,-10px,-10px,-10px)} //nope
#clip2 {clip: auto} //nope
Add a viewBox that describes the content of the SVG. The browser will then ensure that the entire ellipse and its stroke are visible. If you want it renderered at 1:1, you will also need to add an equivalent width and height.
<svg id="svg1" width="335" height="199" viewBox="-3 -3 335 199">
<ellipse id="oval1" cx="164.041666625656" cy="96.1319444204114" rx="164.041666625656" ry="96.1319444204114" />
</svg>
Demo here

Getting an SVG to Keep It's Aspect Ratio in an HTML Page

I have HTML like that below. I create a SVG with a viewbox of 100x100. When rendered (in Chrome) The svg is rendering itself as 200px wide (good) but ~500px high and the text is pushed off the bottom of the page. Making my window bigger or smaller has no affect - the SVG simply grows or shrinks in height accordingly.
Why is the svg not constrained in height in any way? Is there a way to make it automatically keep it's 1:1 aspect ratio? The content inside of the SVG is fine - it scales appropriately. This is causing some major headaches
<html>
<head>
<title>SVG Ahoy</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="width: 200px;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"
preserveAspectRatio="xMinYMin meet" version="1.1"></svg>
<div>content below</div>
</div>
</body>
</html>
You have prserveApsectRatio set here, but I do not see a height on your div. Is that causing an issue?Also, if you look at the svg documentation, svg items default to 100% width and height. Try setting it's height and widths explicitly as pixel values and see if that helps.
Put the svg in an <img> tag: <img src="path_to_svg" />
Style the image: {width: 100%; height: auto;}
Setting the height to auto is the secret. Like this, even the preserveAspectRatio attribute is optional. (Putting it into a table layout will break Internet Explorer compatability).