This is a CSS neon glow border: a button framed by a neon tube, with two highlights running around the border towards each other โ and now and then the tube dies with a flicker and ignites again with a flash, like a real sign. Pure CSS, no JavaScript, no SVG. It works on a <button> or a link, grows with its text, and every setting is a CSS variable you can change live. Below: how the glowing border and the running highlights work, the settings, and the traps.
Why this effect
This is the frame from the panels on this site, turned into a button. Most animated glowing borders spin one gradient round and round forever โ after a few seconds you stop seeing it. A neon tube is more alive: it has a hot spot that travels, and once in a while it fails and comes back. The flicker at ignition is my favourite part; it's what makes it look like glass and gas, not like a gradient.
What you'll build
A dark button with a lime word. Its border is a thin bright line with a soft glow outside and inside. Two highlights slide along the border from opposite sides โ one bright with a white-hot centre, one dimmer. Every few seconds the tube flickers out in a third of a second, stays dark for a moment and ignites with four quick flickers and a short flash. The word itself stays lit, so the button is always readable.
How the neon border works
1. The button: padding is the gap to the tube
The button is an ordinary element with position: relative; the tube and the highlights are its ::after and ::before, so the HTML stays one tag. Everything is in em from the text size, so a longer or bigger label gets a bigger tube.
.nb-btn {
position: relative;
padding: 0.35em 0.8em; /* space between the word and the tube */
border: 0;
border-radius: 0.25em;
background: #0e1012;
font: 700 72px "Space Grotesk", system-ui, sans-serif;
color: #d4ff00;
text-shadow: 0 0 0.17em rgb(212 255 0 / 0.6);
}
2. The tube: one border, four shadows
A neon glow border is mostly box-shadow. The line itself is a 2px border on the ::after; then a 1px spread ring thickens it a little, a tight glow and a wide faint glow light the air around it, and an inset glow lights the inside of the button โ real tubes light both sides.
.nb-btn::after {
content: "";
position: absolute;
inset: -1px;
border-radius: inherit;
border: 2px solid rgb(212 255 0 / 0.85);
box-shadow:
0 0 0 1px rgb(212 255 0 / 0.25),
0 0 0.25em 2px rgb(212 255 0 / 0.5),
0 0 0.83em rgb(212 255 0 / 0.2),
inset 0 0 0.25em rgb(212 255 0 / 0.2);
}
3. Running highlights: a conic gradient and a mask
The highlights are a conic-gradient on the ::before โ a pie of colour around the centre that is transparent almost everywhere and bright in one narrow slice. A mask cuts out the middle and leaves only a 4px ring along the edge: content-box is the inside (the element has padding: 4px), and mask-composite: exclude keeps what is not inside.
.nb-btn::before {
inset: -2px;
padding: 4px;
background:
conic-gradient(from var(--nb-a), transparent 78%,
#d4ff00 86%, #fff 91%, #d4ff00 95%, transparent),
conic-gradient(from calc(180deg - var(--nb-a)),
transparent 86%, #d4ff00 93%, transparent);
mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
mask-composite: exclude;
}
The second gradient turns the other way (180deg - angle), so the two highlights run towards each other, meet and pass.
4. Spinning an angle with @property
You can't animate conic-gradient(from โฆ) directly โ the browser sees a whole new image every frame. But you can animate a custom property if you tell the browser its type:
@property --nb-a {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
@keyframes nb-spin { to { --nb-a: 360deg; } }
Now --nb-a is a real angle that goes from 0 to 360 degrees, and the gradients that use it turn with it. On a wide button the highlights slow down on the short sides and speed up on the long ones โ a conic gradient turns at a steady angle, not at a steady distance. It reads as light running along glass.
5. Dying and igniting: flicker as numbers
In the video the flicker is keyframes in an 8-second cycle. On a real page you want to choose how long the tube stays lit, and keyframes would stretch the flicker along with the pause. So the full version runs a clock โ --nb-t from 0 to 1 per cycle โ and --nb-s is the seconds since the tube started to die. Each flicker is one jump in brightness: an amount times a 0 โ 1 ramp.
--nb-s: calc(var(--nb-t) * (var(--nb-pause) + 2.1) - var(--nb-pause));
--nb-o: calc(1
- 0.8 * clamp(0, var(--nb-s) / 0.064, 1) /* drops */
+ 0.6 * clamp(0, (var(--nb-s) - 0.064) / 0.04, 1) /* catches */
/* ...eight more jumps... */);
The tube and the highlights use opacity: var(--nb-o), and at full ignition filter: brightness() flashes to 2 and settles. The flicker always takes the same 2.1 seconds; --nb-pause only changes how long the tube burns between failures. The same clock is explained in How to Add a Pause Between CSS Animation Loops.
Make it yours
| Variable | What it does |
|---|---|
--nb-pause |
Seconds the tube stays lit before it flickers out again. |
--nb-lap |
Seconds for the highlights to go round. 2โ3 s is busy, 10 s is calm. |
--nb-glow |
Glow strength: 0.5 is a thin line, 2 is a sign seen in fog. |
--nb-size |
Text size; padding, corners and glow follow it. |
--nb-color |
Tube and word colour. |
--nb-flash |
The hot centre of the bright highlight. |
Spacing: padding is the space between the word and the tube (in em, so it grows with the text); margin: 0 is the space around the button on your page โ set your own.
Gotchas
- Glow gets cut off: a parent with
overflow: hiddenclips the outer glow. Give the parent some padding or let it overflow. mask-composite: excludeis the standard name; very old Safari only knows-webkit-mask-composite: xor. Without it you'd see a filled pie instead of a ring.- The pause is a
<number>: write--nb-pause: 10, not10s. - Readability: only the tube flickers; the word stays lit. If you make the word flicker too, the button is unreadable for a moment โ fine for a sign, bad for a call to action.
Accessibility
It's a real <button> (or <a>), so it's focusable and works with the keyboard; :focus-visible adds a quiet ring outside the tube. The glow and the flicker are decoration only. With prefers-reduced-motion: reduce the tube just glows: no running highlights, no flicker.
Variations
- The same tube as letters: Neon Sign โ neon text with one failing letter.
- The same yellow phosphor behind glass: CRT Screen.
- Highlights only, no flicker: set
--nb-pause: 60.
FAQ
How do I make a neon glow border in CSS?
Give the element a thin bright border and several box-shadows in the same colour: a tight one, a wide faint one and an inset one for the inside. Put it on a pseudo-element if you want to animate the tube without touching the text.
How do I animate a glowing border in CSS?
Use a conic-gradient on a pseudo-element, cut it down to a ring with mask and mask-composite: exclude, and spin the gradient's starting angle โ registered with @property so it can be animated. No JavaScript needed.
How do I make a neon button flicker?
Animate the tube's opacity through a few quick drops and recoveries: down to 0.2, up to 0.8, down to 0.1, up to 0.6, off. Igniting is the same in reverse with a short brightness() flash at the end. Keep the word itself lit so the button stays readable.
Does the glowing border work on any size?
Yes โ everything is in em from the text size, so a small nav link and a big hero button get a tube in proportion. Only the line width and the 4px highlight ring stay in px, like a real tube's thickness.