You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SVG circle元素的r属性百分比参照对象是什么?

关于SVG circle元素r属性百分比参照对象的疑问

示例代码

CSS代码:

.container{
    width: 100px;
    height: 200px;
    background-color: red;
    resize: both;     /* Just to make it... */
    overflow: hidden; /* ...resizable for testing. */
}

HTML代码:

<div class="container">
    <svg width="100%" height="100%">
        <circle cx="50%" cy="50%" r="25%" fill="black"></circle>
    </svg>
</div>

问题解答

你观察到cx参照容器宽度、cy参照容器高度是正确的,而<circle>的r属性使用百分比时,参照的是当前SVG视口(即与.container尺寸一致的SVG元素)的宽度的百分比。

具体表现:

  • 调整.container的宽度时,r的实际长度会随宽度的百分比变化;
  • 仅调整.container的高度时,r的实际长度不会改变,因为它只和视口宽度挂钩。

比如.container宽度为100px时,r="25%"对应25px;宽度拉到200px时,r就变成50px,哪怕高度还是200px。


内容的提问来源于stack exchange,提问作者Peppe L-G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:35:13