How to Add Background Music to a Website Without Annoying Visitors
Background music can make a portfolio, a game page or a wedding site feel complete. It can also send visitors straight back to Google. The difference is almost always in how the music starts, how easy it is to stop and how much it slows the page. This guide shows how to add background music to a website so it adds mood without getting in the way.
Table of Contents
When background music helps and when it hurts
Music works when sound is part of what you offer: a band or DJ site, a game, an interactive story, an online exhibition or an event invitation. Visitors expect audio there, and many will want it.
It hurts on almost everything else. People browse in offices, on trains and late at night with others asleep. If your site is a shop, a blog, a service page or documentation, unexpected sound is a reason to leave. In those cases, offer music as an option the visitor turns on, not something that happens to them.
A simple test: if the page still makes sense with the sound off, the music is decoration. Decoration should never be loud, never be forced and always be one click away from silence.
Why browsers block autoplay with sound
All major browsers stop pages from playing sound before the visitor interacts with them. Chrome, Edge, Firefox and Safari all allow muted autoplay, but audio with sound usually needs a click, tap or key press on the page first. Chrome also uses a Media Engagement score, so a site a person often plays media on may be allowed to autoplay, but you cannot count on that for new visitors.
In practice this means code like <audio autoplay> will often do nothing, and audio.play() called on page load returns a rejected promise. We cover the rules in detail in why audio autoplay is blocked. The short version: design for a start button, and treat autoplay as a bonus that may not happen.
The safe pattern: start on the first click
The pattern that works in every browser is to wait for the visitor to act, then start the music. There are two good ways to do that.
A play button visitors can see
Put a small, clearly labelled music button in a fixed corner or in the header. Use a speaker icon plus text such as “Music on” so its purpose is obvious, and change the label to “Music off” while it plays.
<audio id="bg-music" src="/media/ambient-loop.mp3" loop preload="none"></audio>
<button id="music-toggle" aria-pressed="false">Music on</button>
<script>
const music = document.getElementById('bg-music');
const btn = document.getElementById('music-toggle');
music.volume = 0.4;
btn.addEventListener('click', async () => {
if (music.paused) {
try { await music.play(); } catch (e) { return; }
btn.textContent = 'Music off';
btn.setAttribute('aria-pressed', 'true');
} else {
music.pause();
btn.textContent = 'Music on';
btn.setAttribute('aria-pressed', 'false');
}
});
</script>
Notice preload="none": the file is not downloaded until someone asks for it, so visitors who never press the button pay nothing in page speed. The volume starts at 40%, which is a polite level for background sound. If you need a refresher on the element and its attributes, see our HTML5 audio element guide.
Remembering the visitor’s choice between pages
If a visitor switches music off, it should stay off on the next page. Store the choice in localStorage and read it before showing the button state:
btn.addEventListener('click', () => {
localStorage.setItem('bgMusic', music.paused ? 'off' : 'on');
});
Even when the stored value is “on”, the browser will usually still need a fresh interaction on the new page before sound can play. A common approach is to start the music on the visitor’s first click anywhere on the page when their saved choice is “on”. For music that keeps playing without a gap while people move between pages, you need a different site structure; our guide on keeping audio playing between pages explains the options.
Keep the file small: format, bitrate and looping
Background music is usually a long file that most visitors will hear only part of, so size matters. Some practical rules:
- Use a short loop. A seamless 60 to 90 second loop is often enough. Export it so the end flows into the start, then use the
loopattribute. - Pick a modest bitrate. MP3 at 128 kbps is about 1 MB per minute and sounds fine for ambient music under speech-level volume. Opus at 64 to 96 kbps sounds similar at roughly half the size.
- Offer two sources if you can. List Opus first and MP3 second inside
<source>elements so each browser takes the best format it supports. - Trim silence. Silence at the start makes visitors think the button is broken.
Our guide to optimizing audio files for the web walks through exporting and compressing in more detail.

Accessibility: pause controls and WCAG 1.4.2
Unexpected sound is a real barrier for people who use screen readers, because the music plays over the voice reading the page. The Web Content Accessibility Guidelines cover this in success criterion 1.4.2, Audio Control: if any audio plays automatically for more than three seconds, there must be a way to pause or stop it, or to control its volume separately from the system volume.
The start-on-click pattern above already meets that rule, because nothing plays until the visitor chooses it. Still make sure that:
- the music button can be reached and pressed with the keyboard;
- it has a text label, not only an icon;
- it sits near the top of the page so people find it quickly;
- its state is announced, for example with
aria-pressed.
For more on building controls everyone can use, read our accessible audio player guide.
WordPress options
In WordPress you can place an Audio block, upload the file and turn on Loop in the block settings. The block also has an Autoplay switch, but browsers will usually ignore it for sound, and it can confuse visitors when it does work. Leave it off and let people press play.
If you want a small floating music button on every page, add the HTML and script above to a reusable block or a site-wide footer area. A plugin is only worth it when you need playlists, a player that continues across pages or detailed styling. We compare the built-in tools and plugins in how to add an audio player in WordPress.
Checklist before you publish
- Music does not start until the visitor presses a button or interacts with the page.
- A labelled control to stop the music is visible and keyboard accessible.
- The starting volume is moderate, around 30% to 50%.
- The file uses
preload="none"and is a short, seamless loop. - The visitor’s on or off choice is remembered between pages.
- You have tested on a phone with the ringer on and off, and in Safari as well as Chrome.
- You have the right to use the music. Royalty-free does not always mean free for every use.
Done this way, background music becomes a feature visitors can enjoy instead of a reason to close the tab.
