110+ shadcn/ui components with drag-drop code editor 🎉 👇
🚀 Try Editor!

Tailwind CSS v4: Stroke Linecap Explained

Learn How to Use Stroke Linecap in Tailwind CSS v4 to Style SVG Elements

by Yucel Faruk Sahan
3 min read
Updated on

When it comes to styling SVGs on the web, precision matters. Whether you're designing custom icons, illustrations, or data visualizations, the way strokes end can make or break your design. Tailwind CSS v4 offers a simple yet powerful way to control this with its stroke-linecap utilities. In this blog post, you’ll discover what stroke-linecap is, the options available, and how to apply them in your projects using Tailwind CSS v4.

What is Stroke Linecap?

In SVG, the stroke-linecap property defines the shape at the ends of open subpaths—like lines or paths—when they’re stroked. It’s a small detail that has a big impact on the look of your SVG elements. There are three options to choose from:

  • butt: Ends the stroke abruptly at the path’s endpoint, giving a clean, squared-off edge.

  • round: Adds a semicircular cap to the ends, creating a smooth, rounded finish.

  • square: Extends the stroke beyond the endpoint by half the stroke width, forming a square cap.

Each option serves a different purpose, depending on the style you’re aiming for.

Using Stroke Linecap in Tailwind CSS v4

Tailwind CSS v4 makes styling SVGs effortless with utility classes for stroke-linecap. You can apply these directly to your SVG elements without writing custom CSS. Here are the classes:

  • stroke-linecap-butt: Sets stroke-linecap: butt;

  • stroke-linecap-round: Sets stroke-linecap: round;

  • stroke-linecap-square: Sets stroke-linecap: square;

These classes work on any SVG element that has a stroke, such as <line>, <path>, or <polyline>.

Practical Examples

Let’s see how these utilities work in action with some real-world examples.

Example 1: Styling a Basic Line

Imagine you’re creating a simple line for a design element:

<svg class="w-32 h-8" viewBox="0 0 100 10">
  <line x1="10" y1="5" x2="90" y2="5" class="stroke-current stroke-2 stroke-linecap-round" />
</svg>

Here, stroke-linecap-round gives the line soft, rounded ends. Swap it with stroke-linecap-butt for a sharp finish or stroke-linecap-square for a slightly extended cap.

Example 2: Enhancing an SVG Icon

For an icon like an arrow, the cap style can define its character:

<svg class="w-12 h-12" viewBox="0 0 24 24">
  <path d="M5 12h14" class="stroke-current stroke-2 stroke-linecap-square" />
  <path d="M12 5l7 7-7 7" class="stroke-current stroke-2 stroke-linecap-square" />
</svg>

Using stroke-linecap-square makes the arrow’s ends more prominent. Try stroke-linecap-round for a friendlier vibe.

Example 3: Line Chart Styling

In a data visualization, cap styles can highlight key points:

<svg class="w-full h-64" viewBox="0 0 100 50">
  <polyline points="10,40 30,20 50,30 70,10 90,30" class="stroke-current stroke-2 stroke-linecap-butt" />
</svg>

The stroke-linecap-butt class keeps the polyline’s ends crisp, perfect for a clean chart look.

Tips for Success

  • Match Your Design: Use round for softer designs, butt for minimalism, or square for bold emphasis.

  • Pair with Other Utilities: Combine stroke-linecap with stroke-width classes (e.g., stroke-4) for more control.

  • Test Across Scales: Ensure your cap style looks good at different SVG sizes.

Wrap-Up

The stroke-linecap property in Tailwind CSS v4 is a small but mighty tool for styling SVG elements. With just a single utility class, you can transform the ends of your strokes to suit any project—whether it’s a sleek icon, a detailed illustration, or a sharp data viz. Experiment with butt, round, and square to find the perfect fit for your designs, and let Tailwind’s simplicity speed up your workflow. Happy styling!

FAQ

Can I add custom stroke-linecap values in Tailwind CSS v4?

The built-in classes cover butt, round, and square. For anything else, you’d need to extend Tailwind’s config or use custom CSS.

Why would I choose square over butt?

Square extends the stroke slightly beyond the endpoint, making it more noticeable than the flush butt style.

Does stroke-linecap affect filled SVG shapes?

No, it only applies to elements with a visible stroke, not filled areas without strokes.

What happens if I don’t specify a stroke-linecap class?

The default is butt, so your strokes will have squared-off ends unless you change it.

Yucel Faruk Sahan

Yucel is a digital product maker and content writer specializing in full-stack development. He is passionate about crafting engaging content and creating innovative solutions that bridge technology and user needs. In his free time, he enjoys discovering new technologies and drawing inspiration from the great outdoors.