CSS Dashed Lines & Animations: The Ultimate Cheatsheet & Guide
Whether you need a simple dotted separator, a "marching ants" selection tool, or a complex self-drawing logo, dashed lines are a staple of UI design.
This guide covers everything from the basics to advanced SVG animations.
Table of Contents
- CSS Dashed Lines & Animations: The Ultimate Cheatsheet & Guide
- ⚡ Quick Copy Cheatsheet
- 1. The Classic Dashed Border
- 2. The "Marching Ants" (Animated Selection)
- Level 1: Understanding Dashes (CSS vs SVG)
- Level 2: The "Marching Ants" Effect (SVG Method)
- Level 3: The "Self-Drawing" Line Animation
- Step 1: The CSS Setup
- Step 2: The JavaScript Calculation
- FAQ: Common Questions
- Conclusion
- Related Quiz - CSS Flexbox & Grid Mastery
⚡ Quick Copy Cheatsheet
Targeting the most common needs (dashed line css, animated border css) right here:
1. The Classic Dashed Border
.dashed-box {
/* The standard CSS way */
border: 2px dashed #000;
}
2. The "Marching Ants" (Animated Selection)
.marching-ants {
/* Creates a moving dashed border effect */
background:
linear-gradient(90deg, #000 50%, transparent 50%) repeat-x,
linear-gradient(90deg, #000 50%, transparent 50%) repeat-x,
linear-gradient(0deg, #000 50%, transparent 50%) repeat-y,
linear-gradient(0deg, #000 50%, transparent 50%) repeat-y;
background-size: 15px 2px, 15px 2px, 2px 15px, 2px 15px;
background-position: 0 0, 0 100%, 0 0, 100% 0;
animation: march 1s linear infinite;
}
@keyframes march {
100% {
background-position: 15px 0, -15px 100%, 0 -15px, 100% 15px;
}
}
Advertisement
Level 1: Understanding Dashes (CSS vs SVG)
While you can create a dashed border in CSS with border-style: dashed;, it offers very little control. You cannot control the length of the dashes or the gaps.
For predictable and animatable dashed lines, SVG (Scalable Vector Graphics) is the industry standard.
The Two Magic Properties:
stroke-dasharray: Controls the pattern.
10= 10px dash, 10px gap.20 5= 20px dash, 5px gap.
stroke-dashoffset: Controls the starting point. Animating this makes the line move.
Advertisement
Level 2: The "Marching Ants" Effect (SVG Method)
*Target Keyword: marching ants effect*
The "marching ants" effect is a classic UI pattern. Unlike the CSS background hack (shown in the cheatsheet), the SVG method handles rounded corners (rx) perfectly.
The HTML
<svg width="200" height="150">
<rect
class="ants-rect"
x="10" y="10"
width="180" height="130"
rx="10"
/>
</svg>
The CSS
.ants-rect {
fill: none;
stroke: #4A90E2;
stroke-width: 3;
/* 8px dash, 8px gap */
stroke-dasharray: 8 8;
/* Animate the offset */
animation: march 1s linear infinite;
}
@keyframes march {
to {
stroke-dashoffset: 16; /* Must match total pattern length (8+8) */
}
}
Why 16? Your pattern is 8px dash + 8px gap = 16px total. To make a seamless loop, you must animate the offset by exactly one full pattern length.
Advertisement
Level 3: The "Self-Drawing" Line Animation
*Target Keyword: css draw line animation*
This is the showstopper effect used on award-winning websites. It creates the illusion that a logo or icon is being drawn in real-time.
The Concept:
- Set the dash length (
dasharray) to the total length of the path. - Set the offset (
dashoffset) to the total length of the path. (This pushes the dash completely out of view, hiding the shape). - Animate the offset to 0. (This pulls the dash back in, "drawing" the line).
Step 1: The CSS Setup
.draw-me {
stroke-dasharray: var(--path-length);
stroke-dashoffset: var(--path-length);
transition: stroke-dashoffset 2s ease-in-out;
}
.draw-me.active {
stroke-dashoffset: 0;
}
Step 2: The JavaScript Calculation
Since we rarely know the exact length of a complex SVG path, we use a tiny bit of JS to calculate it automatically.
document.addEventListener('DOMContentLoaded', () => {
const path = document.querySelector('.draw-me');
// Calculate total length
const length = path.getTotalLength();
// Set it as a CSS variable
path.style.setProperty('--path-length', length);
// Trigger animation (e.g., after a delay or on scroll)
setTimeout(() => {
path.classList.add('active');
}, 500);
});
FAQ: Common Questions
Q: Can I create a dashed line in HTML only? (Target Keyword: html for dashed line) No. HTML is for structure, not style. The <hr> tag creates a solid line by default, but you must use CSS (border-style: dashed) to make it dashed.
Q: How do I animate a dashed border without SVG? (Target Keyword: animated border css) You can use the background-image gradient trick shown in the Cheatsheet section above. However, this is difficult to use on elements with rounded corners (border-radius). For rounded animated borders, SVG is always the better choice.
Q: Why is my stroke-dasharray not working? Ensure your element has a stroke color defined (e.g., stroke: black). If the stroke is transparent or none, the dashes won't appear.
Conclusion
From simple CSS borders to complex SVG path animations, dashed lines are a powerful tool in your design kit.
- Need a simple box? Use
border-style: dashed. - Need rounded corners and animation? Use SVG with
stroke-dasharray. - Need a logo reveal? Use the
getTotalLength()trick.
Related Quiz - CSS Flexbox & Grid Mastery
CSS Flexbox & Grid Mastery
Test your knowledge and reinforce what you've learned in this article.
Md Nasim Sheikh
Software Developer at softexForge