简体   繁体   English

如何将preserveAspectRatio添加到用作背景图像的svg

[英]how to add preserveAspectRatio to svg used as background-image

I'm having trouble with a svg pattern background that is not repeating on the x-axis in IE11 (and probably other IEs) and the Android native browser. 我在svg模式背景上遇到麻烦,该背景在IE11(可能还有其他IE)和Android本机浏览器的x轴上没有重复。
Apparently I should add preserveAspectRatio: "none slice" to the svg element, but what if the svg is used as background-image? 显然我应该向svg元素添加preserveAspectRatio: "none slice" ,但是如果svg用作背景图像怎么办?

CSS CSS

header.logoheader:after {
    background-image: url('patroon5.svg');
    background-size: auto 100%;
    background-repeat: repeat-x;
    content: "";
    display: block;
    height: 100px;
    left: 0;
    position: absolute;
    top: 40px;
    width: 100%;
}

(I'm using the :after selector so the transparent pattern covers the div) (我使用:after选择器,因此透明模式覆盖了div)

In my HTML there is no svg element to add the preserveAspectRatio to. 在我的HTML没有SVG元素的添加preserveAspectRatio到。 I'm thinking this is because of the :after selector. 我在想这是因为:after选择器。

Anyone got an solution? 任何人都有解决方案吗?

您可以编辑背景图像svg文件以包含它,也可以使用SVG片段标识符,

background-image: url('patroon5.svg#svgView(preserveAspectRatio(none))');

Had this kind of problem before on IE. 以前在IE上有过这种问题。 To fix it you have to edit your svg file then remove the set width and height assuming you have viewBox set in there. 要解决此问题,您必须编辑svg文件,然后假定其中已设置viewBox然后删除设置的widthheight Then add preserveAspectRatio="xMinYMid" as an attribute. 然后添加preserveAspectRatio="xMinYMid"作为属性。

eg 例如

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     preserveAspectRatio="xMinYMid" viewBox="0 0 1024 1024" enable-background="new 0 0 1024 1024" xml:space="preserve">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM