DEV Community

Cover image for How to create neon text using vanilla CSS
Amir-Lotfi
Amir-Lotfi

Posted on

5 2

How to create neon text using vanilla CSS

Learn how to implement neon effects using CSS, a technique for adding glowing to text.

To add a glow effect to the text, we need text-shadow property. The text-shadow property accepts a comma-separated list of shadows that applies to the text. Each shadow is described by some combination of X and Y offsets from the text, blur radius, and color.

text-shadow: [x-offset] [y-offset] [blur-radius] [color];

Creating glow effect

First, we need to add a white glow to the outer edges of the text’s letters with a small blur radius. Next, we add the red glow by adding multiple shadows separated by a comma. We need all these shadows so that they can be stacked over one another to add more depth to the glow. To increase the size of the glow effect, we would increase the blur radius of shadows.

.neon-text {
  text-shadow: 
    #FFFFFF 0 0 12px,
    #A91079 0 0 25px,
    #A91079 0 0 35px,
    #A91079 0 0 45px;
  text-align: center;
  font-size: 2.5rem;
  color: #fff;
}
Enter fullscreen mode Exit fullscreen mode

Adding the flickering effect

One thing you might notice about neon signs is that some of them — particularly older ones — tend to flicker. The light kind of goes in and out. We can do the same sort of thing with CSS animations.

To achieve that, we create a custom animation named neon using @keyframes. Then we move the shadows to the keyframe. The keyframe should apply and completely remove the shadows at chosen points during animation to create the flickering effect.

@keyframes neon {
  0%, 18%, 22%, 25%, 53%, 57%, 100% {
    text-shadow: 
    #FFFFFF 0 0 12px,
    #A91079 0 0 25px,
    #A91079 0 0 35px,
    #A91079 0 0 45px;
  }

  20%, 24%, 55% {
    text-shadow: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, we call the animation where we want the light to flicker.

.neon-text {
  animation: neon 3s infinite alternate;
  text-align: center;
  font-size: 2.5rem;
  color: #fff;
}
Enter fullscreen mode Exit fullscreen mode

You can see the working animation and play with the final code at nexuscode.online

Challenge your skills

Fork the code in the link above and experiment with various hues and colors as well as blur radius sizes, and don’t forget to try out different animations, too. Leave a comment if you’ve created a neat shadow effect you want to share.
Thanks for reading.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay