SVG Line

What You’ll Learn
How to draw straight lines with SVG using the <line> element. Lines are a fundamental building block for diagrams, charts, icons, grids, separators, and custom drawings.
You’ll learn the syntax, the four core coordinate attributes (x1, y1, x2, y2), and the most useful stroke properties like stroke, stroke-width, stroke-linecap, and stroke-dasharray.
⚡ Quick Reference — SVG Line Attributes
numberStart point coordinates
numberEnd point coordinates
colorLine colour (required to see the line)
numberThickness in pixels
butt | round | squareShape of the line ends
patternDashed or dotted lines
<svg>
<line x1="x1" y1="y1" x2="x2" y2="y2" stroke="black" stroke-width="2" />
</svg>Complete HTML Example
This example draws a diagonal line from (50, 50) to (150, 150) inside a 200×200 SVG canvas:
<!DOCTYPE html>
<html>
<body>
<svg width="200" height="200">
<line x1="50" y1="50" x2="150" y2="150" stroke="black" stroke-width="2" />
</svg>
</body>
</html>👀 Live Preview — Stroke Styling
The <line> element is all about strokes. Here are the most useful style variations:
🧠 How It Works
Set the start point (x1, y1)
The line begins at (x1, y1). SVG coordinates start at the top-left of the SVG canvas.
Set the end point (x2, y2)
The line ends at (x2, y2). Together, these four values define the geometry of the line.
Add a stroke to render it
Unlike filled shapes, <line> has no area to fill. You must set stroke (and usually stroke-width) to make it visible.
Style the ends and pattern
Use stroke-linecap for end shape and stroke-dasharray for dashed/dotted patterns.
Crisp vector lines
SVG lines remain sharp at any resolution and are perfect for UI dividers, charts, and diagrams.
Use Cases
SVG lines are used in many common UI and visualisation patterns:
Axes, grid lines, trend lines, and tick marks.
Connectors, callouts, and flow lines.
Separators and decorative strokes.
Simple strokes, plus signs, arrows, and glyphs.
Dashes for loading and timeline steps.
Underlines, highlights, and pointers in tutorials.
💡 Best Practices
Do
- Always set a
strokeon<line>elements - Use
stroke-linecap="round"for friendly UI strokes - Keep stroke widths consistent across an icon or diagram
- Use a
viewBoxif the SVG needs to be responsive - Prefer whole-pixel stroke widths for sharper rendering
Don’t
- Forget that
<line>has no fill area - Use a stroke width of
0(it won’t render) - Place coordinates outside the SVG viewBox/canvas
- Overuse heavy dash patterns in dense charts (it can get noisy)
- Hard-code pixel sizes when the graphic must scale
Key Takeaways
A line is defined by two points
(x1, y1) (x2, y2)stroke is required to see the line
Use stroke-linecap to control end shapes
Use stroke-dasharray for dashed and dotted lines
Use viewBox for responsive SVGs
❓ Frequently Asked Questions
<line> and set x1, y1, x2, y2. Then set stroke and stroke-width to make it visible.stroke (and a non-zero stroke-width). Also verify the coordinates are inside the SVG bounds/viewBox.stroke-dasharray. Example: stroke-dasharray="8 6" draws 8 units of dash followed by 6 units of gap.butt, round, or square.viewBox on the <svg> and size the SVG with CSS. The line scales with the coordinate system.Add Colour with Gradients
Next, learn how to create linear gradients and apply them to fills and strokes in SVG.
5 people found this page helpful
