files/en-us/web/css/reference/properties/x/index.md
The x CSS property defines the x-axis coordinate of the top left corner of the SVG {{SVGElement("rect")}} shape, {{SVGElement("image")}} image, {{SVGElement("foreignObject")}} viewport or nested {{SVGElement("svg")}} viewport relative to the nearest <svg> ancestor's user coordinate system. If present, it overrides the element's {{SVGAttr("x")}} attribute.
[!NOTE] The
xproperty only applies to {{SVGElement("rect")}}, {{SVGElement("image")}}, {{SVGElement("foreignObject")}}, and {{SVGElement("svg")}} elements nested in an {{SVGElement("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.
/* length and percentage values */
x: 40px;
x: 40%;
/* Global values */
x: inherit;
x: initial;
x: revert;
x: revert-layer;
x: unset;
The {{cssxref("length")}} and {{cssxref("percentage")}} values denote the x-axis coordinate position of the top left corner of the SVG element container.
{{cssxref("length")}}
{{cssxref("percentage")}}
{{CSSInfo}}
{{csssyntax}}
This example demonstrates the basic use case of x, and how the CSS x property takes precedence over the x attribute.
We include four identical SVG <rect> elements; their x and {{SVGAttr("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>
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;
}
{{EmbedLiveSample("Defining the x-axis coordinate of SVG shapes", "300", "180")}}
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}}
{{Compat}}
x, {{cssxref("cx")}}, {{cssxref("cy")}}, {{cssxref("r")}}, {{cssxref("rx")}}, {{cssxref("ry")}}, {{cssxref("y")}}, {{cssxref("width")}}, {{cssxref("height")}}