Horizontally align an html5 audio element in a div

早过忘川 提交于 2019-12-12 15:14:43

问题


I got this simple div:

#div-2 {
    margin-left: auto;
    margin-right: auto;
}

and an html5 audio element in the body of my page:

<div ID="div-2">
        <audio id="a1" src="../../audio/sound_files/mystuff.ogg"></audio>
</div>

I would like the audio element (that is, the audio controls displayed to the user) to be horizontally centered in the div. Please note I can't specify a width attribute for the audio controls or the audio elements (I tried, did nothing), and as a general rule I don't know the audio controls width (in pixels) between browsers (so I can't adjust the width attribute in the div).

Is there a way to do this either in CSS or, alternatively, in Javascript? It would be a bonus if someone could point me to a relevant resource on the web.


回答1:


#div-2 {
    display: table;
    margin: 0 auto;
}

Also if you are using the browser audio controls then remember to add controls to the tag, like so:

<audio id="a1" src="../../audio/sound_files/mystuff.ogg" controls></audio>



回答2:


<div align="center" id="player">
<audio controls>
<source src="name.mp3" type="audio/mpeg">
Unsupported browser!
</audio>
</div>


来源:https://stackoverflow.com/questions/5232480/horizontally-align-an-html5-audio-element-in-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!