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>
You do this by setting the display property to “flex.” Then, define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
Just add float: right and it will gonna work.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With