Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

วิธีสร้างข้อความเรืองแสง (Glowing Text) ด้วย CSS

Animation Text-Shadow สร้างเอฟเฟกต์ส่องแสงสะพรั่ง

ใช้ text-shadow หลายชั้น ร่วมกับ @keyframes animation ทำให้ข้อความดูเหมือนเรืองแสงกระพริบสลับสี สร้างบรรยากาศไซเบอร์พังค์/นีออนได้ง่าย

โค้ดตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      background-color: rgb(0, 0, 0);
      font-family: cursive;
    }
    .glowing {
      font-size: 80px;
      color: #fff;
      text-align: center;
      animation: glowing 1s ease-in-out infinite alternate;
    }
    @keyframes glowing {
      from {
        text-shadow:
          0 0 10px #fff, 0 0 20px #fff,
          0 0 30px #4f00b6, 0 0 40px #17057c,
          0 0 50px #29c2ff, 0 0 60px #96fbff,
          0 0 70px #1f0352;
      }
      to {
        text-shadow:
          0 0 20px #fff, 0 0 30px #2b4ebe,
          0 0 40px #4276e6, 0 0 50px #4644cf,
          0 0 60px #3533d1, 0 0 70px #493cbb,
          0 0 80px #8beeff;
      }
    }
  </style>
</head>
<body>
  <h1 class="glowing">GLOWING TEXT EXAMPLE</h1>
</body>
</html>

เทคนิคสำคัญ

  • text-shadow รับค่าได้หลายค่า คั่นด้วยจุลภาค – ยิ่งเยอะยิ่งหนา/สว่าง
  • animation: glowing 1s ease-in-out infinite alternate – วนลูปไป-กลับ (alternate)
  • พื้นหลังดำ (background: black) ทำให้แสงเด่นชัด
  • ใช้ cursive font เพิ่มความรู้สึกมือเขียน/นีออน

ประยุกต์ใช้

ปรับระยะเวลา (duration), easing, สีเงา ได้ตามธีม ไปปรับใน @keyframes เพื่อสร้างเอฟเฟกต์ Neon, Fire, Magic, Hologram ฯลฯ