html - 将段落文本放置在视频背景上的 h1 下方
问题描述
我无法将段落文本像 H1 一样居中放置在视频背景上。我不确定我应该使用什么。我尝试margin
并padding
进行了调整,但它们使视频尺寸变大,这不是我想要的。
/* Background Video */
#background-video>video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
z-index: 1;
}
#home {
position: relative;
}
/* Overlay the Video for Text */
#overlay {
position: relative;
z-index: 2;
}
/* Video Text */
h1 {
color: hsl(0, 0%, 100%);
height: 40vw;
display: flex;
justify-content: center;
align-items: center;
}
<main>
<section id='home'>
<div id='overlay'>
<h1>My Name</h1>
<p id="Info">Web Developer</p>
</div>
<section id='background-video'>
<video autoplay loop muted>
<source src="Video 3.mp4">
</video>
</section>
</section>
</main>
解决方案
这是你可以尝试的:)
使用position:absolute
和transform:translate();
属性。100vh
并设置section
元素的高度id="home"
CODEPEN 链接: https ://codepen.io/emmeiWhite/pen/JjNVZWe
*{
margin:0;
padding:0;
box-sizing:border-box;
}
#home{
height:100vh;
width:100vw;
position:relative;
}
#background-video > video
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
z-index: 1;
background:blue;
}
/* Overlay the Video for Text */
#overlay
{
position: absolute;
z-index: 2;
top:50%;
left:50%;
transform:translate(-50%,-50%);
}
/* Video Text */
<html>
<body>
<main>
<section id='home'>
<div id='overlay'>
<h1>My Name</h1>
<p id="Info">Web Developer</p>
</div>
<section id='background-video'>
<video autoplay loop muted>
<source src="Video 3.mp4">
</video>
</section>
</section>
</main>
</body>
</html>
推荐阅读
- desktop - 如何在 Google 远程桌面上重新登录我的 Ubuntu 远程桌面?
- javascript - 获取多个东西
- julia - Julia 中的直接求和、二元积(Kronecker 积)和 Hadamard 积
- html - 定位 Div 和 Video
- microsoft-graph-api - Microsoft Graph GET /subscribedSku 缺少 SKU
- javascript - 如何使用turfjs通过东北绘制弧线
- database-design - 如何在图形数据库(海王星)中为社交媒体应用相关用例进行数据建模?
- typeorm - 如何添加引用实体的字段?
- rust - Rust std::fmt 没有像预期的那样工作
- ios - iOS:重新安装后删除缓存中的所有数据