{"id":4722,"date":"2021-10-27T02:00:00","date_gmt":"2021-10-27T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/background-video-css\/"},"modified":"2026-01-26T17:36:20","modified_gmt":"2026-01-26T16:36:20","slug":"background-video-css","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/background-video-css\/","title":{"rendered":"How to Create a Video Background with CSS [Step-by-Step]"},"content":{"rendered":"\n<p><strong>To use a video background, we have to use the HTML 5 <code>&lt;video><\/code> element with position <code>absolute<\/code> inside a container wrapper.<\/strong><\/p>\n\n\n\n<p>Videos are increasingly common on websites nowadays and are 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. However, note that this technique works only for <code>&lt;video><\/code> elements. This means the video will have to be hosted somewhere. You can also <a href=\"https:\/\/alvarotrigo.com\/blog\/how-to-create-a-youtube-video-background-with-css\/\">use CSS to create a YouTube video background<\/a>, but that&#8217;s not what this tutorial is about.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"finding-a-background-video\">Finding a background video<\/h2>\n\n\n<p>If you don&#8217;t have a video yet, the easiest way to find videos for your site is by using video stock galleries like <a href=\"https:\/\/pixabay.com\/videos\/\" target=\"_blank\" rel=\"noopener nofollow\">Pixabay<\/a> or <a href=\"https:\/\/www.pexels.com\/videos\/\" target=\"_blank\" rel=\"noopener nofollow\">Pexel<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/assets\/imgs\/2021-10-27\/pixabay-video-stock-free.jpeg\" alt=\"Pixabay Free Video Stock\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n\n<p>Once you find the one you want to use, you&#8217;ll have to download it and upload it to your server (or CDN services like <a href=\"https:\/\/cloudinary.com\/\" target=\"_blank\" rel=\"noopener nofollow\">Cloudinary<\/a>).<\/p>\n\n\n\n<p>Once uploaded, we have the URL of the video and we can use it in our <code>&lt;video><\/code> element.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-the-html-for-the-video\">Adding the HTML for the video<\/h2>\n\n\n<p>For the video, we&#8217;ll be using the HTML5 video element. It expects at least a video source file and can be configured by passing multiple HTML attributes.<\/p>\n\n\n\n<p>For our example ,we&#8217;ll be using this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">video<\/span> <span class=\"hljs-attr\">playsinline<\/span> <span class=\"hljs-attr\">autoplay<\/span> <span class=\"hljs-attr\">muted<\/span> <span class=\"hljs-attr\">loop<\/span> <span class=\"hljs-attr\">poster<\/span>=<span class=\"hljs-string\">\"cake.jpg\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-video-url.webm\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/webm\"<\/span>&gt;<\/span>\n    Your browser does not support the video tag.\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">video<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We&#8217;ll talk later more about the <a href=\"#configuring-the-video\">background video configuration<\/a> and the <a href=\"#video-formats-and-browser-compatibility\">video formats<\/a>.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"placing-the-video-in-the-background-with-css\">Placing the video in the background with CSS<\/h2>\n\n\n<p>Usually, in CSS, we set the background image by using the <code>background<\/code> property or the <code>background-image<\/code> one.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">background<\/span>: <span class=\"hljs-selector-tag\">url<\/span>(<span class=\"hljs-selector-tag\">imgs<\/span>\/<span class=\"hljs-selector-tag\">my-background-image<\/span><span class=\"hljs-selector-class\">.png<\/span>);\n<span class=\"hljs-selector-tag\">background-size<\/span>: <span class=\"hljs-selector-tag\">cover<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/background\" target=\"_blank\" rel=\"noopener nofollow\">background property<\/a> only works for images and colors. Not with videos.<\/p>\n\n\n\n<p>Now we have a standard video element, so how do we convert it into a background? We&#8217;ll be using a little trick here. Using the CSS, <code>position: absolute<\/code> we will simulate a background element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">video<\/span> {\n  <span class=\"hljs-attribute\">position<\/span>: absolute;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">0<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This will place our video outside the normal flow of the document and relative to the element we choose within its parents.<\/p>\n\n\n\n<p>So, time to add our video container! We&#8217;ll be using a <code>div<\/code> element as the container for our video. We called it <code>video-wrapper<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"video-wrapper\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">video<\/span> <span class=\"hljs-attr\">playsinline<\/span> <span class=\"hljs-attr\">autoplay<\/span> <span class=\"hljs-attr\">muted<\/span> <span class=\"hljs-attr\">loop<\/span> <span class=\"hljs-attr\">poster<\/span>=<span class=\"hljs-string\">\"cake.jpg\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-css-video-background.webm\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/webm\"<\/span>&gt;<\/span>\n    Your browser does not support the video tag.\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">video<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And here&#8217;s where the magic happens:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.video-wrapper<\/span> {\n  <span class=\"hljs-comment\">\/* Telling our absolute positioned video to \n  be relative to this element *\/<\/span>\n  <span class=\"hljs-attribute\">position<\/span>: relative;\n\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">400px<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">200px<\/span>;\n\n  <span class=\"hljs-comment\">\/* Will not allow the video to overflow the \n  container *\/<\/span>\n  <span class=\"hljs-attribute\">overflow<\/span>: hidden;\n\n  <span class=\"hljs-comment\">\/* Centering the container's content vertically \n  and horizontally *\/<\/span>\n  <span class=\"hljs-attribute\">text-align<\/span>: center;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">align-items<\/span>: center;\n  <span class=\"hljs-attribute\">justify-content<\/span>: center;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I added comments on the CSS code so you can see what each of the properties is for.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"making-the-background-video-cover-the-whole-container\">Making the background video cover the whole container<\/h2>\n\n\n<p>It&#8217;s common for backgrounds to cover the whole container. We usually do this with CSS by using the style <code>background-size: cover<\/code> directly on the background image. But because in this case, our video is not actually a CSS background, we&#8217;ll be using another little trick by using <code>object-fit: cover<\/code> it together with <code>height: 100%<\/code> and <code>width: 100%<\/code> on our <code>video<\/code> element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">video<\/span> {\n  <span class=\"hljs-comment\">\/** Simulationg background-size: cover *\/<\/span>\n  <span class=\"hljs-attribute\">object-fit<\/span>: cover;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n\n  <span class=\"hljs-attribute\">position<\/span>: absolute;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">0<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To illustrate this better, here&#8217;s an example of a video background not covering the whole container:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_gOxWyYw\" data-src=\"\/\/codepen.io\/anon\/embed\/gOxWyYw?height=450&amp;theme-id=1&amp;slug-hash=gOxWyYw&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed gOxWyYw\" title=\"CodePen Embed gOxWyYw\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>And here&#8217;s the same video background covering the whole container:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_QWMvPya\" data-src=\"\/\/codepen.io\/anon\/embed\/QWMvPya?height=450&amp;theme-id=1&amp;slug-hash=QWMvPya&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed QWMvPya\" title=\"CodePen Embed QWMvPya\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-elements-on-top-of-the-video-background\">Adding elements on top of the video background<\/h2>\n\n\n<p>Right now there&#8217;s nothing special we have to do in order to add content on top of our video. And that&#8217;s because we added the property <code>position: relative<\/code> to the video container when setting the video as a background (which is absolute positioned).<\/p>\n\n\n\n<p>Anything that you add after the video element will be positioned on top of it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"video-wrapper\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">video<\/span> <span class=\"hljs-attr\">playsinline<\/span> <span class=\"hljs-attr\">autoplay<\/span> <span class=\"hljs-attr\">muted<\/span> <span class=\"hljs-attr\">loop<\/span> <span class=\"hljs-attr\">poster<\/span>=<span class=\"hljs-string\">\"cake.jpg\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-css-video-background.webm\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/webm\"<\/span>&gt;<\/span>\n    Your browser does not support the video tag.\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">video<\/span>&gt;<\/span>\n\n  <span class=\"hljs-comment\">&lt;!-- This will be positioned on top of our video background --&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"header\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Blueberry Cheesecake<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span>&gt;<\/span>Recipe here<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the final result:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_GRvmzMX\" data-src=\"\/\/codepen.io\/anon\/embed\/GRvmzMX?height=450&amp;theme-id=1&amp;slug-hash=GRvmzMX&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed GRvmzMX\" title=\"CodePen Embed GRvmzMX\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"configuring-the-video\">Configuring the video<\/h2>\n\n\n<p>If you noticed, in our HTML code we&#8217;ve added quite a few things on the <code>video<\/code> element. The most important ones are <code>loop<\/code>, <code>muted<\/code> and <code>autoplay<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">video<\/span> <span class=\"hljs-attr\">playsinline<\/span> <span class=\"hljs-attr\">autoplay<\/span> <span class=\"hljs-attr\">muted<\/span> <span class=\"hljs-attr\">loop<\/span> <span class=\"hljs-attr\">poster<\/span>=<span class=\"hljs-string\">\"cake.jpg\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-css-video-background.webm\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/webm\"<\/span>&gt;<\/span>\n    Your browser does not support the video tag.\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">video<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Videos won&#8217;t be able to autoplay unless we mute them first. And if you are using a video background, using the <code>loop<\/code> option is quite common too, so the video will play endlessly.<\/p>\n\n\n\n<p>The <code>poster<\/code> is the image we want to show while the video is still loading and can&#8217;t yet autoplay.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"video-formats-and-browser-compatibility\">Video formats and browser compatibility<\/h2>\n\n\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/video\" target=\"_blank\" rel=\"noopener nofollow\">HTML 5 video element<\/a> allows the use of multiple source files. This means we can provide our video in different formats for browser compatibility.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">video<\/span> <span class=\"hljs-attr\">playsinline<\/span> <span class=\"hljs-attr\">autoplay<\/span> <span class=\"hljs-attr\">muted<\/span> <span class=\"hljs-attr\">loop<\/span> <span class=\"hljs-attr\">poster<\/span>=<span class=\"hljs-string\">\"my-static-image.jpg\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-video-url.webm\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/webm\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">source<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"my-video-url.mp4\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"video\/mp4\"<\/span>&gt;<\/span>\n    Your browser does not support the video tag.\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">video<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we first try to load the <code>webm<\/code> video, and if the browser can&#8217;t play it, it will fall back to the <code>mp4<\/code> version of it.<\/p>\n\n\n\n<p>Not all browsers can play all kinds of video formats, see <a href=\"https:\/\/caniuse.com\/?search=video%20format\" target=\"_blank\" rel=\"noopener nofollow\">the compatibility table for each video format<\/a>.<\/p>\n\n\n<p><a href=\"https:\/\/caniuse.com\/?search=video%20format\" target=\"_blank\" rel=\"noopener nofollow\"><\/p>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/assets\/imgs\/2021-10-27\/video-formats-compatiblity.jpeg\" alt=\"AVI Format Browser Compatibility\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n<p><\/a><\/p>\n\n\n<p>The <code>mp4<\/code> format is widely accepted by most modern browsers, but using alternatives like <code>webm<\/code> can come in handy to save bandwidth too. So, adding both can be a good combination.<\/p>\n\n\n\n<p>If you wonder how to convert from one format to another, you can use any of the online converters. Just Google &#8220;mp4 to webm&#8221; or &#8220;webm to mp4&#8221; and you&#8217;ll find plenty of converters online.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n<p>There&#8217;s no excuse to not use videos nowadays. Videos on websites are a very powerful design tool that can provide your website with a modern look. They are easy to implement, ligther that GIF animations, and of high quality.<\/p>\n\n\n\n<p>As you could see, you only need a few lines of CSS to set any video as a background. And, if you want to go one step further, you can go full-screen by using components like <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js<\/a>. Check out the <a href=\"https:\/\/alvarotrigo.com\/fullPage\/examples\/videoBackground.html\">video example here<\/a>.<\/p>\n\n\n<p><a href=\"https:\/\/alvarotrigo.com\/fullPage\/\"><br \/>\n    <video width=\"680\" height=\"382\" muted autoplay loop playsinline><source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/wordpress-slider-video-fullpage.webm\" type=\"video\/webm\"\/><source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/wordpress-slider-video-fullpage.mp4\" type=\"video\/mp4\" \/>Your browser does not support the video tag.<br \/>\n    <\/source><\/source><\/video><br \/>\n<\/a><\/p>\n\n<h2 class=\"wp-block-heading\" id=\"related-articles\">Related articles<\/h2>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/how-to-create-a-youtube-video-background-with-css\/\">How to create a Youtube background video with CSS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/how-to-create-a-fullscreen-video-with-css-and-html\/\">How to create full-screen background videos with HTML and CSS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/10-cool-css-animations-to-add-to-your-website\/\">10 CSS cool animations to add to your site<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/css-snap-scroll-horizontally\/\">CSS scroll snap horizontally<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/make-font-bold-in-css-best-ways\/\">Best Ways To Make Font Bold in CSS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We show you in this tutorial how to set a video as a background using just CSS and HTML.<\/p>\n","protected":false},"author":1,"featured_media":4721,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[106],"tags":[9,16],"class_list":["post-4722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","tag-css","tag-design"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=4722"}],"version-history":[{"count":6,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4722\/revisions"}],"predecessor-version":[{"id":19612,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4722\/revisions\/19612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/4721"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=4722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=4722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=4722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}