HTML SVG <line>

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

The SVG <line> element draws a straight segment between two points: start (x1, y1) and end (x2, y2). Place it inside an <svg>.

A line has no fill area — it is pure stroke. Style with stroke, stroke-width, and related properties from SVG stroke. For chains of segments, use <polyline>.

Baseline · Widely available.

<line> is a stable SVG basic shape across modern browsers since about July 2015.

No stroke = invisible.

If you omit stroke (or set stroke-width to 0), the line does not show — even when coordinates are correct.

Syntax

line.html
<svg width="200" height="120" viewBox="0 0 200 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Example diagonal line">
  <line x1="20" y1="100" x2="180" y2="20"
        stroke="#0f172a" stroke-width="4" />
</svg>

Self-closing <line /> is fine in HTML. Always set a visible stroke.

Attributes

Core <line> attributes:

AttributeDescriptionTry it
x1 / y1Start point (default 0)Try it
x2 / y2End point (default 0)Try it
strokeLine color (required to see it)Try it
stroke-widthThickness of the strokeTry it
stroke-linecapbutt, round, or squareTry it
stroke-dasharrayDash and gap patternTry it
pathLengthOptional length for dash mathTry it

Global Attributes

The <line> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Drag the end point and thickness. Start stays at (20, 100).

180
20
4

Remember: without stroke, the line stays invisible.

SVG

x1="20" y1="100" x2="180" y2="20"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Stroke Line

Connect two points with a visible stroke.

HTML
<svg width="200" height="120" viewBox="0 0 200 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Blue diagonal line">
  <line x1="20" y1="100" x2="180" y2="20"
        stroke="#2563eb" stroke-width="4" />
</svg>
Try It Yourself

How It Works

  1. Create an <svg> with a viewBox.
  2. Set x1, y1 for the start.
  3. Set x2, y2 for the end.
  4. Add stroke and stroke-width so the line is visible.

Example 2 — Stroke Width and Caps

Compare butt, round, and square ends.

HTML
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Three lines with different linecaps">
  <line x1="30" y1="30" x2="210" y2="30"
        stroke="#0f172a" stroke-width="14" stroke-linecap="butt" />
  <line x1="30" y1="70" x2="210" y2="70"
        stroke="#2563eb" stroke-width="14" stroke-linecap="round" />
  <line x1="30" y1="110" x2="210" y2="110"
        stroke="#16a34a" stroke-width="14" stroke-linecap="square" />
</svg>
Try It Yourself

How It Works

  1. Keep endpoints the same across the three lines.
  2. butt stops exactly at the end points.
  3. round adds a semicircle cap.
  4. square extends the stroke past the ends.

Example 3 — Dashed Line

Use stroke-dasharray for dashes and gaps.

HTML
<svg width="220" height="80" viewBox="0 0 220 80"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Dashed purple line">
  <line x1="20" y1="40" x2="200" y2="40"
        stroke="#7c3aed" stroke-width="6"
        stroke-linecap="round" stroke-dasharray="12 10" />
</svg>
Try It Yourself

How It Works

  1. Draw a solid line with stroke first.
  2. stroke-dasharray="12 10" means 12 on, 10 off.
  3. Round caps soften each dash end.
  4. See also the SVG stroke page for more patterns.

Example 4 — Multiple Lines

Several lines share one SVG coordinate system.

HTML
<svg width="200" height="140" viewBox="0 0 200 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Three colored lines from one origin">
  <line x1="20" y1="120" x2="180" y2="20" stroke="#2563eb" stroke-width="3" />
  <line x1="20" y1="120" x2="180" y2="70" stroke="#16a34a" stroke-width="3" />
  <line x1="20" y1="120" x2="180" y2="120" stroke="#ea580c" stroke-width="3" />
</svg>
Try It Yourself

How It Works

  1. Share the same start (x1, y1).
  2. Change only the end (x2, y2) per line.
  3. Vary stroke to tell them apart.
  4. Later lines paint on top if they overlap.

Example 5 — Simple Chart Axes

Two lines form X and Y axes for a tiny chart frame.

HTML
<svg width="220" height="160" viewBox="0 0 220 160"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Chart axes with a data line">
  <line x1="40" y1="20" x2="40" y2="130" stroke="#64748b" stroke-width="2" />
  <line x1="40" y1="130" x2="200" y2="130" stroke="#64748b" stroke-width="2" />
  <line x1="40" y1="110" x2="200" y2="40"
        stroke="#2563eb" stroke-width="3" stroke-linecap="round" />
</svg>
Try It Yourself

How It Works

  1. Vertical line for the Y axis (x1 === x2).
  2. Horizontal line for the X axis (y1 === y2).
  3. Third line acts as a simple data segment.
  4. For multi-point data, switch to polyline or path.

Edge Cases & Pitfalls

Most missing-line bugs are stroke, zero width, or viewport clipping.

stroke

Stroke is required

Default stroke is none for teaching purposes in practice — always set stroke and a positive stroke-width.

viewBox

Stay inside the viewBox

Endpoints outside the SVG box are clipped. Size the graphic to fit both points plus half the stroke width.

fill

Fill does nothing useful

<line> has no interior. Changing fill will not paint the segment — use stroke properties.

choice

One segment only

Need corners or many points? Prefer <polyline> or <path> instead of stacking many lines.

Browser Support

The SVG <line> element is supported in every modern browser.

✓ Baseline · Widely available

SVG <line>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support line geometry and stroke presentation attributes. Internet Explorer 9+ supports basic SVG lines.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
<line> Widely supported

Bottom line: Safe for production charts, icons, and UI guides. Always set stroke explicitly in examples and production markup.

Key Takeaways

  • Geometry: x1, y1, x2, y2.
  • Paint: stroke + stroke-width (not fill).
  • Style: caps and stroke-dasharray.
  • Many points? Use polyline or path.

One line: two points + stroke inside an <svg>.

Default CSS Settings

Conceptual SVG presentation defaults for lines:

defaults.css
/* Conceptual SVG defaults */
line {
  fill: none;          /* lines are stroked, not filled */
  stroke: none;        /* set stroke yourself or nothing shows */
  stroke-width: 1;
  stroke-linecap: butt;
}

Always set stroke explicitly in tutorials and production markup.

Frequently Asked Questions

Put a <line> inside an <svg>. Set x1,y1 for the start and x2,y2 for the end, then set stroke and stroke-width.
Lines have no fill. Without stroke (or with stroke-width 0), nothing shows. Also check the viewBox and SVG size.
Use stroke-dasharray, for example stroke-dasharray="8 6" for dash 8 and gap 6.
It shapes the ends: butt (flat), round, or square (extends past the endpoints).
Use line for one straight segment. Use polyline for connected segments, and path for curves or mixed commands.
Yes. Set stroke, stroke-width, stroke-linecap, and stroke-dasharray as attributes or CSS.

Next: SVG Polygon

Learn closed shapes built from a list of points.

SVG Polygon →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful