Chapter 7: SVG line

What does line actually do?

It draws a single straight line segment between two points.

No curves, no filling inside — just a stroke (the visible part) connecting point A to point B.

The 4 must-know attributes (all required in practice)

Attribute Meaning Typical values Default Notes
x1 Starting point — x coordinate number, % 0 Horizontal start position
y1 Starting point — y coordinate number, % 0 Vertical start position
x2 Ending point — x coordinate number, % 0 Horizontal end position
y2 Ending point — y coordinate number, % 0 Vertical end position

Remember SVG coordinates forever:

  • (0,0) = top-left corner
  • x increases → right
  • y increases → down

Here are some clear visual explanations of how the coordinate system works with a line:

SVG Tutorial / Jaemin Jo | Observable
observablehq.com
HTML SVG course lesson | Uxcel
app.uxcel.com

Example 1 – Simplest horizontal line (like a divider)

HTML

Result: Thick dark horizontal line across the middle.

Example 2 – Diagonal line (very common)

HTML

This goes from near top-left → bottom-right.

Visual example of a diagonal line with coordinates labeled:

All you need to know about SVG Shapes | by Anish Antony | Javarevisited |  Medium
medium.com
All you need to know about SVG Shapes | by Anish Antony | Javarevisited | Medium

Example 3 – Vertical line + different thicknesses & colors

HTML

Example 4 – Dashed & dotted lines (super useful for charts, wireframes)

HTML

Here are some dashed/dotted line style examples (not exactly SVG but shows the idea clearly):

Dotted Lines & Dashed Lines SVG Bundle PNG Bundle Dotted Line Svg Dashed  Line Svg Svg Bundle 9 Lines Dotted, Dashed, Thick Dashes - Etsy
etsy.com
Dashed Line Zigzag Stock Illustrations – 1,155 Dashed Line Zigzag Stock  Illustrations, Vectors & Clipart - Dreamstime
dreamstime.com

Example 5 – Arrows using <line> + <marker> (very common pattern)

HTML

Quick comparison: When to use <line> vs others

Goal Best choice Why?
Single straight line <line> Simplest & clearest
Connected lines (polyline) <polyline> One element, many points
Complex path with curves <path> Most powerful (d attribute)
Arrow / directed line <line> + marker Easy arrowheads
Grid / axis lines Multiple <line> Or <path> for dashed grid

Common beginner mistakes with <line>

  • Forgetting all four coordinates → line invisible
  • Setting x1 = x2 and y1 = y2 → zero-length line (invisible)
  • No stroke color or stroke-width → nothing visible (default stroke = none)
  • Thinking it can be filled → no, <line> has no fill (use stroke only)

Mini practice tasks for you

  1. Draw a big red X across the whole SVG using two<line> elements
  2. Create a simple coordinate cross (horizontal + vertical line meeting in center) with arrows on both ends
  3. Make a dashed blue border around the SVG using four <line> elements

Paste your code here if you want feedback — I’ll check it like we’re debugging together 😄

Any part still not 100% clear? Just tell me which example or concept — we’ll redraw it until it clicks perfectly! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *