html - SVG 不改变大小

标签 html css svg

我创建了一个带有背景图片的 svg heart

我试图让 svg 变大,但是改变图案和 svg 本身的宽度和高度不起作用。

    <svg
    
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    version="1.1" height="315" width="345" >
    
     <!-- START SVG RULES -->
    <defs>
    <!-- DEFINE IMAGE INSIDE PATTERN -->
    <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image xlink:href="https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" x="0" y="0" width="100" height="100" /></pattern>
    
    <!-- SVG SHAPE CREATION -->
    <g id="heart">
    <path
    d="M0 200 v-200 h200
    
        a100,100 90 0,1 0,200
    
        a100,100 90 0,1 -200,0
    
        z" />
    </g>
    </defs>
    <use xlink:href="#heart" class="outline" fill="url(#img1)"  />
    </svg>

最佳答案

对于可调整大小的 SVG,请使用 viewBox="0 0 315 345" 而不是 height="315"width="345"

    <svg
    
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    version="1.1" viewBox="0 0 315 345" >
    
     <!-- START SVG RULES -->
    <defs>
    <!-- DEFINE IMAGE INSIDE PATTERN -->
    <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image xlink:href="https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" x="0" y="0" width="100" height="100" /></pattern>
    
    <!-- SVG SHAPE CREATION -->
    <g id="heart">
    <path
    d="M0 200 v-200 h200
    
        a100,100 90 0,1 0,200
    
        a100,100 90 0,1 -200,0
    
        z" />
    </g>
    </defs>
    <use xlink:href="#heart" class="outline" fill="url(#img1)"  />
    </svg>

关于html - SVG 不改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969039/

相关文章:

html - 为什么一个元素随着 "overflow: visible"消失但随着 "overflow: hidden"可见?

javascript - 在 reactjs 中合成 html 代码

javascript - Myghtyslider chavroka 的 slider 宽度自动缩放

css - 概述和部分填充 SVG 形状

javascript - d3 可拖动 svg 路径线段

javascript - 获取相关元素的CSS样式

javascript - Angularjs如何检测 Controller 中加载的所有内容,而内部很少有ajax调用?

html - 如何使用CSS设置拉伸(stretch)背景图片

html - 中间有单词的水平线的CSS技术

javascript - 如何在 D3.js SVG 网格矩形上居中文本?