SVG spritesheet targeting with CSS

后端 未结 2 786
孤城傲影
孤城傲影 2020-12-11 08:42

I\'ve been trying to find a solution for handling SVG elements used as background images via CSS:

.icon.arrow-down
{
    background-image: url( \'images/sp         


        
相关标签:
2条回答
  • 2020-12-11 09:26

    It's a known issue and is specific to using it as a background and apparently won't be fixed because of security concerns (Opera also doesn't display it). If you view the SVG directly, it works as you would expect.

    https://code.google.com/p/chromium/issues/detail?id=128055#c6

    SVG stacks will not be supported for CSS properties taking CSS Image values. This includes, but is not limited to, background-image, mask-image, border-image.

    This is a resolution of the SVG and CSS WG to differ between resources (like SVG gradients, masks, clipPath) and image values during parse time of CSS. This is a security requirement to protect the users privacy and safety.

    See following discussions for further information:

    http://lists.w3.org/Archives/Public/www-style/2012Oct/0406.html

    http://lists.w3.org/Archives/Public/www-style/2012Oct/0765.html

    You're just going to handle your SVG the same way you would an old fashioned sprite map.

    0 讨论(0)
  • 2020-12-11 09:42

    For my latest project, I've implemented my own way of creating custom SVG parameters using a PHP MVC framework I've been working on. Essentially, I created a controller for linking to icons:

    /icon/NAME_OF_ICON.svg?color=F00
    

    My icon controller takes the filename and injects the GET parameters into the SVG file.

    //define( ROOT, "path/to/webroot" );
    //$filename = ...get filename from URL...;
    
    $svg = simplexml_load_file( ROOT . "/assets/icons/{$filename}" );
    if( isset( $_GET['color'] ) )
    {
        $svg->path->addAttribute( 'fill', '#' . $_GET['color'] );
    }
    
    header( "Content-type: image/svg+xml" );
    echo $svg->asXML( );
    

    I'll be adding code to cache the queried custom SVG's, eventually.

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