如何在HTML/CSS中嵌入背景视频?
P粉176203781
P粉176203781 2023-11-06 13:59:13
0
1
863

大家好,我最近遇到了一个问题。我尝试给我的网站设置一个视频背景,但是居中的文本一直跑到底部,我不知道为什么,然而导航栏却没问题。我做了很多研究,但是找不到解决办法。

对于CSS,我做了以下设置:


*{
    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,我做了以下设置
<!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粉176203781
P粉176203781

全部回复(1)
P粉314915922

是的,可以使用HTML和CSS将视频作为背景添加。

1)在HTML文件中添加视频标签和视频引用,如下所示:

<video autoplay muted loop id="myVideo">
  <source src="rain.mp4" type="video/mp4">
</video>

2)将宽度和高度设置为100%,以覆盖整个窗口,并使用position fixed将视频设置为背景。可能会遇到与可见性相关的问题,但可以使用z-index和背景叠加效果来解决。

#myVideo {
  position: fixed;
  right: 0;
  bottom: 0;
  min-width: 100%; 
  min-height: 100%;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!