SVG Line Element: Purpose, Syntax, and Usage

The <line> element in Scalable Vector Graphics (SVG) is a fundamental graphical primitive used to draw straight line segments between two defined points within an SVG coordinate system. This article explains the primary purpose of the <line> element, its essential attributes, how it is styled, and its common applications in web design and data visualization.

The Purpose of the SVG Line Element

The primary function of the <line> element is to create a direct vector connection between a single starting point and an ending point. Unlike more complex SVG elements such as <path> or <polygon>, which can define curves and closed multi-segment shapes, the <line> element is specifically optimized for simple, non-curved, two-point strokes. Because it has a minimal syntax overhead, it renders quickly and is easy to manipulate programmatically via CSS or JavaScript.

Essential Attributes

To position a line accurately in the SVG viewport, the <line> element relies on four primary geometric attributes:

Rendering and Styling

By default, an SVG line has no fill because it is a one-dimensional path with no interior area. To make a line visible on the screen, you must define its stroke properties either via presentation attributes or CSS.

Basic Example

<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
  <line x1="20" y1="20" x2="180" y2="80" stroke="blue" stroke-width="4" stroke-linecap="round" />
</svg>

Common Use Cases