此頁面為 Module:SVG Data URL 的說明文件
本模塊使得可以在頁面中嵌入SVG圖像。用途包括:實現隨參數變化的內容;使用currentColor[1];避免小而雜亂的圖被上傳到共享站;[2]……
本站沒有啟用<svg>標籤,顯示SVG的原理是使用Data URL為<img>提供源。
參數svg填SVG代碼。前面不要填<?xml …或<!DOCTYPE svg …,直接以<svg開頭。
沒指定xmlns的話,會自動加上xmlns="http://www.w3.org/2000/svg"。
{{svg|svg=
<svg width="300" height="200">
<rect width="100%" height="100%" fill="darkturquoise"/>
<circle cx="150" cy="100" r="80" fill="cadetblue"/>
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>
}}
<img srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='300'%20height='200'%3E%3Crect%20width='100%25'%20height='100%25'%20fill='darkturquoise'/%3E%3Ccircle%20cx='150'%20cy='100'%20r='80'%20fill='cadetblue'/%3E%3Ctext%20x='150'%20y='125'%20font-size='60'%20text-anchor='middle'%20fill='white'%3ESVG%3C/text%3E%3C/svg%3E" alt="SVG">
除svg、src、srcset以外的任何參數,如width、alt、class、style等,會被原封不動地添加到<img>上。不需要加引號。
{{svg
|width=100
|style=border-radius:6px; box-shadow: 0 2px 6px rgb(0 0 0 / 0.5);
|alt=表情
|svg=
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 93.1 81.3">
<path d="M93.1,44.8v36.5H4.6C0,64.1-.3,51.9,.1,34.3c0-5.9,.3-17.6,5.8-21.7,7,.7,9.8,7.5,16,9.6,9.7-3.7,20.1-4.6,30.1-4.8C58,9.9,62.1,1.5,71.6,0c5.3,2.5,6,7,7.1,11.4,1.6,6.2,2.7,12.5,4.7,18.6,1.9,5.6,3.5,11.8,9.7,14.8Z"/>
<circle cx="25.8" cy="47.7" r="9" fill="#fff"/>
<circle cx="58.2" cy="42" r="9" fill="#fff"/>
<circle cx="22.6" cy="45.6" r="5"/>
<circle cx="62" cy="43.6" r="5"/>
</svg>
}}
<img srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2093.1%2081.3'%3E%3Cpath%20d='M93.1,44.8v36.5H4.6C0,64.1-.3,51.9,.1,34.3c0-5.9,.3-17.6,5.8-21.7,7,.7,9.8,7.5,16,9.6,9.7-3.7,20.1-4.6,30.1-4.8C58,9.9,62.1,1.5,71.6,0c5.3,2.5,6,7,7.1,11.4,1.6,6.2,2.7,12.5,4.7,18.6,1.9,5.6,3.5,11.8,9.7,14.8Z'/%3E%3Ccircle%20cx='25.8'%20cy='47.7'%20r='9'%20fill='%23fff'/%3E%3Ccircle%20cx='58.2'%20cy='42'%20r='9'%20fill='%23fff'/%3E%3Ccircle%20cx='22.6'%20cy='45.6'%20r='5'/%3E%3Ccircle%20cx='62'%20cy='43.6'%20r='5'/%3E%3C/svg%3E" alt="表情" style="border-radius:6px; box-shadow: 0 2px 6px rgba(0, 0, 0, .3);" width="100">
作為文字的"和'需要換為XML實體("→",'→'),否則兩者會顛倒。
{{svg|svg=
<svg width="300" height="100" style="background:#fff">
<text x="150" y="40" font-size="20" text-anchor="middle">字符:"雙引號" '單引號'</text>
<text x="150" y="80" font-size="20" text-anchor="middle">實體:"雙引號" '單引號'</text>
</svg>
}}
<img srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='300'%20height='100'%20style='background:%23fff'%3E%3Ctext%20x='150'%20y='40'%20font-size='20'%20text-anchor='middle'%3E%E5%AD%97%E7%AC%A6%EF%BC%9A'%E5%8F%8C%E5%BC%95%E5%8F%B7'%20%22%E5%8D%95%E5%BC%95%E5%8F%B7%22%3C/text%3E%3Ctext%20x='150'%20y='80'%20font-size='20'%20text-anchor='middle'%3E%E5%AE%9E%E4%BD%93%EF%BC%9A%26quot%3B%E5%8F%8C%E5%BC%95%E5%8F%B7%26quot%3B%20%26apos%3B%E5%8D%95%E5%BC%95%E5%8F%B7%26apos%3B%3C/text%3E%3C/svg%3E" alt="SVG">
當沒有使用svg參數時,模板進入CSS模式。原理是使用clip-path: path(...) CSS屬性與函數。
{{svg}}參數(標「*」的為必填參數):
tag
|
最外層元素所用HTML標籤。默認為span
|
|---|---|
width*
|
寬度,為CSS長度(<length>數據類型)或數字(表示px)
|
height*
|
高度,同上。width和height暫時都是必填,若需自動推導請提出討論。
|
viewBox
|
作用同<svg>的viewBox。
|
style
|
CSS樣式。 |
匿名參數1*
|
內部元素,見下。可以放入多個元素。 |
{{svg/path}}d或匿名參數1*
|
SVG路徑,見CSS path()函數的參數。
|
|---|---|
fill
|
填充,語法同CSS background屬性。默認為黑色。
|
fill-rule
|
見CSS path()函數的參數。
|
style
|
CSS樣式。 |
不支持stroke
展示了fill=currentColor。
{{svg
| width = 10em | height = 8.733em | viewBox = 0 0 93.1 81.3
| style = color: green; border-radius: 6px; box-shadow: 0 2px 6px rgb(0 0 0 / 0.5);
| {{svg/path
| M93.1,44.8v36.5H4.6C0,64.1-.3,51.9,.1,34.3c0-5.9,.3-17.6,5.8-21.7,7,.7,9.8,7.5,16,9.6,9.7-3.7,20.1-4.6,30.1-4.8C58,9.9,62.1,1.5,71.6,0c5.3,2.5,6,7,7.1,11.4,1.6,6.2,2.7,12.5,4.7,18.6,1.9,5.6,3.5,11.8,9.7,14.8Z
| fill = currentColor
}}
}}
| |||||||||||||||||||||||||||||||||