如何在响应视频上垂直居中文本

时间:2016-05-07 22:23:39

标签: javascript jquery html css html5-video

出于某种原因,这让我感到很难,并且无法找到可靠的答案。

我在后台播放视频,全屏播放并响应屏幕尺寸。我在视频上有标题/文字。但我不能为我的生活弄清楚如何以响应的方式将此文本垂直居中到视频中!谢谢你的帮助!

Codepen:

http://codepen.io/149203/pen/VagPxe

HTML:

<body>
  <div class="header-container">
    <div class="video-container">
      <video preload="true" autoplay="autoplay" loop="loop" volume="0">
            <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" type="video/mp4">
        </video>
    </div> <!-- video-container -->
    <h3>Centered Title</h3>
    <h6>This should be vertically and horizontally centered</h6>
  </div> <!-- header-container -->
</body>

的CSS:

.header-container {
  width: 100%;
  height: 100vh;
  position: relative;
  padding: 1px;
}

.video-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

video {
  position: absolute;
  z-index: -100;
  width: 100%;
}

h3,
h6 {
  text-align: center;
  color: white;
}

拉进这个css:

https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/cyborg/bootstrap.min.css

3 个答案:

答案 0 :(得分:3)

display: table展示包装,将子元素更改为display: table-cellvertiacl-align: middle. http://codepen.io/anon/pen/ZWwLPP

答案 1 :(得分:2)

我发现并分叉了这个CodePen。这是我发现的最佳解决方案。 (使用Bootstrap。)

http://codepen.io/149203/pen/JXxbEL

<强> HTML

<div class="row">
  <div class="col-xs-12 vert-center-container">
    <video autoplay loop style="width:100%" class="img-responsive">
      <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" />
    </video>
    <div class="vert-center-text">
      <h1>Caption Text</h1>
    </div>
  </div>  
</div>

<强> CSS

.vert-center-text {
   position: absolute; 
   top: 40%; 
   left: 0;
   text-align: center; 
   width: 100%; 
}

.vert-center-text h1 { 
    color: white;
}

.vert-center-container {
  position:relative;
}

答案 2 :(得分:0)

将标题容器模拟为表格,将h3,h6模拟为具有vertical-align:middle的表格单元格

.header-container {
  width: 100%;
  height: 100vh;
  position: relative;
  padding: 1px;
  display:table;
}

.video-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;

}

video {
  position: absolute;
  z-index: -100;
  width: 100%;
}

h3,
h6 {
  display:table-cell;
  vertical-align:middle;
  text-align: center;
  color: white;
}
<body>
  <div class="header-container">
    <div class="video-container">
      <video preload="true" autoplay="autoplay" loop="loop" volume="0">
            <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" type="video/mp4">
        </video>
    </div>
    <!-- video-container -->
    <h3>Centered Title</h3>
    <h6>This should be vertically and horizontally centered</h6>
  </div>
  <!-- header-container -->
</body>