How to make a background video component in React

Ibrahim AlRayyan
2 min readSep 15, 2019

--

What’s up, guys?!

In this article I'll show you how to make a video as a background to any content in React js just by using normal HTML and CSS without any other third-party packages. So let’s get started!!

first things first

create new React project, so hit in your preferred CLI next command

npx create-react-app background-video

second step

Create backgroundVideo component. Inside your project specifically in src.

Now let's get our hands dirty!

Inside the backgroundVideo component. First, create a functional(statless) component as follow.

Then add the following jsx code to this function…

You may ask, what are ‘./BackgroundVideo.module.css’;and {classes.Container}??!! 😮😮😮 calm down man 😁!

I’ll tell you now! these stuffs called CSS modules it’s a greate feature in React that makes CSS classes so powerfull and so flexible. Read this article for more info. And don’t worry i’ll attach the enitr repository of the prject including CSS file(module).

If you add the previous code and then hit npm start you’ll see just a video is playing. So, That’s it?! Absolutely not buddy 😀.

Now let's add the content we wanna it to overlay the video, So below video HTML tag add the following div

Off course you can add any other content. But the secret here is CSS style so you should keep the style of Content div as it’s {classes.Content} or to be more specific the style of the positioning where it’s set to absolute. So the absolute property is the secret of this wonderful component.

Now we reach the end to this article. now you can get the entire code from GitHub.

Also, you can see the component live from this link.

Good-by guys. See you in the next article 😀.

--

--

Ibrahim AlRayyan

React Developer, Mobile cross-platform developer, JavaScript Lover