Reputation: 211
I'm trying to center a video within my site but I don't want to use the center tags in HTML because it's kinda obsolete. How do I do this with CSS? Here's my HTML code if it's any help.
<center>
<video width="320" height="240" controls>
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
<source src="video.webm" type="video/webm">
Your browser does not support the video tag.
</video>
</center>
Upvotes: 19
Views: 171644
Reputation: 178
I know this topic was so long but here ya go:
This is for the center of the page
video {
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
And this is for the middle of the line
video {
display: block;
margin: auto;
}
Just for who are looking for this
Upvotes: 9
Reputation: 365
Here are 3 ways to center your video:
video {
display: block;
margin: auto;
}
video {
margin-left: 50vw;
transform: translate(-50%);
}
.container video {
display: flex;
justify-content: center;
}
Upvotes: 19
Reputation: 1159
To center video horizontally add:
display: block;
margin: 0 auto;
Upvotes: 7
Reputation: 163
Try this, have a wrapper div around your video tag. html5 videos and images are treated like text when styling. So a text-align:center will work. You can check the fiddle below.
<div class="video">
<video width="320" height="240" controls>
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
<source src="video.webm" type="video/webm">
Your browser does not support the video tag.
</video>
</div>
Upvotes: 1
Reputation: 1499
Here's an example: http://jsfiddle.net/Cn7SU/
Just add these CSS rules to the element video
:
display: block;
margin: 0 auto;
Add the display: block
property is very important. Otherwise you can't center the element.
Upvotes: 26