SVG gradient for perfectly horizontal path

The default for gradientUnits is objectBoundingBox. The key issue you have is described in the last paragraph of the specification text

Keyword objectBoundingBox should not be used when the geometry of the applicable element has no width or no height, such as the case of a horizontal or vertical line, even when the line has actual thickness when viewed due to having a non-zero stroke width since stroke width is ignored for bounding box calculations. When the geometry of the applicable element has no width or height and objectBoundingBox is specified, then the given effect (e.g., a gradient or a filter) will be ignored.

Why not use a rect with a fill rather than a path with a stroke if you’ve got a horizontal line? Or alternatively use userSpaceOnUse units.

Leave a Comment