This code snippet helps you to create a full-screen video background with text overlay. It positions a video element covering the entire viewport with text overlaid on it. This is useful for showcasing videos as backgrounds with accompanying text. The code utilizes CSS for layout and positioning, ensuring the video covers the entire screen regardless of size.
You can use this code on websites to create captivating landing pages or homepage backgrounds. It helps grab attention with dynamic video backdrops while conveying essential text information.
How to Create Full-Screen Video Background With Text Overlay
1. First of all, load the Google Fonts by adding the following CDN links into the head tag of your HTML document. (Optional)
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Raleway:wght@400;900&display=swap" rel="stylesheet">
2. Create the structure for your header using the <header>
tag. Inside it, add a <video>
tag with the src
attribute pointing to your video file. Include a <div>
for your header text and a separate <div>
for descriptions or additional text. Modify the text in the description <div>
to suit your message.
<header class="video-header"> <video src="https://drive.google.com/uc?id=1QmPm1EdHE0yLepVKu8FqqpPxmNKSxwnC" autoplay loop playsinline muted></video> <div class="header-text"> <h1> Exploring <span>The Sea</span> </h1> </div> <div class="description"> <p>This is an example of a full screen video being shown as a background at any screen size. This is accomplished with giving video absolute positioning, full viewport width and height, and using 'object-fit: cover;' </p> <p class="credit">Video credit: Taryn Elliot on <a href="https://www.pexels.com/video/colorful-fish-swimming-among-seaweed-5548030/">pexels.com.</a></p> </div> </header>
3. Style the video background section using the following CSS styles. You can customize the CSS code according to your needs.
html, body { font-family: 'Raleway', sans-serif; background-color: teal; } video { width: 100dvw; min-height: 100dvh; position: absolute; top: 0; left: 0; object-fit: cover; } .header-text { position: absolute; color: white; display: flex; flex-direction: column; justify-content: center; text-align: center; transform: translateY(-5rem); top: 0; bottom: 0; left: 0; right: 0; } .header-text h1 { font-weight: 400; font-size: 1.75rem; text-transform: uppercase; line-height: 1; } .header-text span { display: block; font-weight: 900; font-size: 6rem; } .description { position: absolute; bottom: 0; left: 0; right: 0; margin: auto; color: white; background-color: hsl(181deg 50% 20% / 70%); padding: 2rem; } .description p { max-width: 40rem; margin-inline: auto; line-height: 1.6; } .credit { font-size: .75rem; } .credit a { color: white; text-underline-offset: 2px; }
That’s all! hopefully, you have successfully created a Full-Screen Video Background using HTML and CSS. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.