CSS y Property

The CSS y property specifies the vertical position of certain SVG elements within the SVG coordinate system.

It is an SVG geometry property that controls the vertical coordinate of supported elements, including rectangles, images, and nested SVG viewports. The property accepts CSS length and percentage values and can be used instead of the SVG y attribute when supported.

Syntax

The y property accepts a CSS length or percentage value that determines a vertical coordinate.

selector {
  y: <length-percentage>;
}

Common declarations include:

rect {
  y: 20px;
}
.svg-image {
  y: 40px;
}
.nested-svg {
  y: 25%;
}
.offset-rectangle {
  y: -10px;
}

The property also accepts CSS-wide keywords such as initial, inherit, unset, revert, and revert-layer.

Nonzero CSS length values require units. For example, y: 20px is valid, while y: 20 is not valid CSS.

Property Values

Value Description
<length> Specifies a vertical coordinate using a CSS length such as px, em, or rem.
<percentage> Specifies a vertical coordinate relative to the height of the current SVG viewport.
0 Positions the element at the vertical 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 y value of the parent element.
unset Resets the property to its initial value because y 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 vertical coordinates. In the default SVG coordinate system, this means moving downward. Negative values move toward decreasing vertical coordinates.

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, while the vertical axis increases from top to bottom.

The y property determines the vertical position of supported SVG elements.

rect {
  y: 40px;
}

For an SVG rectangle, this places its top edge at a vertical coordinate of 40 units when CSS pixels correspond directly to SVG user units.

The horizontal position is controlled separately by the x property.

rect {
  x: 30px;
  y: 40px;
}

These declarations position the rectangle at horizontal coordinate 30 and vertical coordinate 40 without changing its dimensions.

The property changes the element's geometry rather than the normal document flow of HTML content.

Supported SVG Elements

The CSS y property applies to SVG elements that support a vertical geometry coordinate.

SVG Element How y Is Used
<rect> Sets the vertical coordinate of the rectangle's top edge.
<image> Sets the vertical coordinate of the image viewport.
<svg> Sets the vertical position of a nested SVG viewport where the property applies.
<foreignObject> Sets the vertical coordinate of the rectangular region containing foreign content.

Not every SVG element uses the y geometry property. Circles and ellipses use cy to define their vertical center coordinate.

circle {
  cx: 80px;
  cy: 60px;
}

SVG lines use endpoint coordinates such as y1 and y2.

Text elements also have a y SVG attribute for positioning text, but that attribute is not the same as the CSS geometry property described here.

SVG Coordinate System

The SVG coordinate system determines how values supplied to the y property are interpreted.

Consider an SVG element with a viewBox that establishes a coordinate region 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 within the SVG viewport.

viewBox="0 0 300 150"

The four values represent the minimum horizontal coordinate, minimum vertical coordinate, width, and height.

When the SVG scales, its coordinate system scales with it. A rectangle positioned at y: 30px remains at the same SVG coordinate even if the entire graphic is displayed at a different size.

Nested coordinate systems and transformations can change the relationship between SVG coordinates and physical screen positions.

Length Values

The y property supports absolute and relative CSS length units.

Using Pixels

The px unit is commonly used for positioning SVG elements.

.rectangle {
  y: 60px;
}

In a typical SVG coordinate system, CSS pixels correspond to SVG user units before additional scaling transformations are applied.

Using em

The em unit is relative to the font size of the element.

.rectangle {
  font-size: 20px;
  y: 2em;
}

In this example, 2em corresponds to 40 pixels.

Using rem

The rem unit is relative to the root element's font size.

.rectangle {
  y: 3rem;
}

If the root font size is 16 pixels, the vertical coordinate corresponds to 48 pixels.

Using Negative Lengths

Negative values are valid and can position an SVG element above the visible coordinate region.

.rectangle {
  y: -20px;
}

Whether the element remains visible depends on its height, the SVG viewport, and the applicable clipping or overflow behavior.

Percentage Values

Percentage values are resolved relative to the height of the current SVG viewport.

.rectangle {
  y: 25%;
}

If the viewport's coordinate height is 200 units, 25% corresponds to a vertical offset of 50 units.

Percentage values are useful when positioning SVG elements relative to the available viewport height.

Percentage Positioning Example

<svg viewBox="0 0 400 200">
  <rect class="percentage-box" width="60" height="40" />
</svg>
.percentage-box {
  x: 40px;
  y: 50%;
}

The rectangle begins at the vertical midpoint of the SVG viewport. Its top edge, rather than its center, is positioned at that coordinate.

To center the entire rectangle vertically, its height must also be considered.

CSS Property vs. SVG Attribute

SVG elements can specify vertical coordinates using either the SVG y attribute or the CSS y property.

Using the SVG Attribute

<rect x="40" y="20" width="80" height="50" />

The SVG attribute directly defines the rectangle's vertical 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" y="10" width="80" height="40" />
.box {
  y: 60px;
}

In this example, the CSS declaration positions the rectangle at vertical coordinate 60 rather than the coordinate 10 specified by the SVG attribute.

SVG attributes remain useful for defining default geometry directly in the markup.

y vs. transform

The y and transform properties can both change an SVG element's visible vertical position, but they work differently.

Property Purpose
y Specifies the element's vertical geometry coordinate.
transform Applies transformations such as translation, rotation, and scaling to an element.

For example:

.geometry-position {
  y: 40px;
}
.transformed-position {
  transform: translateY(40px);
}

The first declaration changes the element's vertical geometry coordinate. The second applies a vertical translation to the rendered element.

Use y when setting the geometry coordinate of a supported SVG element. Use transform when translation, rotation, scaling, or other transformations are needed.

Examples

Example 1: Positioning a Rectangle

The following declaration positions an SVG rectangle 40 units from the vertical origin.

.rectangle {
  x: 20px;
  y: 40px;
}

The rectangle's dimensions remain unchanged.

Example 2: Using Percentage Positioning

A percentage positions an SVG element relative to the viewport height.

.percentage-rectangle {
  y: 30%;
}

This places the rectangle's top edge at 30% of the viewport height.

Example 3: Positioning an SVG Image

The y property can position an SVG <image> element.

.svg-image {
  x: 25px;
  y: 35px;
}

The image is positioned within the SVG coordinate system rather than ordinary HTML document flow.

Example 4: Using calc()

CSS math functions can calculate a vertical coordinate.

.calculated-position {
  y: calc(50% - 20px);
}

For a rectangle 40 units high, this places its top edge 20 units above the vertical midpoint, centering it when the relevant units map directly.

Example 5: Moving a Rectangle with a Transition

The y property is animatable as a length or percentage value.

.moving-rectangle {
  y: 20px;
  transition: y 0.4s ease;
}
.moving-rectangle:hover {
  y: 100px;
}

In browsers supporting CSS geometry property transitions, the rectangle moves vertically when hovered.

Example 6: Using a CSS Custom Property

Custom properties can simplify the management of SVG coordinates.

.custom-position {
  --vertical-position: 60px;
  y: var(--vertical-position);
}

Changing the custom property updates the vertical coordinate.

Inheritance

The CSS y property is not inherited. Each supported SVG element uses its own specified value or the initial value unless inheritance is explicitly requested.

.parent {
  y: 40px;
}
.child {
  y: inherit;
}

The inherit keyword explicitly uses the parent's computed value, although the property only affects applicable SVG elements.

The initial value is 0.

In most cases, define the vertical coordinate directly on each SVG element that requires positioning.

Practical Example

This example demonstrates how the CSS y property positions three SVG rectangles at different vertical coordinates.

HTML

<svg class="y-property-demo" viewBox="0 0 360 180" role="img" aria-label="Three colored rectangles positioned at different vertical coordinates">
  <rect class="y-box-one" width="70" height="35" />
  <rect class="y-box-two" width="70" height="35" />
  <rect class="y-box-three" width="70" height="35" />
</svg>

CSS

.y-property-demo {
  display: block;
  width: 100%;
  max-width: 360px;
  height: auto;
  border: 1px solid #ccc;
  background: #f5f5f5;
}
.y-box-one {
  x: 20px;
  y: 15px;
  fill: #3498db;
}
.y-box-two {
  x: 130px;
  y: 65px;
  fill: #2ecc71;
}
.y-box-three {
  x: 240px;
  y: 120px;
  fill: #e67e22;
}
Browser Result

Result: The blue rectangle begins at vertical coordinate 15, the green rectangle begins at 65, and the orange rectangle begins at 120. Each rectangle also has a different horizontal position controlled by x.

The example demonstrates how CSS geometry properties can position SVG shapes without requiring coordinate attributes directly in the SVG markup.

Common Mistakes

Using y on Ordinary HTML Elements

The y property does not position ordinary HTML elements.

/* Does not position an HTML div */
div {
  y: 40px;
}

For HTML positioning, use properties such as top, inset-block-start, or transform as appropriate.

Omitting Units in CSS

Nonzero CSS length values require units.

/* Incorrect */
rect {
  y: 40;
}
/* Correct */
rect {
  y: 40px;
}

SVG attributes can use unitless numbers, but CSS declarations follow CSS length syntax.

Confusing y with cy

SVG rectangles use y to specify a vertical coordinate, while circles and ellipses use cy for their vertical center coordinate.

rect {
  y: 40px;
}
circle {
  cy: 40px;
}

Use the geometry property appropriate for the SVG element.

Assuming Percentages Refer to the Element Height

A percentage used with y is resolved relative to the SVG viewport height, not the height of the positioned rectangle.

For example, y: 50% places the rectangle's top edge at the vertical midpoint rather than centering the rectangle itself.

Ignoring the viewBox

The SVG viewBox and coordinate transformations affect how geometry coordinates map to the displayed graphic.

Check the active coordinate system when an element appears at an unexpected vertical position.

Confusing Geometry Positioning with Transforms

The y property changes a supported element's geometry coordinate. A transform applies an additional transformation to the element.

Although the visible results may sometimes appear similar, the properties serve different purposes.

Important Considerations

  • SVG-specific property: The y property applies to supported SVG geometry elements, not ordinary HTML elements.
  • Initial value: The initial vertical coordinate is 0.
  • Inheritance: The property is not inherited unless explicitly requested.
  • Length values: Nonzero CSS lengths require units such as px or em.
  • Percentages: Percentage coordinates are resolved relative to the SVG viewport height.
  • Coordinate systems: The SVG viewBox and 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 cy, y1, and y2.

Use y when you need to control the vertical 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 y 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 y 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 y property is defined in the SVG 2 specification as a geometry property for supported SVG elements.