我在网站上添加了一个小视频背景,并在顶部添加了一些文字。但是,我无法将文本置于视频中间。有人可以帮忙吗?请参阅下面的代码。
.header-unit {
height: 80px;
border: 2px solid #000;
border-right: none;
border-left: none;
margin-top: 50px;
position: relative;
padding: 20px;
}
#video-container {
position: absolute;
}
#video-container {
top: 0%;
left: 0%;
height: 100%;
width: 100%;
overflow: hidden;
}
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: white;
}
video {
position: relative;
z-index: 0;
}
video.fillWidth {
width: 100%;
}
<div class="header-unit">
<div id="video-container">
<p>Next Event: December 10th</p>
<video autoplay loop class="fillWidth">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4"/>
</video>
</div>
</div>
答案 0 :(得分:0)
您只需在此规则中添加2行:
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: white;
width:100%;
text-align:center
}
如果您给绝对定位的段落提供100%的宽度,则可以轻松地使用text-align: center
.header-unit {
height: 80px;
border: 2px solid #000;
border-right: none;
border-left: none;
margin-top: 50px;
position: relative;
padding: 20px;
}
#video-container {
position: absolute;
}
#video-container {
top:0%;
left:0%;
height:100%;
width:100%;
overflow: hidden;
}
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: white;
width:100%;
text-align:center
}
video {
position: relative;
z-index: 0;
}
video.fillWidth {
width: 100%;
}
<div class="header-unit">
<div id="video-container">
<p>Next Event: December 10th</p>
<video autoplay loop class="fillWidth">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4"/>
</video>
</div>
</div>
答案 1 :(得分:0)
left
和包含元素的偏移位置
right
属性值0
text-align: center
transform: translateY(50%)
示例:强>
video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: black; /* for the sake of demonstration */
/* additional */
left: 0;
right: 0;
margin: auto;
text-align: center;
transform: translateY(50%);
}
代码段示范:
.header-unit {
height: 80px;
border: 2px solid #000;
border-right: none;
border-left: none;
margin-top: 50px;
position: relative;
padding: 20px;
}
#video-container {
position: absolute;
}
#video-container {
top: 0%;
left: 0%;
height: 100%;
width: 100%;
overflow: hidden;
}
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: black; /* for the sake of demonstration */
/* additional */
left: 0;
right: 0;
margin: auto;
text-align: center;
transform: translateY(50%);
}
video {
position: relative;
z-index: 0;
}
video.fillWidth {
width: 100%;
}
&#13;
<div class="header-unit">
<div id="video-container">
<p>Next Event: December 10th</p>
<video autoplay loop class="fillWidth">
<source src="https://coverr.co/s3/mp4/Into_the_Wild.mp4"/>
</video>
</div>
</div>
&#13;
left
和包含元素的偏移位置
right
属性值0
text-align: center
p
的{{1}}和top
属性值将嵌套的right
元素的高度定义为垂直居中示例:强>
0
代码段示范:
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: black; /* for the sake of demonstration */
/* additional */
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
height: 58px;
}
&#13;
.header-unit {
height: 80px;
border: 2px solid #000;
border-right: none;
border-left: none;
margin-top: 50px;
position: relative;
padding: 20px;
}
#video-container {
position: absolute;
}
#video-container {
top: 0%;
left: 0%;
height: 100%;
width: 100%;
overflow: hidden;
}
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: black; /* for the sake of demonstration */
/* additional */
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
height: 58px;
}
video {
position: relative;
z-index: 0;
}
video.fillWidth {
width: 100%;
}
&#13;
<div class="header-unit">
<div id="video-container">
<p>Next Event: December 10th</p>
<video autoplay loop class="fillWidth">
<source src="https://coverr.co/s3/mp4/Into_the_Wild.mp4"/>
</video>
</div>
</div>
display: flex
样式声明为包含元素(有关详细信息,请参阅示例)flex-box
的{{1}}属性值以进行垂直居中示例:强>
margin
代码段示范:
p
&#13;
video-container {
top: 0%;
left: 0%;
height: 100%;
width: 100%;
overflow: hidden;
/* additional */
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
&#13;
答案 2 :(得分:0)
这应该有助于你正在寻找什么。为了方便,我添加了背景和边框;您可以在使用中删除它们。
.header-unit {
height: 200px;
border: 2px solid #000;
margin-top: 50px;
position: relative;
padding: 20px;
background: #CCC;
}
#video-container {
height:100%;
width:100%;
overflow: hidden;
position: relative;
border: 1px solid blue;
}
#video-container p {
position: absolute;
z-index: 1;
font-size: 50px;
color: white;
margin: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid red;
width: 85%;
text-align: center;
}
video {
position: absolute;
z-index: 0;
border: 1px solid green;
}
video.fillWidth {
width: 100%;
}
<div class="header-unit">
<div id="video-container">
<p>Next Event: December 10th</p>
<video autoplay loop class="fillWidth">
<source src="Forest.mov" type="video/mp4"/>
</video>
</div>
</div>
答案 3 :(得分:0)
只需使用以下代码替换#video-container
和#video-container p
样式:
#video-container {
top:0%;
left:0%;
height:100%;
width:100%;
overflow: hidden;
text-align: center;
}
#video-container p {
width:auto;
margin: auto;
z-index: 1;
font-size: 50px;
color: white;
line-height: 80px;
}
编辑:试试这个:
#video-container p {
position: absolute; //set this to be able to place it anywhere
top: 5%; //calculate this value based on your video block's height
left: 5%; //calculate this value based on your video block's width
z-index: 1;
font-size: 50px;
color: white;
}