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:
The colored boxes are atm just, CSS boxes.
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.
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.
As it plays, right-click on the player window and choose Video > Set as Wallpaper. Alternatively, click Video > Set as Wallpaper from the menu.
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.
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
.
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>
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>
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