The broader the spectrum of browsers and operating systems on which we have to design our website, the more complicated things become. So we will have to go to the element
embed
to put background music on our website.
We can also do this with the bgsound element (it is recommended to read the article that explains how to use this tag). Which was the first element that was created to be able to have background music on our website.
In this article we are going to go into depth about how we can use the
embed
and the different attributes available to manage music.
Although, it must be taken into account that this element is still not standard. Although, it is true that it is interpreted by a greater number of browsers and platforms. (as of March 2007)
The most important attribute to know about the element
embed
is
src
. This will allow us to indicate which file contains the audio. The code would be something similar to the following:
<embed src="starwars.mid">
If we put this code as it is, we will get a musical control with its buttons for play, stop, volume control,… and of course, since we want to put music in the background, we will have to hide said control. For this we have the attribute
hidden
, to which we must give a value of true.
<embed src="starwars.mid" hidden="true">
And now, having done this, we have a slight problem. And when we had the music control, we had the play button to force the song to start and now, having hidden the control, we have no way to do it and that is why the song will not start. Therefore, we must use the attribute with a value of true to start the song.
autostart
<embed src="starwars.mid" autostart="true">
And enjoy how to put background music on our website.
Update
This example was valid in older browsers and for version HTML 4.01. From the HTML 5 specification we have the element
audio
that allows us to manage the music within the web pages and the
autoplay
to start music automatically. You can read the article Infinite music on your website with HTML5 to see the new operation.
