HTML5 video compatibility is really high. In fact, it’s supported in all modern browsers (> IE8). The inner workings of video in general can be complicated, especially when you start to dig into multiple formats, tracks, containers, and the works.
Play video in background of HTML5.
Source: https://codepen.io/dudleystorey/pen/knqyK
WCAG general accessibility recommendation is that media such as background video play through only once.
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <video autoplay poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/polina.jpg" id="bgvid" loop> <!-- WCAG general accessibility recommendation is that media such as background video play through only once. Loop turned on for the purposes of illustration; if removed, the end of the video will fade in the same way created by pressing the "Pause" button --> <source src="http://thenewcode.com/assets/videos/polina.webm" type="video/webm"> <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4"> </video> <div id="polina"> <h1>Polina</h1> <p>filmed by Alexander Wagner 2011 <p><a href="http://thenewcode.com/777/Create-Fullscreen-HTML5-Page-Background-Video">original article</a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor.</p> <p>Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nisl.</p> <button>Pause</button> </div> |
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | body { margin: 0; background: #000; } video { position: fixed; top: 50%; left: 50%; min-width: 100%; min-height: 100%; width: auto; height: auto; z-index: -100; transform: translateX(-50%) translateY(-50%); background: url('//demosthenes.info/assets/images/polina.jpg') no-repeat; background-size: cover; transition: 1s opacity; } .stopfade { opacity: .5; } #polina { font-family: Agenda-Light, Agenda Light, Agenda, Arial Narrow, sans-serif; font-weight:100; background: rgba(0,0,0,0.3); color: white; padding: 2rem; width: 33%; margin:2rem; float: right; font-size: 1.2rem; } h1 { font-size: 3rem; text-transform: uppercase; margin-top: 0; letter-spacing: .3rem; } #polina button { display: block; width: 80%; padding: .4rem; border: none; margin: 1rem auto; font-size: 1.3rem; background: rgba(255,255,255,0.23); color: #fff; border-radius: 3px; cursor: pointer; transition: .3s background; } #polina button:hover { background: rgba(0,0,0,0.5); } a { display: inline-block; color: #fff; text-decoration: none; background:rgba(0,0,0,0.5); padding: .5rem; transition: .6s background; } a:hover{ background:rgba(0,0,0,0.9); } @media screen and (max-width: 500px) { div{width:70%;} } @media screen and (max-device-width: 800px) { html { background: url(//demosthenes.info/assets/images/polina.jpg) #000 no-repeat center center fixed; } #bgvid { display: none; } } |
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var vid = document.getElementById("bgvid"); var pauseButton = document.querySelector("#polina button"); function vidFade() { vid.classList.add("stopfade"); } vid.addEventListener('ended', function() { // only functional if "loop" is removed vid.pause(); // to capture IE10 vidFade(); }); pauseButton.addEventListener("click", function() { vid.classList.toggle("stopfade"); if (vid.paused) { vid.play(); pauseButton.innerHTML = "Pause"; } else { vid.pause(); pauseButton.innerHTML = "Paused"; } }) |