非常教程

SVG参考手册

属性 | Attributes

preserveaspectratio

In some cases, typically when using the viewBox attribute, it is desirable that the graphics stretch to fit non-uniformly to take up the entire viewport. In other cases, it is desirable that uniform scaling be used for the purposes of preserving the aspect ratio of the graphics.

Attribute preserveAspectRatio indicates whether or not to force uniform scaling.

For all elements that support that attribute (see above), except for the <image> element, preserveAspectRatio only applies when a value has been provided for viewBox on the same element. For these elements, if attribute viewBox is not provided, then preserveAspectRatio is ignored.

For <image> elements, preserveAspectRatio indicates how referenced images should be fitted with respect to the reference rectangle and whether the aspect ratio of the referenced image should be preserved with respect to the current user coordinate system.

Usage context

Categories

None

Value

<align> <meetOrSlice>

Animatable

Yes

Normative document

SVG 1.1 (2nd Edition)

<align>The <align> parameter indicates whether to force uniform scaling and, if so, the alignment method to use in case the aspect ratio of the viewBox doesn't match the aspect ratio of the viewport. The <align> parameter must be one of the following strings:

  • none Do not force uniform scaling. Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle. (Note: if <align> is none, then the optional <meetOrSlice> value is ignored.)
  • xMinYMin - Force uniform scaling. Align the <min-x> of the element's viewBox with the smallest X value of the viewport. Align the <min-y> of the element's viewBox with the smallest Y value of the viewport.
  • xMidYMin - Force uniform scaling. Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport. Align the <min-y> of the element's viewBox with the smallest Y value of the viewport.
  • xMaxYMin - Force uniform scaling. Align the <min-x>+<width> of the element's viewBox with the maximum X value of the viewport. Align the <min-y> of the element's viewBox with the smallest Y value of the viewport.
  • xMinYMid - Force uniform scaling. Align the <min-x> of the element's viewBox with the smallest X value of the viewport. Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
  • xMidYMid (the default) - Force uniform scaling. Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport. Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
  • xMaxYMid - Force uniform scaling. Align the <min-x>+<width> of the element's viewBox with the maximum X value of the viewport. Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
  • xMinYMax - Force uniform scaling. Align the <min-x> of the element's viewBox with the smallest X value of the viewport. Align the <min-y>+<height> of the element's viewBox with the maximum Y value of the viewport.
  • xMidYMax - Force uniform scaling. Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport. Align the <min-y>+<height> of the element's viewBox with the maximum Y value of the viewport.
  • xMaxYMax - Force uniform scaling. Align the <min-x>+<width> of the element's viewBox with the maximum X value of the viewport. Align the <min-y>+<height> of the element's viewBox with the maximum Y value of the viewport.

<meetOrSlice>The <meetOrSlice> parameter is optional and, if provided, is separated from the <align> value by one or more spaces and then must be one of the following strings:

  • meet (the default) - Scale the graphic such that:
    • aspect ratio is preserved
    • the entire viewBox is visible within the viewport
    • the viewBox is scaled up as much as possible, while still meeting the other criteria

In this case, if the aspect ratio of the graphic does not match the viewport, some of the viewport will extend beyond the bounds of the viewBox (i.e., the area into which the viewBox will draw will be smaller than the viewport).

  • slice - Scale the graphic such that:
    • aspect ratio is preserved
    • the entire viewport is covered by the viewBox
    • the viewBox is scaled down as much as possible, while still meeting the other criteria

In this case, if the aspect ratio of the viewBox does not match the viewport, some of the viewBox will extend beyond the bounds of the viewport (i.e., the area into which the viewBox will draw is larger than the viewport).

Example

Elements

The following elements can use the preserveAspectRatio attribute

  • <svg>
  • <symbol>
  • <image>
  • <feImage>
  • <marker>
  • <pattern>
  • <view>

Edit this page on MDN

 © 2005–2017 Mozilla Developer Network and individual contributors.

Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio

属性 | Attributes相关

1.accent-height(焦点高度)
2.accumulate(积累)
3.additive
4.alignment-baseline(对齐基线)
5.ascent
6.attributename(属性名字)
7.Attributes(属性)
8.attributetype(属性类型)
9.azimuth(方位)
10.basefrequency
11.baseline-shift(基线漂移)
12.baseprofile
13.begin(开始)
14.bias(偏压)
15.calcmode
16.class
17.clip(剪辑)
18.clip-path(剪辑路径)
19.clip-rule(剪辑规则)
20.clippathunits
21.color(颜色)
22.color-interpolation(彩色插值)
23.color-interpolation-filters(色彩插值滤波器)
24.color-profile(彩色轮廓)
25.color-rendering(显色)
26.contentscripttype
27.contentstyletype
28.cursor(光标)
29.cx
30.cy
31.d
32.diffuseconstant
33.direction(方向)
34.display(显示)
35.divisor
36.dominant-baseline(显性基线)
37.dur
38.dx
39.dy
40.edgemode
41.elevation
42.end
43.externalresourcesrequired
44.fill
45.fill-opacity
46.fill-rule
47.filter
48.filterres
49.filterunits
50.flood-color
51.flood-opacity
52.font-family
53.font-size
54.font-size-adjust
55.font-stretch
56.font-style
57.font-variant
58.font-weight
59.fr
60.from
61.fx
62.fy
63.gradienttransform
64.gradientunits
65.height
66.href
67.image-rendering
68.in
69.in2
70.k1
71.k2
72.k3
73.k4
74.kernelmatrix
75.kernelunitlength
76.kerning
77.keysplines
78.keytimes
79.lengthadjust
80.letter-spacing
81.lighting-color
82.limitingconeangle
83.local
84.marker-end
85.marker-mid
86.marker-start
87.markerheight
88.markerunits
89.markerwidth
90.mask
91.maskcontentunits
92.maskunits
93.max
94.min
95.mode
96.numoctaves
97.opacity
98.operator
99.order
100.overflow
SVG

可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集),用于描述二维矢量图形的一种图形格式。它由万维网联盟制定,是一个开放标准。