Chrome not rendering SVG referenced via

后端 未结 21 1503
孤独总比滥情好
孤独总比滥情好 2020-11-29 02:35

I am having issues with google chrome not rendering svg with an img tag. This happens when refreshing the page and initial page load. I can get the image to show up by \"

相关标签:
21条回答
  • 2020-11-29 03:17

    I was having the same issue with an SVG image included via the IMG tag. It turned out for me that Chrome didn't like there being a blank line directly at the top of the file.

    I removed the blank line and my SVG immediately started rendering.

    0 讨论(0)
  • 2020-11-29 03:19

    I had a similar problem and the existing answers to this either weren't applicable, or worked but we couldn't use them for other reasons. So I had to figure out what Chrome disliked about our SVGs.

    In our case in turned out to be that the id attribute of the symbol tag in the SVG file had a : in it, which Chrome didn't like. Soon as I removed the : it worked fine.

    Bad:

    <svg
        xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 72 72">
        <defs>
            <symbol id="ThisIDHasAColon:AndChromeDoesNotLikeIt">
            ...
            </symbol>
        </defs>
        <use
            ....
            xlink:href="#ThisIDHasAColon:AndChromeDoesNotLikeIt"
        />
    </svg>
    

    Good:

    <svg
        xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 72 72">
        <defs>
            <symbol id="NoMoreColon">
            ...
            </symbol>
        </defs>
        <use
            ....
            xlink:href="#NoMoreColon"
        />
    </svg>
    
    0 讨论(0)
  • 2020-11-29 03:20

    The svg-tag needs the namespace attribute xmlns:

    <svg xmlns="http://www.w3.org/2000/svg"></svg>
    
    0 讨论(0)
  • 2020-11-29 03:20

    Use <object> instead (of course, replace each URL with your own):

    .BackgroundImage {
            background: url('https://upload.wikimedia.org/wikipedia/commons/b/bd/Test.svg') no-repeat scroll left top;
            width: 400px;
            height: 600px;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>SVG Test</title>
    </head>
    <body>
        <div id="ObjectTag">
            <object type="image/svg+xml" data="https://upload.wikimedia.org/wikipedia/commons/b/bd/Test.svg" width="400" height="600">
              Your browser does not support SVG.
            </object>
        </div>
        <div class="BackgroundImage"></div>
    </body>
    </html>

    0 讨论(0)
  • 2020-11-29 03:22

    Adding the width attribute to the [svg] tag (by editing the svg source XML) worked for me: eg:

    <!-- This didn't render -->
    <svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
    ...  
    </svg>
    
    <!-- This did -->
    <svg version="1.1" baseProfile="full" width="1000" xmlns="http://www.w3.org/2000/svg">
    ...  
    </svg>
    
    0 讨论(0)
  • 2020-11-29 03:22

    looks like a Chrome bug, i did something else as i almost got crazy because of this... using Chrom debugger if you change the css of the svg object it shows on the screen.

    so what i did was: 1. check for screen size 2. listen to the "load" event of my SVG object 3. when the element is loaded i change its css using jQuery 4. it did the trick for me

    if (jQuery(window).width() < 769) {
    
      jQuery('object#mysvg-logo')[0].addEventListener('load', function() {
           jQuery("object#mysvg-logo").css('width','181px');
      }, true);
    
    }
    width: 180px;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <object id="mysvg-logo" type="image/svg+xml" data="my svg logo url here">Your browser does not support SVG</object>

    0 讨论(0)
提交回复
热议问题