CSS x Property
The CSS x property specifies the horizontal position of certain SVG elements within the SVG coordinate system.
It is a CSS geometry property that can position SVG rectangles, images, and nested SVG viewports. The property accepts length and percentage values and provides a CSS alternative to the SVG x presentation attribute on supported elements.
On This Page
Syntax
The x property accepts a CSS length or percentage that specifies a horizontal coordinate.
selector {
x: <length-percentage>;
}
Common declarations include:
rect {
x: 20px;
}
.image-element {
x: 40px;
}
.nested-svg {
x: 25%;
}
.offset-rectangle {
x: -10px;
}
The property also accepts CSS-wide keywords such as initial, inherit, unset, revert, and revert-layer.
Unlike the SVG x attribute, CSS declarations normally require units for nonzero lengths. A unitless 0 is valid.
Property Values
| Value | Description |
|---|---|
<length> |
Specifies a horizontal coordinate using a CSS length, such as px, em, or rem. |
<percentage> |
Specifies a horizontal coordinate relative to the width of the current SVG viewport. |
0 |
Positions the element at the horizontal origin of its current SVG coordinate system. This is the initial value. |
initial |
Resets the property to its initial value, 0. |
inherit |
Uses the computed x value of the parent element. |
unset |
Resets the property to its initial value because x is not inherited. |
revert |
Reverts the property according to the CSS cascade. |
revert-layer |
Reverts the property according to the previous cascade layer. |
Positive values generally move an element toward increasing horizontal coordinates, while negative values move it toward decreasing coordinates. The visible direction depends on the SVG coordinate system and any transformations applied to it.
How It Works
SVG uses a coordinate system to position graphical elements. In the default coordinate system, the horizontal axis increases from left to right, and the vertical axis increases from top to bottom.
The x property specifies a horizontal position for supported SVG elements.
rect {
x: 40px;
}
For an SVG rectangle, this positions its left edge at the horizontal coordinate of 40 units in the current user coordinate system when the CSS pixel unit maps directly to a user unit.
The vertical position is controlled separately using the y property.
rect {
x: 40px;
y: 20px;
}
These declarations position the rectangle horizontally and vertically without changing its width or height.
The x property affects the element's geometry rather than the layout of ordinary HTML elements.
Supported SVG Elements
The CSS x property applies to specific SVG elements that support an x geometry coordinate.
| SVG Element | How x Is Used |
|---|---|
<rect> |
Sets the horizontal coordinate of the rectangle's left edge. |
<image> |
Sets the horizontal coordinate of the image viewport. |
<svg> |
Sets the horizontal position of a nested SVG viewport where the property applies. |
<foreignObject> |
Sets the horizontal position of the rectangular region containing foreign content. |
The property is not a general-purpose positioning property for all SVG elements. For example, a circle uses cx to specify its center's horizontal coordinate.
circle {
cx: 80px;
cy: 60px;
}
Similarly, SVG lines use endpoint coordinates such as x1 and x2 rather than x.
SVG Coordinate System
Understanding the SVG coordinate system is important when using the x property.
Consider an SVG element with a viewBox that establishes a coordinate system 300 units wide and 150 units high.
<svg viewBox="0 0 300 150">
<rect class="positioned-box" width="80" height="40" />
</svg>
The rectangle can be positioned using CSS:
.positioned-box {
x: 50px;
y: 30px;
}
With the default coordinate orientation, the rectangle begins 50 units from the left and 30 units from the top of the viewBox.
Understanding the viewBox
The SVG viewBox attribute defines the coordinate region displayed inside an SVG viewport.
Its four values represent the minimum horizontal coordinate, minimum vertical coordinate, width, and height.
viewBox="0 0 300 150"
If the SVG is scaled to fit a larger or smaller area, the coordinates scale with the SVG drawing.
Transformations and nested coordinate systems can change how these positions appear on screen.
Length Values
The x property accepts absolute and relative CSS length units.
Using Pixels
The px unit is commonly used when positioning SVG elements.
.rectangle {
x: 60px;
}
In a typical SVG viewBox coordinate system, CSS pixels correspond to user units before any scaling transformations.
Using em
The em unit is relative to the font size of the element.
.rectangle {
font-size: 20px;
x: 2em;
}
In this example, 2em corresponds to 40 pixels.
Using rem
The rem unit is relative to the root element's font size.
.rectangle {
x: 3rem;
}
If the root font size is 16 pixels, the horizontal coordinate corresponds to 48 pixels.
Using Negative Lengths
Negative values are valid and can position part or all of an SVG element outside the visible viewport.
.rectangle {
x: -20px;
}
Whether the element remains visible depends on its dimensions, the viewport, and applicable clipping or overflow behavior.
Percentage Values
Percentage values are resolved relative to the width of the current SVG viewport.
.rectangle {
x: 25%;
}
If the viewport's coordinate width is 400 units, 25% corresponds to a horizontal offset of 100 units.
Percentage values are useful when positioning SVG elements relative to the available viewport width.
Percentage Positioning Example
<svg viewBox="0 0 400 200">
<rect class="percentage-box" width="60" height="40" />
</svg>
.percentage-box {
x: 50%;
y: 40px;
}
The rectangle begins at the horizontal midpoint of the SVG viewport. Its left edge, not its center, is positioned at that coordinate.
To center the entire rectangle, its width must also be considered.
CSS Property vs. SVG Attribute
SVG elements can specify horizontal coordinates using the SVG x attribute or the CSS x property.
Using the SVG Attribute
<rect x="40" y="20" width="80" height="50" />
The SVG attribute directly defines the rectangle's position.
Using CSS
<rect class="css-positioned" width="80" height="50" />
.css-positioned {
x: 40px;
y: 20px;
}
Both approaches can position the rectangle at the same coordinates.
Which Takes Priority?
For supported SVG geometry properties, the corresponding SVG attributes act as presentation attributes.
When an author CSS declaration and an SVG presentation attribute specify conflicting values, the CSS declaration generally takes precedence according to the cascade.
<rect class="box" x="10" width="80" height="40" />
.box {
x: 60px;
}
In this example, the CSS declaration positions the rectangle at 60px rather than the 10 specified by the SVG attribute.
The SVG attribute remains useful for defining default geometry directly in the markup.
x vs. transform
The x and transform properties can both change where an SVG element appears, but they work differently.
| Property | Purpose |
|---|---|
x |
Specifies the element's horizontal geometry coordinate. |
transform |
Applies transformations such as translation, rotation, and scaling to an element's coordinate system. |
For example:
.geometry-position {
x: 40px;
}
.transformed-position {
transform: translateX(40px);
}
The first declaration changes the element's horizontal geometry coordinate. The second applies a horizontal translation to the rendered element.
For supported SVG elements, use x when setting the actual geometry coordinate and transform when a transformation is needed.
Unlike x, transforms can also be applied to many other SVG elements and ordinary HTML elements.
Examples
Example 1: Positioning a Rectangle
The following declaration positions an SVG rectangle 40 units from the horizontal origin.
.rectangle {
x: 40px;
y: 20px;
}
The rectangle's width and height remain unchanged.
Example 2: Using Percentage Positioning
A percentage can position an SVG element relative to the viewport width.
.percentage-rectangle {
x: 30%;
}
This places the rectangle's left edge at 30% of the viewport width.
Example 3: Positioning an SVG Image
The x property can position an SVG <image> element.
.svg-image {
x: 25px;
y: 15px;
}
The image is positioned using the SVG coordinate system rather than ordinary HTML document flow.
Example 4: Using calc()
CSS math functions can calculate a horizontal coordinate.
.calculated-position {
x: calc(50% - 40px);
}
For an 80-unit-wide rectangle, this positions its left edge 40 units before the horizontal midpoint, centering it when the relevant units map directly.
Example 5: Moving a Rectangle with a Transition
The x property is animatable as a length or percentage value.
.moving-rectangle {
x: 20px;
transition: x 0.4s ease;
}
.moving-rectangle:hover {
x: 120px;
}
In browsers supporting CSS geometry property transitions, the rectangle moves horizontally when hovered.
Example 6: Using a CSS Custom Property
Custom properties can make SVG coordinates easier to maintain.
.custom-position {
--horizontal-position: 60px;
x: var(--horizontal-position);
}
Changing the custom property updates the horizontal coordinate.
Inheritance
The CSS x property is not inherited. Each supported SVG element uses its own specified value or the initial value unless inheritance is explicitly requested.
.parent {
x: 40px;
}
.child {
x: inherit;
}
The inherit keyword explicitly uses the parent's computed value, although the property only has a positioning effect on applicable SVG elements.
The initial value is 0.
In most cases, define the horizontal coordinate directly on each SVG element that needs positioning.
Practical Example
This example demonstrates how the CSS x property positions three SVG rectangles at different horizontal coordinates.
HTML
<svg class="x-property-demo" viewBox="0 0 360 160" role="img" aria-label="Three colored rectangles positioned at different horizontal coordinates">
<rect class="x-box-one" width="70" height="35" />
<rect class="x-box-two" width="70" height="35" />
<rect class="x-box-three" width="70" height="35" />
</svg>
CSS
.x-property-demo {
display: block;
width: 100%;
max-width: 360px;
height: auto;
border: 1px solid #ccc;
background: #f5f5f5;
}
.x-box-one {
x: 20px;
y: 15px;
fill: #3498db;
}
.x-box-two {
x: 90px;
y: 65px;
fill: #2ecc71;
}
.x-box-three {
x: 180px;
y: 110px;
fill: #e67e22;
}
Result: The blue rectangle begins at horizontal coordinate 20, the green rectangle begins at 90, and the orange rectangle begins at 180. Each rectangle also has a different vertical position controlled by y.
The example demonstrates how CSS geometry properties can position SVG shapes without requiring coordinate attributes directly in the SVG markup.
Common Mistakes
Using x on Ordinary HTML Elements
The x property is not a general-purpose positioning property for HTML elements.
/* Does not position an HTML div */
div {
x: 40px;
}
For ordinary HTML positioning, use properties such as left, inset-inline-start, or transform as appropriate.
Omitting Units in CSS
Nonzero CSS length values require a unit.
/* Incorrect */
rect {
x: 40;
}
/* Correct */
rect {
x: 40px;
}
SVG attributes can use unitless numbers, but CSS declarations follow CSS length syntax.
Confusing x with cx
SVG rectangles use x to specify a horizontal coordinate, while circles and ellipses use cx for their horizontal center coordinate.
rect {
x: 40px;
}
circle {
cx: 40px;
}
Use the geometry property appropriate for the SVG element.
Assuming Percentages Refer to the Element Width
A percentage used with x is resolved relative to the SVG viewport width, not the width of the positioned rectangle.
For example, x: 50% places the rectangle's left edge at the horizontal midpoint rather than centering the rectangle itself.
Ignoring the viewBox
The SVG viewBox and coordinate transformations affect how geometry coordinates map to the displayed image.
Check the active coordinate system when an element appears at an unexpected location.
Confusing Geometry Positioning with Transforms
The x property changes a supported element's geometry coordinate. A transform applies an additional transformation to the element.
Although the visible results can sometimes appear similar, the properties serve different purposes.
Important Considerations
- SVG-specific property: The
xproperty applies to supported SVG geometry elements, not ordinary HTML elements. - Initial value: The initial horizontal coordinate is
0. - Inheritance: The property is not inherited unless explicitly requested.
- Length values: Nonzero CSS lengths require units such as
pxorem. - Percentages: Percentage coordinates are resolved relative to the SVG viewport width.
- Coordinate systems: The SVG
viewBoxand transformations affect the displayed position. - SVG attributes: CSS declarations can override corresponding SVG presentation attributes through the cascade.
- Animation: The property can be animated as a length or percentage value where supported.
- Responsive SVG: Percentage values and scalable viewBox coordinates can help position elements in responsive graphics.
- Element differences: Other SVG shapes use different geometry properties, such as
cx,x1, andx2.
Use x when you need to control the horizontal geometry coordinate of a supported SVG element. For general movement or positioning of other elements, choose the appropriate layout or transform property.
Browser Support
The CSS x geometry property is supported in modern versions of major browsers, including Chrome, Edge, Firefox, Safari, and Opera, although support for SVG geometry properties has historically varied across browser versions.
SVG x attributes have broader historical support than CSS geometry declarations. For compatibility with older browsers, consider defining essential coordinates using SVG attributes.
For current compatibility information, visit Can I Use? .
Specifications
The CSS x property is defined in the SVG 2 specification as a geometry property for supported SVG elements.
