search

Home  >  Q&A  >  body text

How to embed background video in HTML/CSS?

Hello everyone, I recently encountered a problem. I'm trying to set a video background to my site, but the centered text runs all the way to the bottom, I don't know why, yet the navigation bar is fine. I've done a lot of research but can't find a solution.

For CSS, I made the following settings:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

*{

    margin: 0;

    padding: 0;

    box-sizing: border-box;

    font-family: TESLA Regular;

}

.hero{

    width: 100%;

    height: 100vh;

    background-image: linear-gradient(rgba(12,3,51,0.3));

    position: relative;

    padding: 0 5%;

    display: flex;

    align-items: center;

    justify-content: center;

}

nav{

    width: 100%;

    position: absolute;

    top: 0;

    left: 0;

    padding: 20px 8%;

    display: flex;

    align-items: center;

    justify-content: space-between;

}

nav .logo{

    width: 150px;

}

nav ul li{

    list-style: none;

    display: inline-block;

    margin-left: 40px;

}

nav ul li a{

    text-decoration: none;

    color: #fff;

}

.content{

    text-align: center;

}

.content h1{

    font-size: 160px;

    color: rgb(110, 57, 57);

    font-weight: 600;

    transition: 0.5s;

}

.content h1{

    -webkit-text-stroke:2px rgb(212, 28, 28) ;

    color: transparent;

}

.back-video{

    position: absolute;

    right: 0;

    bottom: 0;

    z-index: -1;

}

@media (min-aspect-ratio: 16/9){

    .back-video{

        width: 100%;

        height: auto;

    }

}

@media (max-aspect-ratio: 16/9) {

    .back-video {

        width: auto;

        height: 100%;

    }

}

 

> 对于HTML,我做了以下设置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div id="hero">

     

        <video autoplay loop muted plays-inline>

            <source src="mylive.mp4" type="video/mp4">

        </video>

        <nav>

            <img src="2086940.jpg" class="logo" alt="">

            <ul>

                <li><a href="#">Home</a></li>

                <li><a href="#">Zone</a></li>

                <li><a href="#">Gaming</a></li>

                <li><a href="#">Blog</a></li>

                <li><a href="#">About us</a></li>

     

            </ul>

        </nav>

        <div class="content">

            <h1>Gaming is not a crime!</h1>

        </div>

</body>

</html>


P粉176203781P粉176203781507 days ago1034

reply all(1)I'll reply

  • P粉314915922

    P粉3149159222023-11-07 11:31:24

    Yes, it is possible to add a video as a background using HTML and CSS.

    1) Add the video tag and video reference in the HTML file as follows:

    1

    2

    3

    <video autoplay muted loop id="myVideo">

      <source src="rain.mp4" type="video/mp4">

    </video>

    2) Set the width and height to 100% to cover the entire window and use position fixed to set the video as background. You may encounter visibility-related issues, but these can be solved using z-index and background overlay effects.

    1

    2

    3

    4

    5

    6

    7

    #myVideo {

      position: fixed;

      right: 0;

      bottom: 0;

      min-width: 100%;

      min-height: 100%;

    }

    reply
    0
  • Cancelreply