Creating an animated border that fades to transparency using keyframes

I am attempting to create a simple animation of a circle with a border that transitions from red to a transparent color. My approach is to initially set the color to red and then animate it to transparent using keyframes as follows:

.pulse{
  margin: 20px;
  width:100px;
  height: 100px;
  background-color: red;
  border-radius: 100px;
  animation-name: pulse;
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: ease;
}

@keyframes pulse{
  0%{border:solid 1px rgba(255, 0, 0, 1)}
  100%{border:solid 1px rgba(255, 0, 0, 0)}
}
    <div class="animation">
      <div class="pulse"></div>
    </div>

Initially, it seems like nothing is happening, but upon closer inspection, I realize that the animation is indeed working. However, the transparent animation is being displayed on top of the existing red border, creating the illusion that nothing is changing. My goal is to have the border transition smoothly from red to transparent, giving the appearance of pulsating without altering the size of the circle.

Answer №1

Consider using box-shadow in place of border

Code Snippet

.pulse {
  margin: 20px;
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 100px;
  animation-name: pulse;
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 4px rgba(255, 0, 0, 1);
  }
  100% {
    box-shadow: 0 0 0 0px rgba(255, 0, 0, 1);
  }
}
<div class="animation">
  <div class="pulse"></div>
</div>

Answer №2

Your background color is the same as the border color, which is why you won't see anything. Additionally, the border definition in your animation is incorrect - the width should come before the border style:

For example, it should be 1px solid color instead of solid 1px rgba(255,0,0,1).

.pulse {
  animation: pulse 1s ease infinite alternate;
  background-color: #ddd;
  border-radius: 100px;
  height: 100px;
  margin: 20px;
  width: 100px;
}

@keyframes pulse {
  0% {
    border: 1px solid rgba(255, 0, 0, 1)
  }
  100% {
    border: 1px solid rgba(255, 0, 0, 0)
  }
}
<div class="animation">
  <div class="pulse"></div>
</div>

However, if you are aiming for a pulsating effect, I recommend using transform: scale() to achieve the desired result.

@keyframes pulse{
  from { transform: scale(1) }
  to { transform: scale(.75) }
}

.pulse{
  margin: 20px;
  width:100px;
  height: 100px;
  border-radius: 100px;
  background-color: red;
  animation: pulse 1s ease infinite alternate;
}
<div class="animation">
      <div class="pulse"></div>
    </div>

Answer №3

To achieve the desired effect, simply include the following CSS rule background-clip: padding-box; within the .pulse element. For more information, you can refer to this resource. Another option mentioned in the previous response is to utilize box-shadow, but bear in mind that box-shadow does not account for space around the element, resulting in a different outcome.

.pulse {
  background-clip: padding-box;
  margin: 20px;
  width:100px;
  height: 100px;
  background-color: red;
  border-radius: 100px;
  animation-name: pulse;
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-timing-function: ease;
}

@keyframes pulse{
  0%{border:solid 1px rgba(255, 0, 0, 1)}
  100%{border:solid 1px rgba(255, 0, 0, 0)}
}
<div class="animation">
  <div class="pulse"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An unusual outcome occurred while attempting to duplicate the text

After copying the letter A, I noticed that an empty string is being logged to the console instead of the expected A. However, when I paste, the console does successfully log the letter A. document.addEventListener('copy', handler); document ...

Is it possible to insert an image as the background of a specific word within a paragraph?

I am looking to enhance certain words in a paragraph by adding a brushstroke image in the background, similar to this example: https://i.sstatic.net/KzjGn.png. I attempted to use a simple background for a span, but the image exceeded the padding and was cu ...

Vue component always renders a new line

Whenever I include a <component> tag in my main application, it always renders on a new line. Is there a way to make it inline? I am trying to achieve something like this: <p>This is a component :<component></component></p> & ...

CSS designed for small-screen laptops is also being utilized on iPads running Safari

To specifically target the iPad/Safari browser and accommodate some minor differences in appearance compared to windows/linux browsers, we are implementing the following HTML meta tag and style query: <meta name="viewport" content="width=device-width, ...

Masonry List with Next.js Image Fill

I am encountering an issue where the normal HTML img tag works perfectly fine inside my Masonry List. However, when I use the Next.js Image Component with a layout of fill, it fails to render anything. Custom Styles: const ImageList = styled('ul&apos ...

Adjust the width of a div element to a specific size, center the div horizontally, and justify

My issue may be small, but I am struggling to find a solution. I have a content header that is 864px wide with a background image repeated vertically and a footer image. I now have a <div> positioned over the background image and I want it to be 855p ...

A guide on aligning a CSS item perfectly in the center using absolute positioning

I have been searching for a solution to my problem, but all I found were answers that addressed similar issues in a slightly different way. My challenge involves placing multiple smaller images on top of a larger background image. To achieve this, I utili ...

Utilizing a flexbox container within a table container, implementing PRE tags for handling overflow

Currently, I am diligently working on an intricate DASHBOARD utilizing bootstrap-4. A peculiar issue arose with the implementation of <pre> within a Flexbox. However, upon closer inspection, it became evident that this was not a flexbox or <pre> ...

Table does not have appropriate rowspan and colspan attributes

I've created a table using bootstrap, but it's not displaying as intended. I used row-span and col-span to format the table, and while most rows are correct, some are not appearing on the page. To better illustrate the issue, I have included an ...

Insert a new row into a table using tablesorter jQuery

Is it possible to add a row to a table built with the jQuery library "Tablesorter" by simply clicking on a button? I have tried this approach, but unfortunately, the new row does not inherit the CSS properties of Tablesorter. As a result, I am unable to se ...

Angular input range slider that automatically rounds decimal values from the data bindings

I've implemented a range slider within an Angular form to capture and display recorded values. <input [formControlName]="object.id" [id]="object.id" type="range" [(ngModel)]="object.answer" [step]="objec ...

"Learn how to specifically extract parent <li> elements without including the child <li> elements

When attempting to crawl a website [URL: https://www.khaasfood.com/shop/], my first task is to retrieve the categories with their hierarchy. The .container element contains a list of li tags representing parent categories. Each parent category may also h ...

Swap out the div block with a new one

I am currently facing an issue with my JavaScript code. The code is supposed to remove block1 and replace it with block2 when an onclick function is triggered. function buyerclick() { div = document.getElementById('block2'); div.style.displa ...

Issue encountered when attempting to invoke a PHP function using Javascript through AJAX

I'm currently working on incorporating a PHP function into my HTML file using Ajax (which also contains JavaScript). My goal is to retrieve all locally stored JSON files. I've been following a guide at , but I'm facing some challenges. When ...

How can we make a link stand out when clicked in jQuery, and additionally display a hidden element?

Check out the codepen: http://codepen.io/tristananthonymyers/full/BRPmKa/ The goal is to have the ".active-link:after" style applied only to the clicked link and show the corresponding page like "#about-page". However, the issue is that the "#about-page" ...

Combining Three HTML Tags Into a Single Page

I currently have a standard PHP webpage that consists of the header, body, and footer sections. Within this setup, I use the following code: <?php include('header.html'); ?> The header.html file contains <html>... content </html& ...

ReactJS not responding to Bootstrap toggler button

My Bootstrap toggler button is not working in ReactJS. I did not include the jQuery scripts because I imported Bootstrap at the top. The button should only appear and function on small devices. import React from 'react'; import 'bootstrap/d ...

The newly added highlighted CSS in jQuery cannot be removed once an alert is generated

http://jsfiddle.net/Cyjye/ I am a beginner with jquery. I have created an HTML table shown in the jsfiddle link. Initially, the selection should be made from the second row, second cell, but in my HTML table, the selection is being made from the first ro ...

Overridden CSS rules

Having a slight CSS dilemma. Within my webpage's html, I have the following structure: <div class='box-div'> <div>Entry 1</div> <div class='hide'>Entry 2</div> </div> Here is my associated ...

What is the best way to create a triangle-shaped div using CSS styling?

Seeking assistance in creating a triangular shape using CSS within a rectangular division. Grateful for any guidance provided. ...