前端网页设置视频背景

  • Post author:
  • Post category:其他


视频设置自动播放、循环播放、静音

一定要设置静音不设置静音的话不会自动播放

video元素设置width: 100%; height: auto;

如果height设置100%的话,定位之后会看不到

<!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>视频背景</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .box {
        width: 100%;
        height: 100%;
        position: relative;
      }
      .box > video {
        width: 100%;
        height: auto;
        position: absolute;
        top: 0;
        left: 0;
      }
      .content {
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: skyblue;
        z-index: 1;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <video muted autoplay loop>
        <source src="./video/background.mp4" />
      </video>
      <div class="content">这是一段话</div>
    </div>
  </body>
</html>

效果:



版权声明:本文为qq_43235503原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。