CSS Border Animation Enhances the Entire Div Components

I have a div with an animated pulsing border, achieved through CSS keyframes. The issue I am facing is that the animation affects the entire content within the div, whereas I want it to apply only to the border itself. Here's my code:

<div class="main">
<div class="border">

-Text Here-

</div>
</div>

Here is the CSS code for the pulse animation:

@keyframes pulse {
  0% { opacity: 0;  animation-timing-function: ease-in;}
  20% { opacity: .2; animation-timing-function: ease-in;}
  40% { opacity: .4; animation-timing-function: ease-in;}
  45% { opacity: .8; animation-timing-function: ease-in;}
  50%% { opacity: 1; animation-timing-function: ease-in;}
  50%% { opacity: 1; animation-timing-function: ease-out;}
  55% { opacity: .8; animation-timing-function: ease-out;}
  60% { opacity: .4; animation-timing-function: ease-out;}
  80% { opacity: .2; animation-timing-function: ease-out;}
  100% { opacity: 0; animation-timing-function: ease-out;}
}

.border {
  border-radius: 25px;
  border: 8px solid #2C3E50;
  animation-name: pulse;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}  

.main{
  padding: 50px;
  width: 500px;
  height: 500px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

In addition to fixing the animation target, I also wish to trigger the animation when the user clicks anywhere inside the div, and keep it running even after clicking outside the div. Any suggestions or solutions would be greatly appreciated as I'm not very proficient in CSS.

Answer №1

Instead of relying on the opacity property which affects the entire div, consider using border-color to create a unique effect. Check out this Fiddle for a visual demonstration.

@keyframes pulse {
        0%   { border-color: rgba(0, 255, 255, 1); }
        50%  { border-color: rgba(0, 255, 255, 0); }
        100% { border-color: rgba(0, 255, 255, 1); }
}

.border {
  border-radius: 25px;
  border: 8px solid #2C3E50;
  animation: pulse 2s infinite;
}  

To toggle a specific div on and off when clicked, you can utilize a function like the one below:

function changeclass() {
   var targetDiv = document.getElementById("border")
   if (targetDiv.className=="border"){
       targetDiv.className = "";
   }
   else{
       targetDiv.className="border";
   }
} 

Your HTML structure should be structured as follows:

<div class="main">
<div id="border" onclick="changeclass()">
-Insert Text Here-
</div>
</div>

If you want the div to turn on when clicked inside and then off when clicked anywhere on the page, add an event listener to the document as shown in the code snippet below:

document.addEventListener('click', function(e) {
    e = e || window.event;
    var clickedElement = e.target || e.srcElement;   
        if (clickedElement.className == "border"){
            clickedElement.className = "";
        }
        else{
            document.getElementById("border").className = "border";
        }
}, false);

Note that opacity is represented as the fourth value in the rgba color format, ranging from 0.0 to 1.0 for varying levels of transparency.

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

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

"Effortless CSS Formatting in VS Code - A Guide to Automatic Styling

Recently started using VS code and I'm on the lookout for a solution to automatically format CSS whenever I save my work. Strangely, my searches haven't led me to any reliable extensions or clear guides on how to achieve auto formatting in VS cod ...

Encountering an issue stating "The element is not visible at the moment, therefore cannot be interacted with" while trying to access a text box within a window opened via an ajax call

Currently, I am working on a Selenium script using Java to automate a specific process. However, I have encountered an issue where clicking on a dropdown causes a hidden popup to appear through an ajax call. Unfortunately, when attempting to interact with ...

Ways to access the properties of an HTML element with JQuery

Can anyone tell me how to extract the value from a specific HTML element using JQuery? For example: <span id="45463_test"></span> (the proper tags need to be used for the code to work correctly) Specifically, I am looking to retrieve the va ...

Limiting the height of a grid item in MaterialUI to be no taller than another grid item

How can I create a grid with 4 items where the fourth item is taller than the others, determining the overall height of the grid? Is it possible to limit the height of the fourth item (h4) to match the height of the first item (h1) so that h4 = Grid height ...

Boot up 4 collapse feature to conveniently close all other collapsible items at once

Utilizing the Bootstrap 4 Collapse component. My goal is to toggle between collapsible sections like "Categories" and "Brands", displaying only one at a time. The current issue is that multiple collapsible elements are visible simultaneously. .view-cust ...

Creating an interactive map on WordPress: A step-by-step guide

I have successfully created a clickable image on Codepen <div style="width: 1000px; height: 993.73px;"> <img src="https://www.dyfedarchaeology.org.uk/wp/wp-content/uploads/Testmap.svg" alt=&q ...

CSS to resolve HTML alignment problems

I am new to HTML and CSS, trying to practice formulating a few things but my efforts are proving futile. Below is the code and images for your reference. I would appreciate your opinion. .container { display: block; width: 200px; height: 120px; } ...

Automated scrolling effect within a container element

I am working on a div container named wrapper2 that houses a chat interface. Inside the container, there are five messages in a div called chatleft, each containing images. The height of the wrapper2 div is smaller than the combined height of all the messa ...

Error encountered in loop for concatenating html elements: identifier unexpectedly found (jquery + html + php)

I am attempting to concatenate HTML inside a variable within a loop and then return it populated. However, I am encountering an error: Uncaught SyntaxError: Unexpected token += in my code. <a style="cursor: pointer" id="addmore">More Entree ?</ ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

What is the process for locating inline CSS styles within HTML body tags?

I'm currently assisting with CSS tasks and have noticed that some developers have included inline CSS in the code. Our preference is to avoid using inline CSS, so I am curious if there is a way to detect the presence of inline CSS within the <body& ...

Tips for showcasing an image prior to uploading within a personalized design input file

I am facing an issue with displaying multiple images inside custom style input file labels before uploading them to the database. Currently, the script I am using only displays one image at a time and in random positions. My goal is to have each image ap ...

Storing a portion of JSON data within a function called in the HTML document

I've been working with Angular and need to save a portion of JSON data in a variable within a function that is called in an HTML file: <select id="postazione" onchange="postazioneSelezionata()"> <option value="" selected disabled >Cho ...

Attempting to decipher the @media queries CSS code responsible for the slider on this particular website

I'm having trouble with the cover not fully expanding when the browser size is less than 750. I am new to using bootstrap. For reference, I am looking at this website: CSS .slide-wrapper { position: relative; } /* Not using now .carousel-caption ...

There was an issue with the SidebarController function being undefined, as it was expected to be a function

I'm encountering two issues with my demo: Error: [ng:areq] Argument 'SidebarController' is not a function, received undefined http://errors.angularjs.org/1.2.26/ng/areq?p0=SidebarController&p1=not%20aNaNunction%2C%20got%20undefined ...

What is the process for transferring selections between two select elements in aurelia?

I am attempting to transfer certain choices from select1 to select2 when a button is clicked. Below is my HTML code: <p> <select id="select1" size="10" style="width: 25%" multiple> <option value="purple">Purple</option> &l ...

Material User Interface, MUI, Modal, Back to Top Scroll按钮

After spending some time experimenting with scrollTop and the Dialog component (a fullscreen fixed modal) from Material-ui, I found that I couldn't quite get scrollTop to function properly. Whenever I clicked the "go down" button, it would either retu ...

Use CSS to ensure that all elements within the body tag are centered

Is there a way to center the content within the <div> tag on the page without adding an extra container? I can't change the generated markup but I can edit the CSS. My goal is to create CSS that allows me to use the zoom tag in IE for the print ...

Retrieve information from MongoDB and display it in an HTML table

I have some data that follows this structure: { "jobname" : "2018_09_27_test123_0", "totalcalls" : 1836.0, "success" : 3.0, "fail" : 1833.0, "failureReasons" : [ { "reason" : "Unavailable", "count" : 53 ...