CSS transform on SVG text element not working in Safari
试图在父 SVG 中放置一个电池指示器。SVG
1 2 3 4 5 6 | <text text-anchor="middle" dominant-baseline="middle" style="transform:translate(50%,98%) scale(.2); font:700 13px sans-serif;fill:#deba78" >24.2%</text> |
Codepen https://codepen.io/niwsa/pen/rNNBKEg?editors=1000
将
您可以给文本一些属性,例如 x 和 y,而不是翻译文本。您可以更改字体大小,而不是缩放文本。
对于路径,您可以选择像这样的 svg 转换:
1 2 3 4 5 6 7 8 9 10 11 12 | body { width: 200px; } .bg { fill: #beeb1b; } .cap { fill: #aaa8a9; } .trunk { fill: #231f20; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <svg xmlns="http://www.w3.org/2000/svg" tabindex="0" viewBox="0 0 351.33 722" aria-labelledby="bottletitle bottledesc" role="img"> <title id="bottletitle"> Bottle <desc id="bottledesc"> Bottle with battery indicator inside </desc> <g data-name="Layer 4" class="bg"> <rect width="351.33" height="722" rx="23.33" ry="23.33"/> </g> <g data-name="Layer 3" class="cap"> <rect x="146.81" y="60.9" width="57.71" height="73.67"/> </g> <g data-name="Layer 2" class="trunk"> <path d="M173,153.25h57.75V223s1.08,25.33,30.41,56c27.06,28.29,35.34,60.33,35,71.33-.21,7,0,324.67,0,324.67s-3.33,7.33-9.33,7.33H117.12s-9-.33-9-6.66v-325s-.33-33,30.34-67c0,0,34.33-32.67,34.33-60.67S173.33,153.63,173,153.25Z" transform="translate(-26.46 -18.67)"/> </g> <svg viewBox="0 0 24 24"> <defs> <linearGradient id="lg" x1="0.5" y1="1" x2="0.5" y2="0"> <stop offset="0%" stop-opacity="1" stop-color="#2ecc71"/> <stop offset="24.2%" stop-opacity="1" stop-color="#2ecc71"/> <stop offset="24.2%" stop-opacity="0" stop-color="#2ecc71"/> <stop offset="100%" stop-opacity="0" stop-color="#2ecc71"/> </linearGradient> </defs> <path fill="url(#lg)" d="M15.67 4H14V2h-4v2H8.33C7.6 4 7 4.6 7 5.33v15.33C7 21.4 7.6 22 8.33 22h7.33c.74 0 1.34-.6 1.34-1.33V5.33C17 4.6 16.4 4 15.67 4z" stroke="#fff" transform="scale(.5,.5) rotate(90,12 12) translate(45,-12)"/> <text text-anchor="middle" dominant-baseline="middle" style="font:700 2.5px sans-serif;fill:#deba78" x="12" y="23"> 24.2% </text> </svg> </svg> |