<line>
Core element
Place <line> inside an <svg> to draw one straight segment.

SVG’s <line> draws a straight segment between two points. This tutorial covers x1, y1, x2, and y2, stroke styling (stroke, stroke-width, stroke-linecap, stroke-dasharray), responsive viewBox usage, five worked examples, and how <line> compares to <polyline> and <path>.
Core element
Place <line> inside an <svg> to draw one straight segment.
Start point
Set where the line begins on the SVG canvas.
End point
Set where the line ends — the segment connects start to end.
Required paint
Lines have no fill — set stroke and stroke-width to see them.
linecap / dasharray
Round ends, square caps, dashed and dotted patterns.
Responsive
Scale lines cleanly with a viewBox and CSS sizing.
SVG’s <line> is the simplest way to draw a straight edge: two points and a stroke. It powers chart axes, icon strokes, dividers, connectors, and guides in diagrams.
Unlike rectangles or circles, a line has no interior fill. Visibility comes entirely from presentation attributes like stroke and stroke-width — forget those and nothing appears.
Raster lines blur when scaled. SVG lines stay sharp, stay tiny in markup, and can be dashed, capped, and themed with CSS just like other vector shapes.
x1, y1, x2, and y2 fully define the segment.
No fill — colour and thickness come from stroke attributes.
Control ends with stroke-linecap and patterns with stroke-dasharray.
One drawing scales to any CSS size without rewriting points.
In short: set start and end points, then paint the stroke — that’s the whole foundation of SVG lines.
Basic form of the SVG line element:
<svg width="200" height="200">
<line x1="50" y1="50" x2="150" y2="150"
stroke="black" stroke-width="2" />
</svg> | Attribute | Type | Description |
|---|---|---|
x1 | Length / number | x-coordinate of the start point. Defaults to 0. |
y1 | Length / number | y-coordinate of the start point. Defaults to 0. |
x2 | Length / number | x-coordinate of the end point. Defaults to 0. |
y2 | Length / number | y-coordinate of the end point. Defaults to 0. |
stroke | Paint | Line colour — required for the line to be visible. |
stroke-width | Length / number | Thickness of the stroke in user units. |
stroke-linecap | Keyword | butt, round, or square end caps. |
stroke-dasharray | List | Dash and gap lengths for dashed or dotted lines. |
| Result | Details |
|---|---|
| vector line segment | Renders a straight stroke from (x1, y1) to (x2, y2). |
<svg width="200" height="200" viewBox="0 0 200 200">
<line x1="50" y1="50" x2="150" y2="150"
stroke="#0f172a" stroke-width="2" />
</svg> | Idea | Detail | Notes |
|---|---|---|
| Origin | (0, 0) is top-left | y grows downward |
| Visibility | Needs stroke | stroke-width of 0 hides it |
| Same points | x1===x2 and y1===y2 | Zero-length line — usually invisible |
| Goal | Code |
|---|---|
| Basic line | <line x1="50" y1="50" x2="150" y2="150" stroke="#0f172a" stroke-width="2" /> |
| Thick stroke | stroke-width="6" |
| Rounded ends | stroke-linecap="round" |
| Dashed line | stroke-dasharray="8 6" |
| Dotted line | stroke-linecap="round" stroke-dasharray="0 10" |
| Responsive SVG | <svg viewBox="0 0 200 200">... + CSS width |
<line> vs <polyline> vs <path> vs <rect>All can create edges — pick the simplest element that fits.
one segmentStraight stroke between two points
many segmentsConnected open chain of points
any shapeCurves, arcs, and mixed commands
box edgesFilled or stroked rectangles — not single segments
<line>Reach for <line> whenever you need one straight vector segment.
Baseline, tick marks, and guide lines in data graphics.
Plus signs, close icons, arrows, and simple strokes.
Horizontal or diagonal separators in UI illustrations.
Links between nodes in diagrams and flow charts.
If you need many connected points, use <polyline>.
Key benefit: four coordinates and a stroke — the lightest straight edge in SVG.
Five starter snippets using <line>. Click View Output for a preview, or Try It Yourself to edit live.
Draw a diagonal line, then thicken the stroke.
Draw a line from (50, 50) to (150, 150) inside a 200×200 canvas.
<svg width="200" height="200">
<line x1="50" y1="50" x2="150" y2="150"
stroke="#0f172a" stroke-width="2" />
</svg> The stroke paints a segment between the two points. Without stroke, this same markup would draw nothing.
Increase stroke-width for bolder UI rules and icon strokes.
<svg width="220" height="80">
<line x1="20" y1="40" x2="200" y2="40"
stroke="#0f172a" stroke-width="8" />
</svg> Half of stroke-width sits on each side of the geometric line. Leave a little margin so thick strokes do not clip at the SVG edge.
Caps, dashes, and responsive sizing.
Compare butt, round, and square end caps on the same geometry.
<svg width="240" height="140">
<line x1="40" y1="30" x2="200" y2="30"
stroke="#64748b" stroke-width="12" stroke-linecap="butt" />
<line x1="40" y1="70" x2="200" y2="70"
stroke="#10b981" stroke-width="12" stroke-linecap="round" />
<line x1="40" y1="110" x2="200" y2="110"
stroke="#3b82f6" stroke-width="12" stroke-linecap="square" />
</svg> butt stops at the endpoint, round adds a semicircle, and square extends half a stroke-width past each end.
Use stroke-dasharray for dashed rules, and combine it with round caps for dots.
<svg width="240" height="100">
<line x1="20" y1="30" x2="220" y2="30"
stroke="#b45309" stroke-width="4" stroke-dasharray="8 6" />
<line x1="20" y1="70" x2="220" y2="70"
stroke="#6d28d9" stroke-width="5"
stroke-linecap="round" stroke-dasharray="0 10" />
</svg> 8 6 means 8 units on, 6 off. A zero-length dash with a gap and round caps creates a dotted look.
Define coordinates in a viewBox, then let CSS control the displayed size.
<style>
.rule { width: 160px; height: auto; display: block; }
</style>
<svg class="rule" viewBox="0 0 100 20"
role="img" aria-label="Blue divider line">
<line x1="4" y1="10" x2="96" y2="10"
stroke="#2563eb" stroke-width="3" stroke-linecap="round" />
</svg> The viewBox maps user units to whatever CSS size you give the SVG. Change .rule { width } and the line scales without rewriting coordinates.
Real-world places where SVG lines show up every day.
Baselines, grid lines, and tick marks in data viz.
Example: x-axis under a bar chart.
Plus, minus, close, and arrow strokes.
Example: X close icon from two diagonals.
Section rules and decorative separators.
Example: dashed rule under a heading.
Edges between nodes in flow charts.
Example: link from box A to box B.
Alignment helpers in design tools and demos.
Example: horizontal guide across a canvas.
Geometry lessons teaching slope and distance.
Example: interactive point-to-point demos.
Pro Tip: for a chain of connected segments, switch to <polyline> instead of stacking many <line> elements.
Why draw lines with SVG instead of borders or images.
Exact endpoints in any direction — not just horizontal CSS borders.
Stays sharp on retina screens without extra assets.
Caps, dashes, and colours via attributes or CSS.
One element replaces PNG strokes for simple graphics.
Animate dashoffset, opacity, or endpoints for motion.
Pro Tip: for UI icons, stroke="currentColor" lets lines inherit the parent text colour for theming.
Follow these practices for clean, scalable SVG lines.
Lines are invisible without stroke and a positive stroke-width.
stroke-linecap="round" softens icon strokes and dividers.
Thick strokes can clip — pad the canvas or pull endpoints inward.
Match stroke widths across an icon set for a cohesive look.
Define geometry once, then size the SVG with CSS.
Pro Tip: whole-pixel stroke widths (1, 2, 3…) usually look sharper at common display sizes.
Avoid these mistakes when your line refuses to show up.
strokeA line with no stroke paint is invisible.
→ Always set stroke and a non-zero stroke-width.
stroke-width="0" draws nothing.
→ Use a positive width such as 1, 2, or 3.
Endpoints outside the canvas get clipped or disappear.
→ Keep both points inside the SVG size or viewBox.
Half the width extends past endpoints and can hit the edge.
→ Inset endpoints or expand the canvas padding.
Many separate <line> tags get hard to maintain.
→ Prefer <polyline> for connected multi-point strokes.
Pro Tip: if a line vanishes, check stroke, stroke-width, and whether both endpoints sit inside the viewBox — those three catch most issues.
<line> Is DrawnThe line begins at (x1, y1). SVG coordinates start at the top-left of the canvas.
The segment ends at (x2, y2). The browser connects the two points with a straight edge.
Unlike filled shapes, <line> has no area to fill. Set stroke and usually stroke-width.
Use stroke-linecap for end shape and stroke-dasharray for dashed or dotted patterns.
The browser draws the vector live from endpoints and stroke paint — sharp at every zoom level.
<line> needs stroke — there is no fill to fall back on.stroke-linecap values: butt, round, square.stroke-dasharray creates dashed and dotted patterns.<polyline>.viewBox when the graphic must scale responsively.Quick Takeaway: two points + stroke paint. Keep endpoints inside the viewBox, and use CSS sizing for responsive icons.
The SVG <line> element is supported in all modern browsers — and has been for many years — as part of SVG 1.1 basic shapes.
Use
Bottom line: Safe for production sites. Prefer inline SVG or cached .svg assets; no polyfill is required for current browsers.
SVG <line> is the simplest straight edge on the web: set x1, y1, x2, and y2, then paint with stroke and stroke-width.
Practice the five examples above, then continue to SVG Polygon when you need closed multi-sided shapes.
Always set a stroke, leave room for thickness, and use viewBox when the line must scale with the layout.
stroke on <line> elementsstroke-linecap="round" for friendly UI strokesviewBox when the graphic must scalestroke and wonder why nothing appears0 (it won’t render)<line> tags when a <polyline> would do<line>Draw straight strokes the SVG way.
One straight segment
APIx1,y1 → x2,y2
GeometryRequired to see it
Paintlinecap & dashes
StyleScales cleanly
ResponsiveAn SVG <line> has no fill area — it is pure stroke. Without stroke (and a non-zero stroke-width), the line is invisible. You can animate stroke-dashoffset to create draw-on line effects — a popular technique for loaders and signature animations.
Learn how a list of points creates closed shapes like triangles and hexagons.
8 people found this page helpful