Michael97 Michael97 - 5 months ago 19
CSS Question

background video css (not fullscreen)

Good day to everyone!
I have a question about background-video,I have it attached to the whole page,but need it just to be embedded at the top. Can anyone tell me what's the problem?
Huge thanks!
HTML:

<video autoplay loop poster="video-bg.jpg" id="bgvid">
<source src="video-bg.webm" type="video/webm">
<source src="video-bg.mp4" type="video/mp4">
</video>


CSS:

video#bgvid {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background: url(video-bg.jpg) no-repeat;
background-size: cover;
}
@media screen and (max-device-width: 800px) {
html {
background: url(video-bg.jpg) #000 no-repeat center center fixed;
}
#bgvid {
display: none;
}
}

Answer

First remove min-width/height: 100%;
Than replace transform: translateX(-50%) translateY(-50%); with transform: translateX(-50%); to move element only horizontally
Finally change top: 50%; to top: 0;

If you wish to scroll also video when scrolling page replace position: fixed with position: absolute;

#bgvid {
  background: none #aaa !important;
  text-align: center;
  width: 300px;
  height: 150px;
}
#bgvid {
    position: fixed;
    top: 0;
    left: 50%;
    /*min-width: 100%;*/
    /*min-height: 100%;*/
    -ms-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    z-index: -100;
    background: url(video-bg.jpg) no-repeat;
    background-size: cover; 
}
@media screen and (max-device-width: 800px) {
    html {
         background: url(video-bg.jpg) #000 no-repeat center center fixed;
    }
    #bgvid {
        display: none;
    }
}
<div id="bgvid">VIDEO HERE</div>