CSS d Property
The d property defines the shape of an SVG <path> element using path commands and coordinates.
SVG paths can create straight lines, curves, polygons, and complex shapes. The d property allows path geometry to be specified through CSS instead of relying exclusively on the SVG d attribute.
This property applies to SVG path elements, not ordinary HTML elements. It can also be used to change a path's shape in response to CSS states or animations, subject to browser support and path compatibility.
d Syntax
The d property accepts none or a path() function containing SVG path data.
path {
d: path("path commands");
}
Basic Line
path {
d: path("M 10 10 L 90 90");
}
Triangle
path {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Rectangle
path {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Curved Path
path {
d: path("M 10 80 Q 50 10 90 80");
}
No Path Data
path {
d: none;
}
CSS-Wide Keywords
path {
d: inherit;
}
path {
d: initial;
}
path {
d: unset;
}
path {
d: revert;
}
path {
d: revert-layer;
}
Path commands inside path() are written as a quoted string. The coordinates follow SVG path syntax rather than ordinary CSS length syntax.
Property Values
| Value | Description |
|---|---|
none |
Specifies no path geometry. This is the initial value. |
path("...") |
Defines the path geometry using SVG path commands. |
inherit |
Uses the computed value from the parent element. |
initial |
Restores the property's initial value, none. |
unset |
Uses the inherited value for inherited properties or the initial value otherwise. For d, this resolves to none. |
revert |
Reverts to the value established by an earlier cascade origin. |
revert-layer |
Reverts to the value established by an earlier cascade layer. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | SVG <path> elements |
| Computed value | As specified |
| Animation type | By computed value type, with compatible path data required for interpolation |
| Percentages | Not applicable to the path-data string |
The CSS property can override an SVG element's presentation attribute through the normal CSS cascade.
How It Works
An SVG path consists of commands that move a drawing point, draw lines, create curves, and close shapes.
Step 1: Create an SVG
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path class="shape" />
</svg>
Step 2: Define the Path with CSS
.shape {
d: path("M 10 10 L 90 10 L 50 90 Z");
}
Step 3: Apply Fill and Stroke
.shape {
fill: #336699;
stroke: #224466;
stroke-width: 2;
}
Step 4: Render the Shape
The browser interprets the path commands and draws the resulting shape within the SVG coordinate system.
Important Distinction
The d property controls path geometry. Properties such as fill, stroke, and stroke-width control its visual appearance.
SVG Path Commands
SVG path data uses single-letter commands followed by coordinate values.
| Command | Name | Description |
|---|---|---|
M, m |
Move to | Moves the current point without drawing a line. |
L, l |
Line to | Draws a straight line to the specified point. |
H, h |
Horizontal line | Draws a horizontal line. |
V, v |
Vertical line | Draws a vertical line. |
C, c |
Cubic Bézier curve | Draws a curve using two control points. |
S, s |
Smooth cubic curve | Draws a cubic curve using a reflected control point when applicable. |
Q, q |
Quadratic Bézier curve | Draws a curve using one control point. |
T, t |
Smooth quadratic curve | Draws a quadratic curve using a reflected control point when applicable. |
A, a |
Elliptical arc | Draws an elliptical arc. |
Z, z |
Close path | Closes the current subpath. |
Basic Example
path {
d: path("M 10 10 L 90 10 L 90 90 L 10 90 Z");
}
This creates a closed rectangular path.
Important Consideration
SVG path commands are case-sensitive. Uppercase commands generally use absolute coordinates, while lowercase commands use relative coordinates.
Absolute and Relative Commands
SVG path commands can use absolute or relative coordinates.
Absolute Coordinates
path {
d: path("M 10 10 L 90 10 L 90 90");
}
Uppercase commands specify positions within the current SVG coordinate system.
Relative Coordinates
path {
d: path("M 10 10 l 80 0 l 0 80");
}
Lowercase commands specify offsets from the current point.
Comparison
| Command | Meaning |
|---|---|
L 90 10 |
Draw a line to the absolute position (90, 10). |
l 80 0 |
Draw a line 80 units right and 0 units vertically from the current point. |
Practical Consideration
Absolute coordinates are often easier for beginners to read. Relative coordinates can simplify paths containing repeated movements.
Drawing Straight Lines
The L, H, and V commands create straight line segments.
Diagonal Line
path {
d: path("M 10 10 L 90 90");
}
Horizontal Line
path {
d: path("M 10 50 H 90");
}
Vertical Line
path {
d: path("M 50 10 V 90");
}
Triangle
path {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Rectangle
path {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Important Consideration
Open paths can be displayed using a stroke. A fill may visually close an open subpath, but the path remains open unless a closing command is included.
Drawing Curves
SVG paths support quadratic and cubic Bézier curves, along with elliptical arcs.
Quadratic Curve
path {
d: path("M 10 80 Q 50 10 90 80");
}
The Q command uses one control point followed by an endpoint.
Cubic Curve
path {
d: path("M 10 80 C 20 10 80 10 90 80");
}
The C command uses two control points followed by an endpoint.
Smooth Cubic Curve
path {
d: path("M 10 80 C 20 10 40 10 50 50 S 80 90 90 20");
}
Elliptical Arc
path {
d: path("M 20 50 A 30 30 0 0 1 80 50");
}
Practical Consideration
Curves provide more flexibility than straight lines but require careful coordinate selection. Start with simple shapes before combining several curve commands.
Closed Paths
The Z or z command closes the current subpath by connecting its final point to its starting point.
Open Triangle
path {
d: path("M 50 10 L 90 90 L 10 90");
}
Closed Triangle
path {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Closed Rectangle
path {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Fill a Closed Shape
path {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: #336699;
}
Outline a Closed Shape
path {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: none;
stroke: #336699;
stroke-width: 3;
}
Important Consideration
Closing a path is especially useful for polygons and shapes that need a complete outline. A path may contain multiple open or closed subpaths.
SVG Attribute vs. CSS Property
SVG path geometry can be defined with the HTML markup's SVG d attribute or through the CSS d property.
SVG Attribute
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path d="M 10 10 L 90 10 L 50 90 Z" />
</svg>
CSS Property
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path class="triangle" />
</svg>
.triangle {
d: path("M 10 10 L 90 10 L 50 90 Z");
}
CSS Overrides the Presentation Attribute
<path class="shape" d="M 10 10 H 90 V 90 H 10 Z" />
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
The CSS declaration takes precedence over the presentation attribute when both apply through the cascade.
Recommended Approach
For static SVG graphics, defining the path directly in the SVG markup provides a reliable baseline. Use CSS when the path needs to change based on styling, interaction, or animation.
Styling SVG Paths
The d property defines geometry, while other SVG-related CSS properties control the path's appearance.
Fill Color
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: #336699;
}
Stroke Color
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: none;
stroke: #336699;
}
Stroke Width
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: none;
stroke: #336699;
stroke-width: 4;
}
Rounded Line Caps
.shape {
d: path("M 10 50 H 90");
fill: none;
stroke: #336699;
stroke-width: 8;
stroke-linecap: round;
}
Dashed Lines
.shape {
d: path("M 10 50 H 90");
fill: none;
stroke: #336699;
stroke-width: 3;
stroke-dasharray: 8 4;
}
Practical Consideration
For a line that should remain unfilled, use fill: none and specify a visible stroke.
Changing Paths with CSS
The d property can change SVG geometry when a CSS state becomes active.
Default Shape
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Hover Shape
.shape:hover {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Parent Hover State
.icon path {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
.icon:hover path {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Class-Based State
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
.shape.active {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
Important Consideration
Changing the d value can replace the shape immediately. Smooth interpolation between shapes requires compatible path data and browser support.
Animating SVG Paths
CSS animations can change SVG path geometry by animating the d property.
Transition Example
.shape {
d: path("M 10 50 L 90 50");
transition: d 0.5s ease;
}
.shape:hover {
d: path("M 10 80 L 90 20");
}
Keyframe Animation
@keyframes path-change {
from {
d: path("M 10 50 L 90 50");
}
to {
d: path("M 10 80 L 90 20");
}
}
.shape {
animation: path-change 2s ease-in-out infinite alternate;
}
Path Compatibility
For smooth interpolation, the starting and ending path data generally need matching command sequences with compatible parameters.
Incompatible Shapes
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
.shape:hover {
d: path("M 10 10 H 90 V 90 H 10 Z");
}
These shapes use different command sequences, so a smooth transition between them should not be assumed.
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.shape {
animation: none;
transition: none;
}
}
Important Consideration
Browser support for CSS path animation is not identical across engines. Test the animation in target browsers and provide a usable static shape as a fallback.
SVG Coordinate System
SVG path coordinates are interpreted within the SVG element's coordinate system.
SVG viewBox
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path class="shape" />
</svg>
Path Coordinates
.shape {
d: path("M 10 10 L 90 90");
}
With this viewBox, the path starts near the upper-left corner and ends near the lower-right corner.
Coordinate Direction
In the default SVG coordinate system, horizontal coordinates increase toward the right and vertical coordinates increase downward.
Responsive Scaling
svg {
width: 100%;
height: auto;
}
The viewBox allows SVG graphics to scale while preserving their internal coordinate relationships.
Important Consideration
The numbers inside path() represent SVG user coordinates. They do not automatically use CSS units such as px, rem, or percentages.
Practical Uses
The d property is useful when SVG path geometry needs to be controlled through CSS.
Custom Icons
.icon-line {
d: path("M 10 50 H 90");
}
Arrow Shapes
.arrow {
d: path("M 10 50 H 90 M 70 30 L 90 50 L 70 70");
}
Geometric Shapes
.triangle {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Hover-Based Shape Changes
.icon path {
d: path("M 10 50 L 90 50");
}
.icon:hover path {
d: path("M 10 20 L 90 80");
}
Animated Graphics
.animated-path {
d: path("M 10 50 L 90 50");
transition: d 0.4s ease;
}
Practical Consideration
Use the SVG d attribute for a dependable static shape and CSS d when dynamic styling or path changes are required.
d Example
This example demonstrates how CSS can define the geometry of three SVG paths.
The first path creates a triangle, the second creates a rectangle, and the third creates a curved line.
<div class="d-demo">
<div class="demo-item">
<svg viewBox="0 0 100 100" aria-label="Triangle">
<path class="triangle" />
</svg>
<p>Triangle</p>
</div>
<div class="demo-item">
<svg viewBox="0 0 100 100" aria-label="Rectangle">
<path class="rectangle" />
</svg>
<p>Rectangle</p>
</div>
<div class="demo-item">
<svg viewBox="0 0 100 100" aria-label="Curve">
<path class="curve" />
</svg>
<p>Curve</p>
</div>
</div>
.d-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
}
.d-demo .demo-item {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
text-align: center;
}
.d-demo svg {
width: 100px;
height: 100px;
}
.d-demo .triangle {
d: path("M 50 10 L 90 90 L 10 90 Z");
fill: #336699;
}
.d-demo .rectangle {
d: path("M 10 10 H 90 V 90 H 10 Z");
fill: #4f8a5b;
}
.d-demo .curve {
d: path("M 10 80 Q 50 10 90 80");
fill: none;
stroke: #b85c38;
stroke-width: 5;
}
.d-demo p {
margin: 10px 0 0;
font-weight: bold;
}
Triangle
Rectangle
Curve
Each SVG uses the same viewBox but receives different path geometry through CSS.
The triangle and rectangle use closed paths with fill colors. The curve uses an open path with a stroke and no fill.
The SVG d attributes in the Browser Result provide fallback geometry for browsers that do not support the CSS d property. In supporting browsers, the CSS declarations control the displayed shapes.
Accessibility
SVG paths are frequently used in icons and graphics. Their meaning should remain accessible regardless of how the geometry is defined.
Provide Accessible Names
Meaningful SVG graphics should have an accessible name, such as a <title> element or an appropriate aria-label.
Decorative Graphics
SVG graphics that are purely decorative can use aria-hidden="true" to avoid unnecessary announcements.
Do Not Rely on Shape Alone
Important instructions should not depend exclusively on the appearance of a shape.
Color Contrast
Ensure that meaningful paths have sufficient contrast against their backgrounds.
Reduced Motion
For animated paths, respect the user's reduced-motion preference when possible.
Fallback Geometry
When using CSS to define essential SVG geometry, provide an SVG d attribute fallback so the graphic remains visible in browsers without CSS d support.
Keyboard Accessibility
If an SVG acts as an interactive control, use appropriate semantic markup and keyboard-accessible interaction behavior.
Important Considerations
Keep the following points in mind when using d:
- Purpose: Defines the geometry of an SVG path.
- Initial value:
none. - Inheritance: The property is not inherited.
- Applicable elements: SVG
<path>elements. - Syntax: Uses
path()with SVG path commands. - Path commands: Supports movement, lines, curves, arcs, and closing commands.
- Absolute commands: Uppercase commands use absolute coordinates.
- Relative commands: Lowercase commands use relative coordinates.
- Coordinate system: Path coordinates are interpreted in SVG user space.
- Styling: Use
fill,stroke, and related properties to control appearance. - CSS cascade: CSS declarations can override the SVG
dpresentation attribute. - Animations: Smooth path interpolation generally requires compatible path command sequences.
- Browser support: CSS
dsupport is less universal than SVGdattribute support. - Fallbacks: Include an SVG
dattribute for important graphics. - Accessibility: Provide meaningful names for informative SVG graphics.
The d property is most useful when SVG geometry needs to respond to CSS styling or animation. For static graphics, the SVG d attribute remains a dependable choice.
Browser Support
The CSS d property has limited availability across browsers compared with the SVG d attribute.
Some modern browser engines support defining SVG path geometry with CSS, but support is not consistent across all browsers and versions.
CSS Example
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
Recommended Fallback
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path class="shape" d="M 50 10 L 90 90 L 10 90 Z" />
</svg>
Feature Detection
@supports (d: path("M 0 0 L 10 10")) {
.shape {
d: path("M 50 10 L 90 90 L 10 90 Z");
}
}
The @supports rule can check whether the browser recognizes the CSS declaration. It does not guarantee that every animation or path interpolation will work identically.
For current compatibility information, visit Can I Use? and search for the CSS d property.
Specifications
The CSS d property is defined in SVG 2.
The specification defines how path data can be provided as a CSS property and how it interacts with SVG path geometry.
