Draw a Growing Border Line on Hover with CSS
To make a border line grow on hover, create it with ::before or ::after and transition scaleX() from 0 to 1. transform-origin determines the starting edge, so the top line can grow from the left and the bottom line from the right.
Run the complete example
Save the HTML as index.html, save the CSS as style.css in the same folder, and open the HTML in a browser. Point to the card and then leave it. The lines grow and then shrink. Press Tab to reveal them with keyboard focus, or Enter to follow the link to the note below.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS border drawing</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Point to the link or reach it with Tab</h1>
<a class="draw-box" href="#notes">Jump to the border explanation</a>
<p id="notes">The gray border is always visible. The top line starts at the left and the bottom line starts at the right.</p>
</body>
</html>
* {
box-sizing: border-box;
}
body {
margin: 1rem;
font: 1rem/1.5 system-ui, sans-serif;
color: #172033;
}
.draw-box {
--edge: 2px;
--line-scale: 0;
position: relative;
display: grid;
place-items: center;
inline-size: min(100%, 24rem);
min-block-size: 9rem;
margin-block: 1.25rem;
padding: 1rem;
border: var(--edge) solid #aab5c8;
color: #153770;
text-align: center;
}
.draw-box::before,
.draw-box::after {
content: "";
position: absolute;
inset-inline: calc(-1 * var(--edge));
block-size: var(--edge);
background: #1d4ed8;
pointer-events: none;
transform: scaleX(var(--line-scale));
transition: transform 400ms ease;
}
.draw-box::before {
inset-block-start: calc(-1 * var(--edge));
transform-origin: left center;
}
.draw-box::after {
inset-block-end: calc(-1 * var(--edge));
transform-origin: right center;
}
.draw-box:focus-visible {
--line-scale: 1;
outline: 2px solid #153770;
outline-offset: 4px;
}
@media (hover: hover) {
.draw-box:hover {
--line-scale: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.draw-box::before,
.draw-box::after {
transition: none;
}
}
Position the lines over the borders
The gray line is the link’s actual border. The pseudo-elements are absolutely positioned within the link, which has position: relative. Their containing block reaches the inside edge of its border. Offsetting their sides by --edge aligns the blue strokes with the actual border.
The blue strokes use the same --edge thickness. They follow the link’s width without a fixed pixel length. pointer-events: none keeps the decoration from becoming the pointer target.
Change where each line starts
Both lines span the card before transformation, but scaleX(0) collapses them horizontally. scaleX(1) restores their full width. left center holds the top line’s left edge in place, and right center holds the bottom line’s right edge. Give the bottom line left center to start both from the left. W3C transform-origin definition
Animate hover and keyboard focus
Changing --line-scale changes the pseudo-elements’ transform. The default transition: transform 400ms ease also animates the return when the pointer leaves. Try 200ms for a quicker response. CSS transition property definition
The :hover rule runs only when the primary input supports hovering. :focus-visible gives keyboard users the same lines plus a separate focus outline. The native underline and gray border keep the link recognizable without the effect. Visible keyboard focus selector
Check small screens and reduced motion
Narrow the browser and check that neither blue line spills outside the card. If a line starts at the wrong edge, check transform-origin. If it appears instantly, check the default transition. Turn on reduced motion in the operating system or browser tools and verify the lines change instantly while the focus outline remains. Reduced motion media feature




