CSS3 realizes the glowing border effect

CSS3 realizes the glowing border effect

Operation effect:

html

<!-- This element is not visible. The DOM is generated by JavaScript -->
<div class="root" style="display: none;">
  <div>
    <div class="side left"></div>
    <div class="side top"></div>
    <div class="side right"></div>
    <div class="side bottom"></div>
  </div>
</div>

CSS

body {
  margin: 0;
  width: 100vw;
  height: 100vh;
  background: #010326;
}

.root {
  --glow_width: 2px;
  --animation_length: 2s;
  --delay_factor: 2;

  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 300px;
  transform: translate(-50%, -50%) rotate(45deg);

/* Uncomment the line below to see how this system is set up */
/* border: 1px dashed red; */
  overflow: hidden;
}

.side {
  position: absolute;
  top: 0;
  left: 0;
}

.side.left,
.side.right {
  width: var(--glow_width);
  height: 0;
  background: linear-gradient(to bottom, transparent, #c03225, transparent);
  animation: heightAnim var(--animation_length) linear infinite,
    hider calc(var(--delay_factor) * var(--animation_length))
      var(--animation_length) infinite;
}

.side.top,
.side.bottom {
  width: 100%;
  height: var(--glow_width);
  background: linear-gradient(to left, transparent, #c03225, transparent);
  animation: widthAnim var(--animation_length) 0s linear infinite,
    hider calc(var(--delay_factor) * var(--animation_length))
      var(--animation_length) infinite;
}

.side.right {
  left: auto;
  right: 0;
  height: 0;
  animation-delay: calc(var(--animation_length) / 2);
  animation-direction: normal, reverse;
}

.side.bottom {
  top:auto;
  bottom: 0;
  width: 0;
  animation-delay: calc(var(--animation_length) / 2);
  animation-direction: normal, reverse;
}

@keyframes heightAnim {
  0% {
    height: 0px;
  }
  50% {
    height: 300px;
    transform: initial;
  }
  100% {
    transform: translate(0, 300px);
  }
}

@keyframes widthAnim {
  0% {
    width: 0px;
  }
  50% {
    width: 300px;
    transform: initial;
  }
  100% {
    transform: translate(300px, 0px);
  }
}

@keyframes hider {
  0%,
  50% {
    opacity: 0;
  }
  51%,
  100% {
    opacity: 1;
  }
}

js

let template = `<div class="root" style="transform: translate(-50%, -50%) rotate({{ value }})">
<div>
    <div class="side left"></div>
    <div class="side top"></div>
    <div class="side right"></div>
    <div class="side bottom"></div>
  </div>
</div>`

let segments = 9
for(let i = -segments; i < segments; i++){
  document.body.innerHTML += template.replace("{{ value }}", 90/segments * i + "deg")
}

// document.body.innerHTML += template.replace("{{ value }}", 90/segments * 0 + "deg")

The above is the details of how to achieve the glowing border effect with CSS3. For more information about the glowing border effect with CSS3, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  MySQL practical window function SQL analysis class students' test scores and living expenses

>>:  Detailed explanation of the process of troubleshooting the cause of high CPU usage under Linux

Recommend

JavaScript to display hidden form text

This article shares the specific code of JavaScri...

Eight examples of how Vue implements component communication

Table of contents 1. Props parent component ---&g...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

Solution to the problem of large font size on iPhone devices in wap pages

If you don't want to use javascript control, t...

Detailed steps for configuring virtual hosts in nginx

Virtual hosts use special software and hardware t...

Detailed explanation of Linux text processing command sort

sort Sort the contents of a text file Usage: sort...

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Introduction to RHCE bridging, password-free login and port number modification

Table of contents 1. Configure bridging and captu...

The ultimate solution for writing bash scripts with nodejs

Table of contents Preface zx library $`command` c...

Detailed steps to install Anaconda on Linux (Ubuntu 18.04)

Anaconda is the most popular python data science ...

A brief analysis of the responsiveness principle and differences of Vue2.0/3.0

Preface Since vue3.0 was officially launched, man...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...

Steps to repair grub.cfg file corruption in Linux system

Table of contents 1. Introduction to grub.cfg fil...