Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use video as background for div

I would like to use a video as background in CSS3. I know that there is no background-video property, but is it possible to do this behavior. Using a fullsize video-tag doesn't give the wanted result, cause there is content that need to be displayed over the video.

It need to be non JS. If it is not possible then I need to do changes on my serverside an give as result also a screenshot of the video.

I need the video to replace the colored boxes:

Boxes

The colored boxes are atm just, CSS boxes.

like image 863
Knerd Avatar asked Dec 28 '13 20:12

Knerd


People also ask

How do you put a video in the background of a div?

You can set a video as a background to any HTML element easily thanks to transform CSS property. Note that you can use the transform technique to center vertically and horizontally any HTML element.

Can you use a video as a background HTML?

To use a video background we have to use the HTML 5 <video> element with position absolute inside a container wrapper. Videos are increasingly common in nowadays websites and a great way to create a modern website. With just a bit of CSS you can add a video background to your site in a matter of minutes.

Can you use a video as a background?

As it plays, right-click on the player window and choose Video > Set as Wallpaper. Alternatively, click Video > Set as Wallpaper from the menu.

Can I put video in background-image CSS?

The biggest reason you don't, probably, is that you can't set a movie file as the background-image in CSS. You'll have to do some layout trickery to get it done. Certainly, a full-page background video can be just a bit much.


1 Answers

Pure CSS method

It is possible to center a video inside an element just like a cover sized background-image without JS using the object-fit attribute or CSS Transforms.

2021 answer: object-fit

As pointed in the comments, it is possible to achieve the same result without CSS transform, but using object-fit, which I think it's an even better option for the same result:

.video-container {     height: 300px;     width: 300px;     position: relative; }  .video-container video {   width: 100%;   height: 100%;   position: absolute;   object-fit: cover;   z-index: 0; }  /* Just styling the content of the div, the *magic* in the previous rules */ .video-container .caption {   z-index: 1;   position: relative;   text-align: center;   color: #dc0000;   padding: 10px; }
<div class="video-container">     <video autoplay muted loop>         <source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4" />     </video>     <div class="caption">       <h2>Your caption here</h2>     </div> </div>

Previous answer: CSS Transform

You can set a video as a background to any HTML element easily thanks to transform CSS property.

Note that you can use the transform technique to center vertically and horizontally any HTML element.

.video-container {   height: 300px;   width: 300px;   overflow: hidden;   position: relative; }  .video-container video {   min-width: 100%;   min-height: 100%;   position: absolute;   top: 50%;   left: 50%;   transform: translateX(-50%) translateY(-50%); }  /* Just styling the content of the div, the *magic* in the previous rules */ .video-container .caption {   z-index: 1;   position: relative;   text-align: center;   color: #dc0000;   padding: 10px; }
<div class="video-container">   <video autoplay muted loop>     <source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4" />   </video>   <div class="caption">     <h2>Your caption here</h2>   </div> </div>
like image 95
campsjos Avatar answered Oct 01 '22 13:10

campsjos