Reputation: 32716
I'm trying to code up a new site where there's a big splash background as a "hero" image. Like this site has, but as a video where the image is of the lady unloading the car: https://www.simple.com
Resize and you can see how the image always fits within it's parent.
I put some of the code I've been working on in JSBin here (not making much progress): http://jsbin.com/ruqisa/1/edit
I can't seem to get that same behavior with CSS. I want the video to spill outside the parent either width or height wise depending on the dimensions of the screen.
Has anyone done this with pure CSS? JS I could calculate it manually on resize, but I'd really love to do this with just CSS.
EDIT: I'm not looking for the entire page to be a video, just the header background.
Upvotes: 6
Views: 5094
Reputation: 24692
Let's adapt the technique outlined on the blog demosthenes.info.
The parent div:
overflow: hidden
The video:
min-width: 100%
and min-height: 100%
z-index: -1
(can be any negative number)To center the video:
top
and left
are set to 50%
which places the top-left corner in the middle of the parent divtransform: translateX(-50%) translateY(-50%);
Here is a full demo with all your markup.
body {
height: 200vh;
margin: 0;
}
div {
height: 500px;
overflow: hidden;
position: relative;
}
video {
position: absolute;
top: 50%;
left: 50%;
min-height: 100%;
min-width: 100%;
z-index: -1;
transform: translateY(-50%) translateX(-50%);
}
<div>
<video autoplay muted loop>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
</div>
The video:
position: fixed
and will position itself in relation to the browser window (viewport)min-width: 100%
and min-height: 100%
z-index: -1
(can be any negative number)To center the video:
top
and left
are set to 50%
which places the top-left corner in the middle of the screentransform: translateX(-50%) translateY(-50%);
Here is a full demo with all your markup.
body {
margin: 0 auto;
padding: 50px;
color: #FFF;
width: 600px;
}
video.background {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
z-index: -1;
transform: translateX(-50%) translateY(-50%);
}
<h1>I am content</h1>
<p>I am content too!</p>
<video class="background" autoplay muted loop>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
Upvotes: 4
Reputation: 3589
.big-splash video {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background: url(some.jpg) no-repeat;
background-size: cover;
}
DEMO: http://jsbin.com/lunarugibe/1/
Source: Create Fullscreen HTML5 Page Background Video
The video is first positioned absolutely then centered using top
, left
and transform
properties along the X and Y axis. That -webkit- vendor prefix is for safari (Can I use... Support tables for HTML5, CSS3, etc).
The min-height
and min-width
properties force smaller resolution video to occupy the whole page.
The background
property gives a preview image until the video is ready to be played. background: cover
is for this preview image, to make it occupy the full area.
and the z-index
value makes sure that the video is behind all other elements.
Upvotes: 2
Reputation: 2159
Yes, you can achieve this affect with pure CSS pretty easily:
.container {
height: 600px;
width: 100%;
overflow: hidden;
}
.video {
min-height: 100%;
min-width: 100%;
}
If you want to keep it centered or do a full screen video, you will probably need to use some Javascript for calculating window heights.
Upvotes: 0