Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you center a video using CSS

Tags:

css

center

video

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> 
like image 431
Rhiannon Avatar asked Jul 07 '13 02:07

Rhiannon


People also ask

How do I center something in CSS?

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.

How do you right align a video in HTML?

Just add float: right and it will gonna work.


2 Answers

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.

like image 132
leoMestizo Avatar answered Sep 28 '22 00:09

leoMestizo


Here are 3 ways to center your video:

1. Using margin

video {
  display: block;
  margin: auto;
}

2. Using transform

video {
  margin-left: 50vw;
  transform: translate(-50%);
}

3. Using a container & flexbox

.container video {
  display: flex;
  justify-content: center;
}
like image 32
khaki Avatar answered Sep 28 '22 00:09

khaki