Responsive Video Background Player for Vue 2 & 3 ⚡️
If you are looking to play videos in the background, you've found the right Vue package! 😜 (Heads up: No YouTube videos... yet!)
>Prerequisites: Vue 3.2 or newer for version 2.x of this package. For Vue 2, use version 1.x.
Installation in 2 Steps
1: Add with npm 💻
# For Vue 3.x.x
npm install vue-responsive-video-background-player
For Vue 2.x.x
npm install vue-responsive-video-background-player@1x
2a: Import the component
<script setup>
import VideoBackground from 'vue-responsive-video-background-player';
</script>
Or register it globally:
import { createApp } from 'vue';
import VideoBackground from 'vue-responsive-video-background-player';
const app = createApp(App);
app.component('VideoBackground', VideoBackground);
2b: Install as a plugin
import { createApp } from 'vue';
import { Plugin } from 'vue-responsive-video-background-player';
const app = createApp(App);
app.use(Plugin);
The plugin registers the component as VideoBackground. You can use it as or .
(3: Only for Nuxt users)
Nuxt 3 and Nuxt 4
Since version 2.5.0 the component works with server-side rendering. Create a plugin file, for example plugins/video-background.ts:
import { Plugin } from 'vue-responsive-video-background-player';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(Plugin);
});
Then use the tag in any page. The server renders the section, the poster, the overlay and your slot content. The server does not know the window width, so the browser adds the video after hydration.
The component injects its CSS with JavaScript. Until the JavaScript runs, the page shows the server HTML without these styles. Since version 2.6.0, add the CSS file to nuxt.config.ts. Then the server HTML has its styles from the start, and the poster shows before the JavaScript runs:
export default defineNuxtConfig({
css: ['vue-responsive-video-background-player/style.css'],
});
If you prefer to render the component only in the browser, name the plugin file video-background.client.ts and wrap the component in :
<ClientOnly>
<video-background src="/videos/hero.mp4" style="height: 100vh;" />
</ClientOnly>
A .client plugin alone is not enough: the server cannot resolve the component, and Vue reports a hydration mismatch.
Nuxt 2 (package version 1.x)
>Thanks to @skoulix for his instructions:Again this is only for Nuxt.js users. Gridsome users click here. At your nuxt.config.js locate the part where you declare your plugins and import the file. Example:
plugins: [
{
src: '~/plugins/vue-video-background',
ssr: false
}
]
Now the component is globally available and can be used at any .vue file without issues.
TypeScript
Since version 2.5.0 the package contains type declarations for the props, the events, the player methods and the plugin. If you added a declare module 'vue-responsive-video-background-player' file for older versions, you can delete it.
import type { VideoBackgroundSource } from 'vue-responsive-video-background-player';
const sources: VideoBackgroundSource[] = [
{ src: '/videos/mobile.mp4', res: 638, autoplay: true },
];
Usage - (or to make it runnable 🏃♂️)
Easiest version 🔍
<video-background
src="<your-video-path>.mp4"
style="max-height: 400px; height: 100vh;"
>
<h1 style="color: white;">Hello welcome!</h1>
</video-background>
Advanced version 🌐
<video-background
src="<your-default-video-path>.mp4"
poster="/images/mainfoto.jpg"
:sources="[
{src: '<your-tablet-video-path>.mp4', res: 900, autoplay: true},
{src: '<your-mobile-video-path>.mp4', res: 638, autoplay: true, poster: '<your-mobile-background-image-path>.png'}
]"
style="max-height: 400px; height: 100vh;"
overlay="linear-gradient(45deg,#2a4ae430,#fb949e6b)"
>
<h1 style="color: white;">Hallo welcome!</h1>
</video-background>
Demo ⚡️
https://avidofood.github.io/vue-responsive-video-background-player/
Props
This package is for responsive videos depicting different video resolution. Have you ever visited my favorite car company Tesla? Have a look, they use a lot of video background videos and are using different resolutions for each device.
Props values
src(required:true)
>Note for Vite and Nuxt: Put the video in the public folder and use the path from the site root, for example src="/videos/hero.mp4". Or import the file, for example import heroVideo from '@/assets/hero.mp4', and bind it with :src="heroVideo". With Vue CLI, bind it like this: `:src="require(@/assets/video/timelapse.mp4)". Read here why
The component sets the type attribute of the video for .mp4, .m4v, .webm, .ogv, .ogg and .m3u8 files. For other URLs, for example a URL without a file extension, it sets no type, and the browser checks the file itself.
>HLS (.m3u8): Safari, iOS and some other browsers play HLS streams natively. For the other browsers, give the component hls.js with the hls prop. See HLS streams.
- poster
(default:'')
>Note: The same as for src applies. With Vue CLI, bind the image like this: :poster="require(@/assets/img/logo.png)".
- sources
(default:[])
To make it work, sources is an array that contains objects. For example:
[{src: '
To make it work you need at least src, res, autoplay.
poster is optional.
res stand for resolution. This example means that between 0px and 638px of the window's width only the mobile video will be shown. After that your default src.
- autoplay
(default:true)
. But remember to set ref=videobackground to the HTML tag , so that it can work.
overlay (default: '')If you love overlays, then copy the overlay from the advanced example.
muted (default: true)
Browsers block autoplay for most videos with sound. If the browser blocks the video, the poster stays visible and the component emits error.
loop (default: true)
Loops through the video. You can catch the event ended to show only the poster.
preload (default: auto)
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#preload
objectFit (default: cover)
So the video fits perfectly in the container
objectPosition (default: center)
So the video fits exact position in the container
the value is also used as a poster background-position
posterBgSize (default: cover)
So the poster fits perfectly in the container
Using the same values for
objectFit and posterBgSize is recommended
playsWhen (default: canplay)
If some of your users have a slow connection, use canplaythrough. Learn more in video events.
playbackRate (default: 1.0)
The playbackRate property sets the current playback speed of the video. Example but negative values didn't work for me?
transition (default: fade)
You can add your own transition styles here. If you set it to an empty string, the video shows without a transition.
The
fade transition takes one second. For a different duration, give the transition your own name and add the CSS for it:
<video-background src="/videos/hero.mp4" transition="slow-fade" />
<style>
.slow-fade-enter-active,
.slow-fade-leave-active {
transition: opacity 3s;
}
.slow-fade-enter-from,
.slow-fade-leave-to {
opacity: 0;
}
</style>
pauseButton (default: false)
Shows a button that pauses and plays the video. See Pause button and accessibility.
pauseLabel (default: 'Pause background video') and playLabel (default: 'Play background video')
The accessible names of the pause button. Screen readers read them. Set them for other languages.
respectReducedMotion (default: false)
If the user turned on "reduce motion" in the system settings, the component shows only the poster and does not load the video.
pauseWhenHidden (default: false)
Pauses the video while it is off screen or the page is in the background. See Less loading and less work.
lazy (default: false)
Loads the video when the section comes within 200px of the viewport.
keepLargerSource (default: false)
If the window gets smaller, the component keeps a larger video that already loads, and does not load the smaller one (#14).
hls (default: null) and hlsConfig (default: undefined)
The Hls class of hls.js and the options for new Hls(). See HLS streams.
Pause button and accessibility
A background video that plays for more than five seconds needs a way to pause it. This is WCAG 2.2, success criterion 2.2.2 (Level A). It applies to decorative videos too. Set
pause-button for this:
<video-background
src="/videos/hero.mp4"
poster="/images/hero.jpg"
pause-button
respect-reduced-motion
>
<h1>Hello welcome!</h1>
</video-background>
The button:
- is a native