HTML Background Music Code

This page contains the HTML code for setting background music on your webpage.

The following code sets background music. The autoplay attribute is what makes it play automatically as soon as the page loads. This code assumes that there is a music file at the specified location.

Here's the code:

See/hear the result. (Background music will start playing as soon as the page loads).

Add Audio Controls

To show the audio controls, just add the controls attribute. You can also remove the autoplay attribute so that the music doesn't start playing as soon as the page loads.

The above HTML code results in this:

For usability reasons, it is generally not recommended to add background music to a website. It can be very annoying for users. If you do decide to add background music, make sure that there is a way for the user to turn it off (by providing audio controls like the ones in the example above).

Also, many browsers will block autoplaying audio by default.