Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should <audio> tags go in the <head> tag?

Should HTML5 audio tags go in the head tags, or in the body tags?

like image 521
Erik Humphrey Avatar asked Nov 23 '25 05:11

Erik Humphrey


1 Answers

I think the accepted answer is assuming a bit, namely that the developer wants to display the audio element controls and not just play a sound when another element on the page is clicked for example. Also, the comments on this good question are unhelpful, obviously it's easier to Google something than post a question on Stack Overflow and wait for an answer; in this particular case, if you Google for Audio element in the head tag you will find nothing, not even on W3C or WHATWG.

It's a perfectly valid case to want to play the sound without adding another traversable element to the DOM under the body tag if the controls won't be displayed. They are optional for a reason.

To answer the question correctly, regardless of whether or not you want to display the Audio element, it should go in the body tag. If you don't put it in the body tag, Chrome, for example, will move it there anyways.

So yes, if you want it to be visible put it in the body tag and make sure it is where you want it. If you don't want it to be visible unfortunately you will need to put it in the body tag regardless, and if you don't your DOM tree might not be what you expect when the browser moves it.

As the other answer mentions, if you want the default controls to display then you can add the "controls" attribute to the element, if not you can still create & style your own and make them work using Javascript.

like image 146
Michael Dunn Avatar answered Nov 24 '25 21:11

Michael Dunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!