viewBox
viewBox 屿§å 许æå®ä¸ä¸ªç»å®çä¸ç»å¾å½¢ä¼¸å±ä»¥éåºç¹å®ç容å¨å ç´ ã
viewBox 屿§ç弿¯ä¸ä¸ªå
å« 4 ä¸ªåæ°çå表 min-x, min-y, width and heightï¼ä»¥ç©ºæ ¼æè
éå·åéå¼ï¼å¨ç¨æ·ç©ºé´ä¸æå®ä¸ä¸ªç©å½¢åºåæ å°å°ç»å®çå
ç´ ï¼æ¥ç屿§preserveAspectRatioã
ä¸å 许宽度åé«åº¦ä¸ºè´å¼ï¼0 åç¦ç¨å ç´ çåç°ã
示ä¾
html
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<!--
with relative unit such as percentage, the visual size
of the square looks unchanged regardless of the viewBox
-->
<rect x="0" y="0" width="100%" height="100%" />
<!--
with a large viewBox the circle looks small
as it is using user units for the r attribute:
4 resolved against 100 as set in the viewBox
-->
<circle cx="50%" cy="50%" r="4" fill="white" />
</svg>
<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
<!--
with relative unit such as percentage, the visual size
of the square looks unchanged regardless of the viewBox
-->
<rect x="0" y="0" width="100%" height="100%" />
<!--
with a small viewBox the circle looks large
as it is using user units for the r attribute:
4 resolved against 10 as set in the viewBox
-->
<circle cx="50%" cy="50%" r="4" fill="white" />
</svg>
<svg viewBox="-5 -5 10 10" xmlns="http://www.w3.org/2000/svg">
<!--
The point of coordinate 0,0 is now in the center of the viewport,
and 100% is still resolve to a width or height of 10 user units so
the rectangle looks shifted to the bottom/right corner of the viewport
-->
<rect x="0" y="0" width="100%" height="100%" />
<!--
With the point of coordinate 0,0 in the center of the viewport the
value 50% is resolve to 5 which means the center of the circle is
in the bottom/right corner of the viewport.
-->
<circle cx="50%" cy="50%" r="4" fill="white" />
</svg>
è¿ä¸ªå±æ§ä¼åå° preserveAspectRatio çå½±åã
夿³¨ï¼width æè
height çå¼ï¼å°äºæçäº 0 çæ
åµä¸ï¼è¿ä¸ªå
ç´ å°ä¸ä¼è¢«æ¸²æåºæ¥ã
æ <marker>, <pattern>, <svg>, <symbol>, å <view> çäºä¸ª svg å
ç´ å¯ä»¥æè¿ä¸ªå±æ§ã
Usage context
| Categories | None |
|---|---|
| Value | See above |
| Animatable | Yes |
Elements
ä¸é¢çå ç´ å¯ä»¥ä½¿ç¨ viewBox 屿§
è§è
| Specification |
|---|
| Scalable Vector Graphics (SVG) 2 > # ViewBoxAttribute > |