html - 如何在 html 中的视频顶部放置文本和图标
问题描述
在下面的 html 中插入视频剪辑作为背景的最佳方法是什么?我希望标题、图标和功能显示在视频背景之上。我想将视频包含在 html 中,如下所示(或类似)
<video width="1920" height="1000" autoplay muted loop playsinline id="vid">
<source src="my-video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
document.getElementById('vid').play();
</script>
我尝试过绝对定位、负边距和其他方法,似乎视频元素不允许在其上放置任何东西。任何帮助,将不胜感激。谢谢你。
.hero {
position: relative;
height: 500px;
}
.hero-title {
z-index: 20;
color: white;
text-align: center;
margin-top: 50px;
text-shadow: 0px 0px 7px rgb(0, 0, 0);
}
.hero-btm-area {
margin-bottom: 50px;
z-index: 20;
text-shadow: 0px 0px 7px rgb(0, 0, 0);
}
.hero-icon {
width: 40px;
margin-bottom: 10px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="hero">
<div class="container h-100 d-flex flex-column">
<div class="row">
<div class="col">
<div class="hero-title">
<h1>Hero Title</h1>
</div>
</div>
</div>
<div class="row mt-auto hero-btm-area">
<div class="col text-white text-center border-right border-white">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature One
</div>
<div class="col text-white text-center border-right border-white">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature Two
</div>
<div class="col text-white text-center">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature Three
</div>
</div>
</div>
<div class="hero-bg-img">
</div>
</div>
解决方案
您可以使用名为 z-index 的 CSS 属性。z-index: -1 会将视频放在背景后面。您需要将 position: absolute 放在内容的后面。
Obs:我没看懂视频背景顶部的部分,如果我做错了,请告诉我。
.hero {
position: relative;
height: 500px;
}
.hero-title {
z-index: 20;
color: white;
text-align: center;
margin-top: 50px;
text-shadow: 0px 0px 7px rgb(0, 0, 0);
}
.hero-btm-area {
margin-bottom: 50px;
z-index: 20;
text-shadow: 0px 0px 7px rgb(0, 0, 0);
}
.hero-icon {
width: 40px;
margin-bottom: 10px;
}
#vid {
position: absolute;
top: 0; left: 0;
object-fit: cover;
z-index: -1;
}
<video width="1920" height="1000" autoplay muted loop playsinline id="vid">
<source src="https://www.w3schools.com/tags/movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
document.getElementById('vid').play();
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="hero">
<div class="container h-100 d-flex flex-column">
<div class="row">
<div class="col">
<div class="hero-title">
<h1>Hero Title</h1>
</div>
</div>
</div>
<div class="row mt-auto hero-btm-area">
<div class="col text-white text-center border-right border-white">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature One
</div>
<div class="col text-white text-center border-right border-white">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature Two
</div>
<div class="col text-white text-center">
<div class="hero-icon mx-auto">
**Icon**
</div>
Feature Three
</div>
</div>
</div>
<div class="hero-bg-img">
</div>
</div>
OBS 2:视频很奇怪,因为它只是一个模板,然后你会把你的视频
推荐阅读
- cytoscape - Cytoscape 是否有静默/无人值守卸载命令?
- python - OpenCV python使用阈值搜索颜色
- python - 将第三个属性添加到 Django 模型选择的最佳方法
- bit-manipulation - 为什么412的4s补码(十进制)是321210?
- php - Yii2 设置 MySQL 用户进行首次迁移表创建?
- java - 删除括号,双引号,并可能在 java 中使用 string.replaceall 添加空格
- c# - 将自定义代码与实体框架生成的类分开
- rust - csv 读取的 Rust 错误输入
- python - 无法使用python将json文件打开到搅拌机
- html - 在 html 中格式化 ascii 艺术的问题