Home >Web Front-end >CSS Tutorial >How to Create an Animated Dashed Border in CSS Without Using SVG or JavaScript?

How to Create an Animated Dashed Border in CSS Without Using SVG or JavaScript?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 14:40:29951browse

How to Create an Animated Dashed Border in CSS Without Using SVG or JavaScript?

Dashed Border Animation without SVG in CSS3

The question raised is how to implement the animated dashed border effect demonstrated in the supplied article without utilizing SVG or JavaScript within a WordPress blog's post divs.

Solution to Achieving Non-SVG Dashed Border Animation:

Fortunately, it is possible to achieve this effect purely with CSS, leveraging multiple backgrounds and animating their positions.

Here's a code snippet that showcases the implementation:

<code class="css">.border {
  height: 100px;
  width: 200px;
  background: linear-gradient(90deg, blue 50%, transparent 50%),
              linear-gradient(90deg, blue 50%, transparent 50%),
              linear-gradient(0deg, blue 50%, transparent 50%),
              linear-gradient(0deg, blue 50%, transparent 50%);
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 16px 4px, 16px 4px, 4px 16px, 4px 16px;
  background-position: 0px 0px, 212px 116px, 0px 116px, 216px 0px;
  padding: 10px;
  transition: background-position 2s;
}
.border:hover {
    background-position: 212px 0px, 0px 116px, 0px 0px, 216px 116px;
}</code>
<code class="html"><div class="border">Some text</div></code>

In this code:

  • Multiple linear gradients are used to create the dashed border effect.
  • The background-position property is animated on hover to shift the gradient positions and create movement.
  • The hover state triggers the animation, giving the impression of a dashed border with a moving effect.

The above is the detailed content of How to Create an Animated Dashed Border in CSS Without Using SVG or JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn