You can add background music using an audio tag, embed tag, and iframe tag. In this article, I will tell you, which of these three tags to use, which tag is best to add background music and how to add background music in HTML from YouTube. You can add audio to HTML in the same way you add a video to HTML
There is no separate tag for adding background music. So, if you add your audio using the <audio>
or <iframe>
tag and then add the "hidden"
attribute, it will appear as background music. For example, <embed hidden src="feelthebgm.mp3" ></embed>.
If you are using an audio tag then you don't need to add a "hidden" attribute. Because if you use the audio tag, the audio controls will be hidden by default, you only know the audio's controls if you add an attribute called "controls".
You can add background music to your HTML using these 3 easy methods. And the other is 2 methods for how to add bg-music from youtube are given below
The below code shows how to add background music using an audio tag.
<html>
<head>
<title>Background music</title>
</head>
<body>
<!--Single background music-->
<audio src="your audio file path.mp3" loop autoplay ></audio>
<!--Multiple background music-->
<audio loop autoplay >
<source src="your audio file path.mp3" type="audio/mp3">
<source src="your audio file path.mp3" type="audio/mp3">
</audio>
</body>
</html>
You can even add background music from youtube using an iframe tag, Let's see how to do it next.
<html>
<head>
<title>Background music</title>
</style>
</head>
<body>
<iframe hidden src="your audio file path.mp3"
allow="autoplay" ></iframe>
</body>
</html>
allow="autoplay"
attribute<embed src="your audio file path.mp3" width="0" autostart="true" ></embed>
"autostart"
attribute and give it a value of "true"
.If you want to add BGM from youtube you must have an internet connection, Because in both these methods your YouTube video is running in the background. I don't recommend adding BGM from youtube, because your site will be slow, BGM will take some time to play and you will need internet. So if you download audio from youtube and add BGM using an audio tag or iframe tag your site will be speed and the internet is not required in this method.
If you want to add background music from youtube, follow these simple steps:
?autoplay=1
" at the end of the youtube link in the "SRC" attribute in the iframe tag given by them, like the below code. Only if you do this correctly will your music play automatically.
<iframe hidden src="https://www.youtube.com/embed/q0fLF0XHGTc?autoplay=1" >
</iframe>
Click here to read more