How to make SVG “fill” act in a similar way to CSS “background-size: cover”

前端 未结 2 1555
旧巷少年郎
旧巷少年郎 2021-01-17 11:56

I\'m am trying to create an SVG which masks an image with a particular path however, when I try to fill the area using a pattern, the image does not cover the entire area.

相关标签:
2条回答
  • 2021-01-17 12:18

    To fix this, add an appropriate preserveAspectRatio attribute to the <image> in your <pattern> to tell it that you want the image to be zoomed and sliced.

    <pattern id="image" patternUnits="userSpaceOnUse" width="83.38" height="100" x="0" y="0">
      <image xlink:href="http://goo.gl/kVVuy1"
             x="0" y="0" width="100%" height="100%"
             preserveAspectRatio="xMinYMin slice"/>
    </pattern>
    

    Demo here

    0 讨论(0)
  • 2021-01-17 12:20

    If background-position is needed as well as background-size: cover;

    preserveAspectRatio="xMidYMid slice" // Will center the image

    This link has all options available for the preserveAspectRatio attribute. And can help with centering the image within the pattern

    All Options

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