How to sync css animations with HTML5 audio ?
Last Updated :
27 Jul, 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=
"https://media.geeksforgeeks.org/wp-content/uploads/20221030123223/Bongos.wav"
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%);
}
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);
}
</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 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;
}
}
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=
"https://media.geeksforgeeks.org/wp-content/uploads/20221030123223/Bongos.wav"
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=
"https://media.geeksforgeeks.org/wp-content/uploads/20221130211219/railroad-crossing-signal.wav"
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
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
11 min read
Basics
HTML IntroductionHTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam
6 min read
HTML EditorsAn HTML Editor is a software application designed to help users create and modify HTML code. It often includes features like syntax highlighting, tag completion, and error detection, which facilitate the coding process. There are two main types of HTML editors: Text-Based Editors - Allow direct codi
5 min read
HTML BasicsHTML (HyperText Markup Language) is the standard markup language used to create and structure web pages. It defines the layout of a webpage using elements and tags, allowing for the display of text, images, links, and multimedia content. As the foundation of nearly all websites, HTML is used in over
7 min read
HTML Structure & Elements
HTML ElementsAn HTML Element consists of a start tag, content, and an end tag, which together define the element's structure and functionality. Elements are the basic building blocks of a webpage and can represent different types of content, such as text, links, images, or headings.For example, the <p> ele
5 min read
HTML AttributesHTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behavior, appearance, or functionality in a variety of ways. Each attribute has a name and a value
8 min read
HTML HeadingsHTML headings are used to define the titles and subtitles of sections on a webpage. They help organize the content and create a structure that is easy to navigate.Proper use of headings enhances readability by organizing content into clear sections.Search engines utilize headings to understand page
4 min read
HTML ParagraphsA paragraph in HTML is simply a block of text enclosed within the <p> tag. The <p> tag helps divide content into manageable, readable sections. Itâs the go-to element for wrapping text in a web page that is meant to be displayed as a distinct paragraph.Syntax:<p> Some Content...
5 min read
HTML Text FormattingHTML text formatting refers to the use of specific HTML tags to modify the appearance and structure of text on a webpage. It allows you to style text in different ways, such as making it bold, italic, underlined, highlighted, or struck-through. Table of ContentCategories of HTML Text FormattingLogic
4 min read
HTML Block and Inline ElementsHTML elements are either block-level, which structure the layout and span full width (like <div> or <p>), or inline, which styles content within blocks without breaking the flow (like <span> or <a>). This distinction covers 80â90% of common HTML usage.Inline and Block Level E
3 min read
HTML CharsetsHTML charsets define how characters are represented in a web document. The character encoding ensures that text appears correctly across different devices and platforms.The <meta> tag's charset attribute is used to specify which character encoding the HTML document uses. By setting the charset
4 min read
HTML List
HTML Visuals & Media
HTML ColorsHTML Colors can be applied to text, backgrounds, borders, links, forms, tables, etc. This article provides an in-depth look at how colors can be applied to various elements such as text, backgrounds, borders, links, forms, and tables in HTML. We will explore different color formats including hexadec
11 min read
HTML Links HyperlinksHTML Links, also known as hyperlinks, are defined by the <a> tag in HTML, which stands for "anchor." These links are essential for navigating between web pages and directing users to different sites, documents, or sections within the same page. The basic attributes of the <a> tag include
3 min read
HTML ImagesThe HTML <img> tag is used to embed an image in web pages by linking them. It creates a placeholder for the image, defined by attributes like src, width, height, and alt, and does not require a closing tag.There are two ways to insert the images into a webpage:By providing a full path or addre
7 min read
HTML FaviconA favicon (short for "favorite icon") is a small yet important image that appears next to your websiteâs title in the browser tab. Also known as a tab icon or bookmark icon, it helps users quickly identify and return to your site. Studies show that over 85% of users rely on visual cues like favicons
4 min read
HTML VideoThe <video> element in HTML is used to show video content on web pages. It supports various video formats, including MP4, WebM, and Ogg. It is introduced in HTML5.Scroll down to the End, there is a Tutorial Video which is a live example of the Video Element displaying on this webpage.Syntax:
4 min read
HTML Layout & Design
HTML TablesHTML (HyperText Markup Language) is the standard markup language used to create and structure web pages. It defines the layout of a webpage using elements and tags, allowing for the display of text, images, links, and multimedia content. As the foundation of nearly all websites, HTML is used in over
10 min read
HTML IframesAn iframe, or Inline Frame, is an HTML element represented by the <iframe> tag. It functions as a 'window' on your webpage through which visitors can view and interact with another webpage from a different source.Iframes are used for various purposes like:Embedding Multimedia: Easily integrate
4 min read
HTML LayoutHTML layouts are a technique used to divide a web page into multiple sections, making it easier to apply styles, organize content, and manage operations efficiently. This division improves readability, accessibility, and overall user experience.HTML layout is achieved through elements like <heade
4 min read
HTML File PathsHTML file paths specify the location of files or resources that a webpage needs to access, such as images, videos, scripts, or other HTML documents. These paths tell the web browser where to find the files required to display the content correctly or to execute scripts as intended. To insert a file
3 min read
HTML Projects& Advanced Topics
HTML FormsHTML forms, defined using the <form> Tags are essential for collecting user input on web pages. They incorporate a variety of interactive controls such as text fields, numeric inputs, email fields, password fields, checkboxes, radio buttons, and submit buttons. Over 85% of websites rely on for
5 min read
HTML5 SemanticsHTML5 introduced a range of semantic elements that clearly describe their purpose in human and machine-readable language. Unlike non-semantic elements, which provide no information about their content, semantic elements clearly define their content. For instance, <form>, <table>, and
6 min read
HTML URL EncodingA Uniform Resource Locator (URL) is simply the address of a website to access the website content. Web browsers retrieve pages from web servers using a URL (Uniform Resource Locator).What is URL Encoding?URL Encoding is the process of converting the URL into a valid format that is accepted by web br
4 min read
HTML Responsive Web DesignHTML Responsive Web Design is a modern approach to web development that ensures web pages look great on any device, from phones and tablets to desktop computers. It focuses on making HTML elements automatically adjustâresizing, hiding, or repositioning based on the screen size. This approach guarant
11 min read
Top 10 Projects For Beginners To Practice HTML and CSS SkillsLearning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read
HTML Tutorial References
HTML Tags - A to Z ListHTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl
15+ min read
HTML Attributes Complete ReferenceHTML attributes are special words placed inside the opening tag of an HTML element to define its characteristics. Each attribute has two parts:Attribute nameAttribute value (separated by an equal sign = and enclosed in double quotes " ").Syntax:<tag_name attribute_name="value"> Contents...
8 min read
HTML Global AttributesHTML attributes provide additional information about an element and define its properties. Global attributes are special types of attributes that can be used with any HTML element, offering common functionality to enhance behavior and presentation.Global attributes can be applied to any HTML element
5 min read
HTML5 Complete ReferenceHTML (HyperText Markup Language) is the standard language used to create and design web pages. It defines the structure and layout of a webpage using a series of elements and tags.HTML5 is the latest version of HTML, bringing significant improvements for building modern web applications.It introduce
8 min read
HTML5 MathML Complete ReferenceThe MathML comes in HTML5. The current MathML version is 3. It was introduced in the year 2015. MathML stands for Mathematics Markup Language. It is used to represent mathematical equations or expressions in web browsers, like other HTML elements. MathML is used to describe mathematics as a basis fo
3 min read
HTML DOM Complete ReferenceHTML DOM (Document Object Model) is a programming interface that represents the elements of an HTML document in a tree-like structure.Allows developers to change content and layout using JavaScript.Enables dynamic updates and user interaction on websites.Facilitates the addition, removal, or modific
15+ min read
HTML DOM Audio/Video Complete ReferenceHTML DOM Audio/Video properties and methods allow developers to control audio and video elements programmatically.These controls include playing, pausing, stopping, and adjusting volume.DOM methods enable dynamic interaction and customization of media elements.They enhance the user experience by off
2 min read
SVG Element Complete ReferenceSVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. It is a type of vector graphic that may be scaled up or down. Elements are the core things that is required to work with SVGs.List of SVG Elements:SVG ElementsDescription<a>The <a>
5 min read
SVG Attribute Complete ReferenceSVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. It is a type of vector graphic that may be scaled up or down. Attributes are the things that give the SVG shape, color, etc it will make the SVGs as attractive as you need.List of SVG Attribut
8 min read
SVG Property Complete ReferenceSVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. It is a type of vector graphic that may be scaled up or down. Properties are used for passing the value to the element, there are lots of properties that can be used in SVG elements. table{ di
7 min read
HTML Canvas Complete ReferenceThe HTML âcanvasâ element is used to draw graphics via JavaScript. The âcanvasâ element is only a container for graphics. One must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.Example: This example shows the basic
4 min read