大网数据广告活动
Web 标准与前端开发

`x` CSS property

· 52CE 我爱测

The **`x`** CSS property defines the x-axis coordinate of the top left corner of the SVG rect shape, image image, foreignObject viewport or nested svg viewport relative to the nearest `<svg>` ancestor's user coordinate system. If present, it overrides the element's x attribute.

The x CSS property defines the x-axis coordinate of the top left corner of the SVG rect shape, image image, foreignObject viewport or nested svg viewport relative to the nearest <svg> ancestor's user coordinate system. If present, it overrides the element's x attribute.

[!NOTE] The x property only applies to rect, image, foreignObject, and svg elements nested in an svg. It has no effect on the outermost <svg> elements itself, and does not apply to other SVG elements nor to HTML elements or pseudo-elements.

Syntax

/* length and percentage values */
x: 40px;
x: 40%;

/* Global values */
x: inherit;
x: initial;
x: revert;
x: revert-layer;
x: unset;

Values

The length and percentage values denote the x-axis coordinate position of the top left corner of the SVG element container.

  • length

    • : As an absolute or relative length, it can be expressed in any unit allowed by the CSS <length> data type.
  • percentage

    • : Percentages refer to the width of the SVG viewBox, if declared, otherwise, the percentage refers to the width of the current SVG viewport.

Formal definition

Formal syntax

Examples

Defining the x-axis coordinates of SVG shapes

This example demonstrates the basic use case of x, and how the CSS x property takes precedence over the x attribute.

HTML

We include four identical SVG <rect> elements; their x and y attributes values are all 10, meaning the four rectangles are all in the same location, 10px from the top and left corner of the SVG viewport.

<svg xmlns="http://www.w3.org/2000/svg">
  <rect width="100" height="100" x="10" y="10" />
  <rect width="100" height="100" x="10" y="10" />
  <rect width="100" height="100" x="10" y="10" />
  <rect width="100" height="100" x="10" y="10" />
</svg>

CSS

We style all the rectangles to have a black border and be slightly transparent, so overlapping rectangles are visible. We provide each rectangle with different fill and x values.

svg {
  border: 1px solid;
  width: 300px;
}

rect {
  fill: none;
  stroke: black;
  opacity: 0.8;
}

rect:nth-of-type(2) {
  x: 3em;
  fill: red;
}

rect:nth-of-type(3) {
  x: 180px;
  fill: yellow;
}

rect:nth-of-type(4) {
  x: 50%;
  fill: orange;
}

Results

defining_the_x-axis_coordinates_of_svg_shapes

The left edges of the rectangles are at 10 (from the attribute), 3em, 180px, and 50%, respectively. The SVG is 300px wide, so the last rectangle's left side is at the 150px mark.

Specifications

Browser compatibility

See also

  • SVG x attribute
  • Geometry properties: x, cx, cy, r, rx, ry, y, width, height
  • fill
  • stroke
  • paint-order
  • basic-shape data type

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。