Add background image in SVG

无人久伴 提交于 2019-12-02 17:35:25

问题


I need to add background image in svg. But I'm getting only black rectangle box, the image is not displaying. How to add the background image?

This is my code:

<style>
    path {
        fill: none;
        stroke: #000;
        stroke-width: 3px;
        stroke-linejoin: round;
        stroke-linecap: round;
    }
</style>

<svg width="1000" height="700">
    <!-- <rect fill="#fff" width="100%" height="100%"></rect> -->

    <defs>
        <pattern id="img1" patternUnits="userSpaceOnUse" x="0" y="0" width="1000" height="700">
            <image xlink:href="home/nayana/Documents/Text Editor Files/abstract-hd.jpg" width="600" height="450" />             
        </pattern>
    </defs>
    <path d="M5,5 l0,680 l980,0 l0,-680 l-980,0 fill="url(#img1)" />
</svg>

Thanks in advance


回答1:


Correct a syntax error in the path, you are missing a " at the end and remove fill:none from CSS that is overriding the fill attribute used with the path:

Full code:

path {
  stroke: #000;
  stroke-width: 3px;
  stroke-linejoin: round;
  stroke-linecap: round;
}
<svg width="1000" height="700">
    <!-- <rect fill="#fff" width="100%" height="100%"></rect> -->

    <defs>
        <pattern id="img1" patternUnits="userSpaceOnUse" x="0" y="0" width="1000" height="700">
                    <image xlink:href="https://lorempixel.com/600/450/" width="600" height="450" />             
            </pattern>
    </defs>
    <path d="M5,5
        l0,680 l980,0 l0,-680 l-980,0"          
        fill="url(#img1)" />
</svg>


来源:https://stackoverflow.com/questions/48299472/add-background-image-in-svg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!