Prevent SVG Text rotation with CSS

2019-08-18 11:23发布

I created an SVG circle with text element inside. I want to rotate SVG with the CSS transform but I don't want text element rotate.

.progress {
webkit-transform: rotate(-90deg);
    transform: rotate(-90deg);
	}
.progress__value {
  stroke-dasharray: 339.292;
    stroke-dashoffset: 339.292;
}
<div class="demo">
    <svg class="progress" width="100px" height="100px" viewBox="0 0 120 120">
<text x="44" class="counter" y="66" fill="#262626" style="
    font-weight: 700; font-size: 16px;
">30%</text>
        <circle cx="60" cy="60" r="54" fill="none" stroke="#DDDDDD" stroke-width="12" />
        <circle class="progress__value"  cx="60" cy="60" r="54" fill="none" stroke="#262626" stroke-width="12" />
    </svg>
</div>

标签: html css svg
1条回答
劫难
2楼-- · 2019-08-18 11:47

Create a group around the elements that you want to rotate.

rotate them

Adjust the rotation center to the center of the circle to avoid translating it

.inner {
    transform-origin: 60px 60px;
    transform: rotate(-90deg);
}
.progress__value {
    stroke-dasharray: 339.292;
    stroke-dashoffset: 339.292;
}
<div class="demo">
    <svg class="progress" width="100px" height="100px" viewBox="0 0 120 120">
        <text x="44" class="counter" y="66" fill="#262626" style="
    font-weight: 700; font-size: 16px;
">30%</text>
        <g class="inner">
        <circle cx="60" cy="60" r="54" fill="none" stroke="#DDDDDD" stroke-width="12" />
        <circle class="progress__value"  cx="60" cy="60" r="54" fill="none" stroke="#262626" stroke-width="12" />
        </g>
    </svg>
</div>

查看更多
登录 后发表回答