I have an svg image which I'm loading using an img
tag. When I view the htmo page, the image renders, but the external fonts do not load.
However, if I right-click > "open image in new tab"
, then the image displays properly in the new tab. This is happening on my web server. I made a plunk to see if I could debug something else weird in my website code. I was very surprised to see the same problem.
http://plnkr.co/edit/8Gl0iX9Iv5tEl5SB0qle?p=preview
It does seem that when I embed the svg inline in the HTML file it does render properly.
These give clues to the problem:
https://stackoverflow.com/a/29274699/188963
https://stackoverflow.com/a/15407892/188963
But since I'm importing the fonts directly into the SVG file, I would think the data encoding thing wouldn't be an obstacle.
Is there no workaround other than having a huge binary string in my html file?
Note: I'm using a templating engine (handlebars), so I might just template in the SVG code inline in the html...if that works.
Note 2: I tried encoding the data, and that didn't work either.
Note 3: I also tried inserting the SVG inline using jquery:
$.get('img/logo.svg',
function(data, status)
{
//alert("Data: " + data + "\nStatus: " + status);
//console.log(data);
$('#logo').text(data);
},
'xml'
);
which did not work either. I get [object XMLDocument]
displayed on the page.
Note 3: @ccprog, here is an screenshot of me viewing the plunk in Firefox. I'm running 55.0.3 (64-bit)
. Which version are you running?
HTML:
<!DOCTYPE html>
<html>
<body>
<!-- Header -->
<header id="logo" class="masthead">
<img src="logo.svg" onerror="this.onerror=null; this.src='image.png'">
</header>
</body>
</html>
logo.svg
<!-- width="664" height="165" -->
<svg
xmlns="http://www.w3.org/2000/svg"
id="logo-svg"
viewBox="0 0 664 165"
>
<defs>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Bitter');
@import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
@import url('https://fonts.googleapis.com/css?family=Roboto+Slab');
</style>
<style>
:root {
--graphics: #670309;
--logo-text: #000;
--services: #fff;
--features: #914E1B;
--logo-font: "Roboto Slab";
}
.house{
fill: none;
stroke: var(--graphics);
}
.bottom{
fill: var(--graphics);
stroke: var(--graphics);
}
.logo{
fill: var(--logo-text);
stroke: none;
font-family: var(--logo-font);
font-weight: 500;
}
.features{
fill: var(--features);
stroke: none;
}
.services{
fill: var(--services);
stroke: none;
}
</style>
</defs>
<!-- ROOF -->
<path d="M 50 100 L 140 50 L 230 100"
stroke-width = "15"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- CHIMNEY -->
<path d="M 200 60 L 200 80"
stroke-width = "15"
stroke-linecap = "butt"
class = "house"
/>
<!-- LEFT WALL -->
<path d="M 70 90 L 70 150"
stroke-width = "12"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- LEFT WALL -->
<path d="M 210 90 L 210 150"
stroke-width = "12"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- BOTTOM BAR AND TEXT -->
<g transform="translate(64,140)">
<svg width="600" height="25">
<rect x="0" y="0"
width="600"
height="25"
class="bottom"
/>
<text x="50%" y="65%"
alignment-baseline="middle"
text-anchor="middle"
font-size="76%"
font-weight="500"
font-family="Roboto Condensed"
letter-spacing="0.07em"
class="services"
>
<tspan class="service">residential</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">commercial</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">buyer</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">seller</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">warranty</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">expert-witness</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">sewer</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">radon</tspan>
</text>
</svg>
</g>
<!-- HOUSE DETECTIVES LLC -->
<g transform="translate(280,35)">
<text x="-50px" y="-10px"
alignment-baseline="middle"
text-anchor="left"
font-size="30px"
transform="rotate(-90)"
class="logo"
>the</text>
<text x="0" y="0"
alignment-baseline="middle"
text-anchor="left"
font-size="64px"
class="logo"
>
<tspan class="name" x="0" dy="50px">House</tspan>
<tspan class="name" x="0" dy="50px">Detectives</tspan>
<tspan class="name" dx="-10px" dy="0" font-size="24px">llc</tspan>
</text>
</g>
<!-- DOOR -->
<rect x="105" y="95" height="45" style="stroke-width:0.5; stroke:var(--features)" width="25" class="features"></rect>
<!-- SINGLE WINDOW DEFINITION -->
<defs>
<rect id="window" x="0" y="0" width="10" height="10" class="features"></rect>
</defs>
<!-- WINDOWS -->
<g transform="translate(150,95)">
<use href="#window" transform="translate(0 0)"/>
<use href="#window" transform="translate(0 12)"/>
<use href="#window" transform="translate(12 0)"/>
<use href="#window" transform="translate(12 12)"/>
</g>
</svg>
<!DOCTYPE html>
<html>
<body>
<!-- Header -->
<header id="logo" class="masthead">
<!-- width="664" height="165" -->
<svg
xmlns="http://www.w3.org/2000/svg"
id="logo-svg"
viewBox="0 0 664 165"
>
<defs>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Bitter');
@import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
@import url('https://fonts.googleapis.com/css?family=Roboto+Slab');
</style>
<style>
:root {
--graphics: #670309;
--logo-text: #000;
--services: #fff;
--features: #914E1B;
--logo-font: "Roboto Slab";
}
.house{
fill: none;
stroke: var(--graphics);
}
.bottom{
fill: var(--graphics);
stroke: var(--graphics);
}
.logo{
fill: var(--logo-text);
stroke: none;
font-family: var(--logo-font);
font-weight: 500;
}
.features{
fill: var(--features);
stroke: none;
}
.services{
fill: var(--services);
stroke: none;
}
</style>
</defs>
<!-- ROOF -->
<path d="M 50 100 L 140 50 L 230 100"
stroke-width = "15"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- CHIMNEY -->
<path d="M 200 60 L 200 80"
stroke-width = "15"
stroke-linecap = "butt"
class = "house"
/>
<!-- LEFT WALL -->
<path d="M 70 90 L 70 150"
stroke-width = "12"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- LEFT WALL -->
<path d="M 210 90 L 210 150"
stroke-width = "12"
stroke-linecap = "butt"
stroke-linejoin = "miter"
class = "house"
/>
<!-- BOTTOM BAR AND TEXT -->
<g transform="translate(64,140)">
<svg width="600" height="25">
<rect x="0" y="0"
width="600"
height="25"
class="bottom"
/>
<text x="50%" y="65%"
alignment-baseline="middle"
text-anchor="middle"
font-size="76%"
font-weight="500"
font-family="Roboto Condensed"
letter-spacing="0.07em"
class="services"
>
<tspan class="service">residential</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">commercial</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">buyer</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">seller</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">warranty</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">expert-witness</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">sewer</tspan>
<tspan class="separator" dx="5px">■</tspan>
<tspan class="service" dx="5px">radon</tspan>
</text>
</svg>
</g>
<!-- HOUSE DETECTIVES LLC -->
<g transform="translate(280,35)">
<text x="-50px" y="-10px"
alignment-baseline="middle"
text-anchor="left"
font-size="30px"
transform="rotate(-90)"
class="logo"
>the</text>
<text x="0" y="0"
alignment-baseline="middle"
text-anchor="left"
font-size="64px"
class="logo"
>
<tspan class="name" x="0" dy="50px">House</tspan>
<tspan class="name" x="0" dy="50px">Detectives</tspan>
<tspan class="name" dx="-10px" dy="0" font-size="24px">llc</tspan>
</text>
</g>
<!-- DOOR -->
<rect x="105" y="95" height="45" style="stroke-width:0.5; stroke:var(--features)" width="25" class="features"></rect>
<!-- SINGLE WINDOW DEFINITION -->
<defs>
<rect id="window" x="0" y="0" width="10" height="10" class="features"></rect>
</defs>
<!-- WINDOWS -->
<g transform="translate(150,95)">
<use href="#window" transform="translate(0 0)"/>
<use href="#window" transform="translate(0 12)"/>
<use href="#window" transform="translate(12 0)"/>
<use href="#window" transform="translate(12 12)"/>
</g>
</svg>
</header>
</body>
</html>