Add a video background to your landing page to make it more gorgeous
In this blog you will learn how to add a video background to your landing page to make it more gorgeous.
Preview:
Requirements:
- Basic HTML & CSS knowledge
- Basic Javascript(Optional. only required for the navigation toggle effect)
I have already created a video about it on my youtube channel. Check that out for more details.
If you like this video, please like share, and Subscribe to my channel.
source code: https://github.com/thatanjan/video-background-landing-page-yt
Starter code
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="UTF-8" />5 <meta name="viewport" content="width=device-width" />6 <title>Taylor Swift</title>7 <link rel="stylesheet" href="style.css" />8 <link rel="stylesheet" href="nav.css" />9 <link rel="stylesheet" href="content.css" />10 <link rel="stylesheet" href="responsive.css" />11 </head>12 <body>13 This is body1415 <script src="index.js"></script>16 </body>17</html>
Let's add the video and overlay to html.
1<section class="video_container">2 <video src="./media/background.mp4" autoplay loop muted></video>3 <div class="overlay"></div>4</section>
Result:
Explanation:
- A video container to contain the video and overlay.
- Video will be started automatically with a loop. It will also be muted.
Css reset
1* {2 padding: 0;3 margin: 0;4 box-sizing: border-box;5}67body {8 max-width: 100vw;9 overflow-x: hidden;10}
Explanation:
- The entire webpage should not be bigger than the screen width.
- Webpage will not have any horizontal scrolling.
Let's style the video:
1.video_container {2 min-height: 100vh;3 max-height: 100vh;4 overflow: hidden;5 position: relative;6}78.video_container video {9 position: absolute;10 left: 0;11 top: 0;12 width: 100%;13 height: 100%;14 object-fit: cover;15 object-position: center;16}
Result:
Explanation:
- The video container is taking the full width of the screen without any overflow. It is also positioned as
relative
. - Actual video is positioned
absolute
and aligned with the container. It is also taking the full height and width.
If you have confusion with Css position then you can watch this video.
- To make the video fit, we need to use
object-fit
tocover
. - If the user adjusts screen width, the user will always see the center of the video because of
object-position: center;
.
Let's style the overlay.
1:root {2 --primary-red: #70000e;3}45.overlay {6 position: absolute;7 left: 0;8 top: 0;9 width: 100%;10 height: 100%;11 background: var(--primary-red);12 mix-blend-mode: soft-light;13}
Result:
Explanation:
- Overlay element is aligned with the container using position
absolute
. - The background color is stored inside a variable. If you don't know about css variable, then you can check out this blog
- Overlay has been blended with the background using a blend mode. We need to use
mix-blend-mode
property to do so. I will usesoft-light
as value. You can learn aboutmix-blend-mode
from here
And that's how you add a video background to a landing page to make it more gorgeous. If you want to learn how the rest of the project was made please watch the video.
source code: https://github.com/thatanjan/video-background-landing-page-yt
Final result:
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: