How to sync css animations with HTML5 audio ?
Last Updated :
26 Apr, 2025
In this article, you will learn to synchronize CSS Animations with audio to create dynamic good websites. This is basically used in online games or animated stories to create some kind of audio effects.
We will first analyze the audio and ultimately sync it with the CSS animation property.
Approach: To explain the sync of audio, we are going to consider an example of a drum sound.
- Analyzing the sound
- Designing the drum
- Syncing them together
1. Analyzing the sound:
To add the audio to the web page, we need to use the HTML <audio/> tag. We will use the preload property to load the audio before the DOM.
<audio id="drum" src="path_to_audio" preload="auto">
... Your browser does not support the audio element.
</audio>
Audio used: Bongos_Drum
We need to figure out the point of time where the sound actually beats and to make movements in the animation at the right time. We can use any software that creates a visualization of the audio. Software like audacity or twisted wave audio editor can help you do this job. We are using twisted wave audio editor which is an online editor which looks like the following.
The timeline in the software will help you to get the point of time at which animation should be done. We need to open the required audio file in this software. Once we get the time in seconds convert them to percentages to get the @keyframes values.
Result:
seconds
| 0s
| 0.183s
| 0.329s
| 0.486s
| 0.636s
| 0.790s
| 0.953s
| 1.094s
| 1.266s
|
%
| 0
| 9.9
| 17.3
| 24.6
| 31.5
| 39
| 47
| 54
| 62
|
——–
| ——-
| ——-
| ——-
| ——-
| ——-
| ——-
| ——-
| ——-
| ——
|
seconds
| 1.439s
| 1.606s
| 1.783s
| 1.927s
|
|
%
| 68
| 78.4
| 87.7
| 95
|
2. Designing the drum: We will design a drum with two sticks and make an animation of sticks beating the drum.
To design the drum we are using the following HTML structure.
In this figure, we have a parent with id= “drum”. It consists of three HTML div with class top, mid and bottom indicating the different portions of the drum, a <audio> tag, and two sticks with class s1 and s2.
HTML Code:
HTML
<!DOCTYPE html>
< html >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >How to Sync CSS Animations with HTML5 Audio ?</ h2 >
< h3 >A Drum Sound with CSS Animation</ h3 >
< div id = 'drum' >
< div class = "top" >
< div class = "stick s1" ></ div >
< div class = "stick s2" ></ div >
</ div >
< div class = "mid" ></ div >
< div class = "bottom" >
< div class = "bar b1" ></ div >
< div class = "bar b2" ></ div >
</ div >
< audio src =
controls>
</ audio >
</ div >
</ body >
</ html >
|
Output:
CSS Code: Adding some styles to the above HTML code.
CSS
<style>
#drum{
width : 50% ;
margin : auto ;
position : relative ;
margin-top : 100px ;
}
audio{
position : relative ;
left : 50% ;
transform:translate( -50% );
}
h 2 {
text-align : center ;
margin : 50px
}
. top {
border : 5px solid blue ;
width : 200px ;
margin : auto ;
height : 100px ;
border-radius: 100% ;
background : white ;
box-shadow: 0px 2px 10px black inset ;
position : relative
}
.mid{
width : 180px ;
background : blue ;
height : 150px ;
margin : auto ;
position : relative ;
top : -60px ;
z-index : -1 ;
}
. bottom {
width : 180px ;
margin : auto ;
height : 100px ;
border-radius: 100% ;
background : blue ;
position : relative ;
top : -120px ;
box-shadow: 0px 25px 20px black ;
}
.mid::before{
content : "" ;
position : absolute ;
left : -8px ;
height : 130px ;
border : 10px solid blue ;
transform:rotate( -5 deg);
}
.mid::after{
content : "" ;
position : absolute ;
right : -8px ;
height : 130px ;
border : 10px solid blue ;
transform:rotate( 5 deg);
}
.bar{
height : 130px ;
border : 3px solid ;
width : 0px ;
display :inline- block ;
position : relative ;
}
.b 1 {
left : 20px ;
transform:rotate( -2 deg);
top : -48px ;
}
.b 2 {
left : 130px ;
transform:rotate( 2 deg);
top : -42px ;
}
.stick{
height : 130px ;
border : 3px solid brown;
width : 0px ;
display :inline- block ;
position : absolute ;
border-radius: 50px ;
top : -70px ;
}
.s 1 {
transform:rotate( -45 deg);
}
.s 2 {
left : 180px ;
transform:rotate( 45 deg);
}
</style>
|
3. Sync with audio:
Animation Type: Sticks beating the drum on click of the audio play button
JavaScript Code: To do this we need JavaScript which adds two classes .active1 and .active2 to the sticks HTML div with classes s1 and s2 to start the animation.
Javascript
<script>
var audio = document.getElementsByTagName( 'audio' )[0];
audio.onplay=()=>{
document.getElementsByClassName( 'stick' )[0].classList.add( 'active1' );
document.getElementsByClassName( 'stick' )[1].classList.add( 'active2' );
console.log( "hi" )
}
audio.onpause=()=>{
document.getElementsByClassName( 'stick' )[0].classList.remove( 'active1' );
document.getElementsByClassName( 'stick' )[1].classList.remove( 'active2' );
}
</script>
|
We will add this code in the above HTML itself. It is selecting the HTML audio tag by adding the onplay classes and removing onpause.
The .active1 and .active2 classes add the animation like the following syntax
.active1{
animation : tap1 2.1s ;
}
.active2{
animation : tap2 2.1s ;
}
The tap1 and tap2 are the animation names for the two sticks in the figure. The duration of the audio is around 2.1s and we will be playing the animation for that many seconds.
CSS Code: To move the sticks we are rotating stick s1 between -45o and -60o and stick s2 between 45o and 60o with a bit of adjustment in their lengths using @keyframes.
CSS
@keyframes tap 1 {
9% {
transform:rotate( -45 deg);
height : 130px ;
}
24.6% {
transform:rotate( -45 deg);
height : 130px ;
}
39% {
transform:rotate( -45 deg);
height : 130px ;
}
54% {
transform:rotate( -45 deg);
height : 130px ;
}
68% {
transform:rotate( -45 deg);
height : 130px ;
}
87.7% {
transform:rotate( -45 deg);
height : 130px ;
}
0% {
transform:rotate( -60 deg);
height : 90px ;
}
17.3% {
transform:rotate( -60 deg);
height : 90px ;
}
31.5% {
transform:rotate( -60 deg);
height : 90px ;
}
47% {
transform:rotate( -60 deg);
height : 90px ;
}
62% {
transform:rotate( -60 deg);
height : 90px ;
}
78.4% {
transform:rotate( -60 deg);
height : 90px ;
}
95% {
transform:rotate( -60 deg);
height : 90px ;
}
}
@keyframes tap 2 {
0% {
transform:rotate( 45 deg);
height : 130px ;
}
17.3% {
transform:rotate( 45 deg);
height : 130px ;
}
31.5% {
transform:rotate( 45 deg);
height : 130px ;
}
47% {
transform:rotate( 45 deg);
height : 130px ;
}
62% {
transform:rotate( 45 deg);
height : 130px ;
}
78.4% {
transform:rotate( 45 deg);
height : 130px ;
}
95% {
transform:rotate( 45 deg);
height : 130px ;
}
9.9% {
transform:rotate( 60 deg);
height : 90px ;
}
24.6% {
transform:rotate( 60 deg);
height : 90px ;
}
39% {
transform:rotate( 60 deg);
height : 90px ;
}
54% {
transform:rotate( 60 deg);
height : 90px ;
}
68% {
transform:rotate( 60 deg);
height : 90px ;
}
87.7% {
transform:rotate( 60 deg);
height : 90px ;
}
}
|
Example 1: The complete running code of the sticks beating the drum with sound.
HTML
<!DOCTYPE html>
< html >
< head >
< style >
#drum{
width:50%;
margin:auto;
position:relative;
margin-top:100px;
}
audio{
position:relative;
left:50%;
transform:translate(-50%);
}
h2{
text-align:center;
margin:50px
}
.top{
border:5px solid blue;
width:200px;
margin:auto;
height:100px;
border-radius:100%;
background:white;
box-shadow:0px 2px 10px black inset;
position:relative
}
.mid{
width:180px;
background:blue;
height:150px;
margin:auto;
position:relative;
top:-60px;
z-index:-1;
}
.bottom{
width:180px;
margin:auto;
height:100px;
border-radius:100%;
background:blue;
position:relative;
top:-120px;
box-shadow:0px 25px 20px black;
}
.mid::before{
content:"";
position:absolute;
left:-8px;
height:130px;
border:10px solid blue;
transform:rotate(-5deg);
}
.mid::after{
content:"";
position:absolute;
right:-8px;
height:130px;
border:10px solid blue;
transform:rotate(5deg);
}
.bar{
height:130px;
border:3px solid;
width:0px;
display:inline-block;
position:relative;
}
.b1{
left:20px;
transform:rotate(-2deg);
top:-48px;
}
.b2{
left:130px;
transform:rotate(2deg);
top:-42px;
}
.stick{
height:130px;
border:3px solid brown;
width:0px;
display:inline-block;
position:absolute;
border-radius:50px;
top:-70px;
}
.s1{
transform:rotate(-45deg);
}
.s2{
left:180px;
transform:rotate(45deg);
}
@keyframes tap1{
9%{
transform:rotate(-45deg);
height:130px;
}
24.6%{
transform:rotate(-45deg);
height:130px;
}
39%{
transform:rotate(-45deg);
height:130px;
}
54%{
transform:rotate(-45deg);
height:130px;
}
68%{
transform:rotate(-45deg);
height:130px;
}
87.7%{
transform:rotate(-45deg);
height:130px;
}
0%{
transform:rotate(-60deg);
height:90px;
}
17.3%{
transform:rotate(-60deg);
height:90px;
}
31.5%{
transform:rotate(-60deg);
height:90px;
}
47%{
transform:rotate(-60deg);
height:90px;
}
62%{
transform:rotate(-60deg);
height:90px;
}
78.4%{
transform:rotate(-60deg);
height:90px;
}
95%{
transform:rotate(-60deg);
height:90px;
}
}
@keyframes tap2{
0%{
transform:rotate(45deg);
height:130px;
}
17.3%{
transform:rotate(45deg);
height:130px;
}
31.5%{
transform:rotate(45deg);
height:130px;
}
47%{
transform:rotate(45deg);
height:130px;
}
62%{
transform:rotate(45deg);
height:130px;
}
78.4%{
transform:rotate(45deg);
height:130px;
}
95%{
transform:rotate(45deg);
height:130px;
}
9.9%{
transform:rotate(60deg);
height:90px;
}
24.6%{
transform:rotate(60deg);
height:90px;
}
39%{
transform:rotate(60deg);
height:90px;
}
54%{
transform:rotate(60deg);
height:90px;
}
68%{
transform:rotate(60deg);
height:90px;
}
87.7%{
transform:rotate(60deg);
height:90px;
}
}
</ style >
</ head >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >How to Sync CSS Animations with HTML5 Audio ?</ h2 >
< h3 >A Drum Sound with CSS Animation</ h3 >
< div id = 'drum' >
< div class = "top" >
< div class = "stick s1" ></ div >
< div class = "stick s2" ></ div >
</ div >
< div class = "mid" ></ div >
< div class = "bottom" >
< div class = "bar b1" ></ div >
< div class = "bar b2" ></ div >
</ div >
< audio src =
controls>
</ audio >
</ div >
< script >
var audio = document.getElementsByTagName('audio')[0];
audio.onplay=()=>{
document.getElementsByClassName('stick')[0].classList.add('active1');
document.getElementsByClassName('stick')[1].classList.add('active2');
console.log("hi")
}
audio.onpause=()=>{
document.getElementsByClassName('stick')[0].classList.remove('active1');
document.getElementsByClassName('stick')[1].classList.remove('active2');
}
</ script >
</ body >
</ html >
|
Output: Play the below video to listen to the audio as well.
Example 2: The following example is of syncing of the railroad crossing signal animation
HTML
<!DOCTYPE html>
< html >
< style >
.signal {
font-size: 1.8em;
height: 400px;
margin: 2em auto 1em;
position: relative;
text-align: center;
width: 500px;
}
.lights,
.lights::after {
background-color: #400000;
border: 0.6em solid #f00;
border-radius: 5%;
height: 2.6em;
left: 0;
position: relative;
width: 2.6em;
}
.lights::after {
content: "";
left: 515%;
right: 0;
top: -0.4em;
width: 2.6em;
height: 2.6em;
position: absolute;
}
.signal.active .lights {
animation: r1 6.38s linear 0s 1;
}
.signal.active .lights::after {
animation: r2 6.38s linear 0s 1;
}
@keyframes r1 {
0.0% {
background-color: #400;
}
1.4% {
background-color: #400;
}
1.5% {
background-color: #FF0;
}
8.8% {
background-color: #400;
}
16.3% {
background-color: #400;
}
16.4% {
background-color: #FF0;
}
23.7% {
background-color: #400;
}
31.7% {
background-color: #400;
}
31.8% {
background-color: #FF0;
}
39.0% {
background-color: #400;
}
46.9% {
background-color: #400;
}
47.0% {
background-color: #FF0;
}
54.4% {
background-color: #400;
}
62.1% {
background-color: #400;
}
62.2% {
background-color: #FF0;
}
69.7% {
background-color: #400;
}
77.5% {
background-color: #400;
}
77.6% {
background-color: #FF0;
}
85.0% {
background-color: #400;
}
92.4% {
background-color: #400;
}
92.5% {
background-color: #FF0;
}
100.0% {
background-color: #400;
}
}
@keyframes r2 {
0.0% {
background-color: #400;
}
8.8% {
background-color: #400;
}
8.9% {
background-color: #FF0;
}
23.7% {
background-color: #400;
}
23.8% {
background-color: #FF0;
}
31.7% {
background-color: #400;
}
39.0% {
background-color: #400;
}
39.1% {
background-color: #FF0;
}
46.9% {
background-color: #400;
}
54.4% {
background-color: #400;
}
54.5% {
background-color: #FF0;
}
62.1% {
background-color: #400;
}
69.7% {
background-color: #400;
}
69.8% {
background-color: #FF0;
}
77.5% {
background-color: #400;
}
85.0% {
background-color: #400;
}
85.1% {
background-color: #FF0;
}
92.4% {
background-color: #400;
}
100.0% {
background-color: #400;
}
}
audio{
margin:40px;
}
</ style >
< body >
< div id = "signal" class = "signal" >
< h1 style = "color:green" >GeekforGeeks</ h1 >
< h3 >How to Sync CSS Animations with HTML5 Audio ?</ h3 >
< div class = "lights" ></ div >
< audio src =
controls preload = "auto" >
Your browser does not support the
< code >audio</ code > element.
</ audio >
</ div >
< script >
var audio = document.getElementsByTagName('audio')[0];
audio.onplay=()=>{
if (!signal.classList.contains('active')) {
signal.classList.add('active');
}
}
audio.onpause=()=>{
signal.classList.remove('active');
}
</ script >
</ body >
</ html >
|
Output: Play the below video to listen to the audio as well:
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read