使用 HTML 和 CSS 显示霓虹灯文本

htmlcssweb developmentfront end technology更新于 2024/5/30 5:00:17

如今,在网页上创建霓虹灯文本已成为一种趋势,使其更具吸引力。我们可以在网页上创建霓虹灯文本,以产生醒目的效果,并将用户的注意力吸引到网页中包含的重要信息上。

我们可以将霓虹灯文本与徽标、标题、广告等一起使用,以突出显示它们。在本教程中,我们将自定义 CSS 属性 text-shadow 的值来创建霓虹灯文本。

语法

用户可以按照以下语法使用 HTML 和 CSS 创建霓虹灯文本。

text-shadow: 0 0 1.5rem white;

我们在上面的语法中使用了 CSS 属性"text-shadow"来添加发光效果。它以水平偏移量作为第一个值,以垂直偏移量作为第二个值,以模糊半径作为第三个值,并以颜色作为第四个值。

我们始终需要将垂直和水平偏移量设置为 0。为了生成霓虹灯效果,我们需要为 text-shadow CSS 属性使用多个具有不同颜色和模糊半径的值。

示例 1(基本霓虹灯文本)

在下面的示例中,我们使用"text-shadow"CSS 属性为文本创建了基本的霓虹灯效果。我们使用了多个具有不同边框半径和颜色的值来生成霓虹灯效果。

在输出中,用户可以观察到具有霓虹灯效果的文本。

<html>
<head>
   <style>
      .neon {
         font-size: 5rem;
         color: white;
         font-family: 'Arial', sans-serif;
         text-shadow: 0 0 1.5rem white, 0 0 3.5rem white, 0 0 5rem white, 0 0 7rem #ff00de, 0 0 8rem #ff00de, 0 0 10rem #ff00de, 0 0 12rem #ff00de, 0 0 15rem #ff00de;
      }
   </style>
</head>
<body>
   <h2> Creating the <i> basic Neon text </i> using the CSS. </h2>
   <div class = "neon"> Neon Text </div>
   </html><html>
   <head>
      <style>
         .neon {
            font-size: 5rem;
            color: white;
            font-family: 'Arial', sans-serif;
            text-shadow: 0 0 1.5rem white, 0 0 3.5rem white, 0 0 5rem white, 0 0 7rem #ff00de, 0 0 8rem #ff00de, 0 0 10rem #ff00de, 0 0 12rem #ff00de, 0 0 15rem #ff00de;
         }
      </style>
   </head>
<body>
<h2> Creating the <i> basic Neon text </i> using the CSS. </h2>
<div class = "neon"> Neon Text </div>
</html>

示例 2(彩虹霓虹灯文本)

在下面的示例中,我们在文本中创建了彩虹霓虹灯效果。在这里,我们将文本添加到 div 元素中。

我们使用 CSS 中的"background-image"CSS 属性添加了线性渐变作为背景。我们应用了 4 种不同颜色以 45 度角的线性渐变。此外,我们使用了"-webkit-background-clip"CSS 属性将背景图像裁剪成文本的形状。

此外,我们使用了"-webkit-text-fill-color"CSS 属性将透明颜色填充到文本中。因此,它将填充与背景相同的颜色。此外,我们还使用了动画 CSS 属性和"rainbow-keyframe"关键帧。在关键帧中,我们改变"色相旋转"的程度来旋转背景的色相。

在输出中,用户可以观察到文本中的彩虹效果。

<html>
<head>
   <style>
      .rainbow-neon {
         font-size: 5rem;
         background-image: linear-gradient(45deg, #f3626b, #6181ff, #a25bf2, #ffc100);
         -webkit-background-clip: text;
         -webkit-text-fill-color: transparent;
         animation: rainbow-keyframe 3s linear infinite;
      }
      @keyframes rainbow-keyframe {
         0% {
            filter: hue-rotate(0deg);
         }
         100% {
            filter: hue-rotate(360deg);
         }
      }
   </style>
</head>
<body>
   <h2> Creating the <i> rainbow Neon text </i> using the CSS </h2>
   <div class="rainbow-neon"> It's raining actually. </div>
</body>
</html>

示例 3(闪烁的霓虹灯文本)

在下面的示例中,我们创建了闪烁的霓虹灯文本。在这里,我们使用了 CSS 属性"text-shadow"为文本添加了霓虹灯效果。此外,我们还使用了"animation"属性来添加闪烁动画。在"flickering"关键帧中,我们更改了"text-shadow"属性的值。

在输出中,我们可以看到带有发光效果的闪烁文本。

<html>
<head>
   <style>
      .flicker {
         font-size: 4rem;
         color: blue;
         text-shadow: 0 0 1.5rem white, 0 0 3.5rem white, 0 0 5rem white, 0 0 7rem #ff00de, 0 0 8rem #ff00de, 0 0 10rem #ff00de, 0 0 12rem #ff00de, 0 0 15rem #ff00de;
         color: #fff;
         animation: flicker 0.5s linear infinite;
      }
      @keyframes flicker {
         0%, 5%, 10%, 15%, 20%, 25%, 30%, 35%, 40%, 45%, 50%, 55%, 60%, 65%, 70%, 75%, 80%, 85%, 90%, 95%, 100%
         { text-shadow: 0 0 1.5rem white, 0 0 3.5rem white, 0 0 5rem white, 0 0 7rem #ff00de, 0 0 8rem #ff00de, 0 0 10rem #ff00de, 0 0 12rem #ff00de, 0 0 15rem #ff00de;
         }
         1%, 6%, 11%, 16%, 21%, 26%, 31%, 36%, 41%, 46%, 51%, 56%, 61%, 66%, 71%, 76%, 81%, 86%, 91%, 96%
         {
            text-shadow: none;
         }
      }
   </style>
</head>
<body>
   <h2> Creating the <i> flickering Neon text </i> using the CSS. </h2>
   <div class = "flicker"> This text is flickering. </div>
</body>
</html>

示例 4(渐变霓虹文本)

在下面的示例中,我们为文本添加了渐变效果。我们使用了"background-image"属性将渐变效果应用于文本。

在输出中,用户可以观察到渐变效果下的文本颜色。此外,用户还可以通过将不同的颜色值传递给 linear-gradient() 函数参数来更改渐变颜色。

<html>
<head>
   <style>
      .gradient {
         font-size: 3rem;
         background-image: linear-gradient(45deg, #00dbde, #fc00ff);
         -webkit-background-clip: text;
         -webkit-text-fill-color: transparent;
      }
   </style>
</head>
<body>
   <h2> Creating the <i> Gradient Neon text </i> using the CSS. </h2>
   <div class = "gradient"> Check gradient in Neon text </div>
</body>
</html>

用户学习了如何仅使用 HTML 和 CSS 创建霓虹灯文字。我们学习了如何从基础的霓虹灯文字创建到包含彩虹效果和闪烁效果等动画的高级霓虹灯文字。


相关文章


有用资源