我試圖使用SVG的一部分作爲平鋪背景。但是,我遇到了很大的問題,使其正常工作。如何在CSS背景中正確製作SVG雪碧圖塊?
我認爲照片會說明最好的問題: demonstration picture
這是我的CSS:
body {
background: url("tiletest.svg#svgView(viewBox(120 32 150 64))");
background-size: 30px 32px;
}
這裏是我使用的示範SVG的代碼:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 21.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg height="100" width="360" version="1.1" id="Lager_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 360 100" style="enable-background:new 0 0 360 100;" xml:space="preserve">
<style type="text/css">
.st0{fill:url(#SVGID_1_);}
.st1{fill:#13FF00;}
.st2{fill:#2732FF;}
</style>
<g>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="9.094947e-13" y1="9.094947e-13" x2="100" y2="100">
<stop offset="0" style="stop-color:#E7FF00"/>
<stop offset="1" style="stop-color:#FF0000"/>
</linearGradient>
<rect class="st0" width="100" height="100"/>
</g>
<polygon class="st1" points="174.68,0 190.92,32.92 227.25,38.2 200.96,63.82 207.17,100 174.68,82.92 142.19,100 148.39,63.82
122.11,38.2 158.43,32.92 "/>
<circle class="st2" cx="310" cy="50" r="50"/>
</svg>
正如你可以看到SVG中的完整viewbox是0 0 360 100,當我在CSS中調用SVG時,我給它一個120的新viewbox 32 150 64,以及相應地更改背景大小(雖然我很確定這無關緊要,因爲無論如何,由viewbox定義的svg應該擴大以填充容器,無論大小,對吧?)。
我試着用SVGA中的viewbox,帶寬度和高度,用preserveAspectRatio屬性來處理,到目前爲止沒有任何工作。我究竟做錯了什麼?
也許例如刪除空格tiletest.svg#svgView(viewBox(120,32,150,64)) –
您使用的瀏覽器是?片段標識符等在所有瀏覽器中都得不到很好的支持。 – Ruskin