Reputation: 65
I have been trying to centre/center my HTML audio player to be in the centre/center of my GitHub Pages static website. I have tried this in HTML and CSS. I have deleted my cache a few times to make sure that isn't the root of my problems. Chrome Browser / Mac OS 10.14.
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
Bottom of code: <source src="etc.mp3" type="audio/mpeg" class="center"/>
Top of code (in style tags) :
#audio {
text-align: center;
}
Bottom of code: <source id="audio" src="etc.mp3" type="audio/mpeg"/>
Top of code (in style tags):
#audio {
text-align: center;
}
Bottom of code:
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
#audio {
vertical-align: center;
}
Bottom of code:
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
#audio {
justify-content: center;
}
Bottom of code:
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
#audio {
align-self: center;
}
Bottom of code:
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
#audio {
align-self: center;
display: inline-block;
}
Bottom of code:
<div class="audio">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
<div style="margin: 0 auto; display: table;">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Top of code (in style tags):
.audio-container {
display: flex;
justify-content: center;
align-items: center
}
Bottom of code:
<div class='audio-container'>
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
From Sandip Nirmal's answer in this thread - also attempt #9 after deleting cache
Top of code (in style tags):
.audio-container {
display: flex;
justify-content: center;
align-items: center
}
Bottom of code:
<div class="audio-container">
<audio controls>
<source src="etc.mp3" type="audio/mpeg"/>
</audio>
</div>
Upvotes: 3
Views: 5402
Reputation: 1363
Try with the css like this:
.audio{
width:100%;
}
.audio audio{
margin: 0 auto;
display: table;
}
The "display: table;" is what does the trick.
Upvotes: 1
Reputation: 471
First of it should be
audio{
:
} or `.audio{...}`
Since #audio target IDs meanwhile audio
targets elements and .audio
targets class with the value 'audio'
This is how I would have done it
audio{
width: 100%;
}
.audio{
width:500px /* relative units are actually better so consider using rem,em or %*/
margin: 0 auto;
}
Upvotes: 0