html - 在背景图像上居中叠加文本

标签 html css

我希望文本 (.herotext) 完美地位于背景图像 (.herobanner) 的垂直和水平中心。此文本旨在覆盖图像,就像使用 slider /旋转木马一样。

我尝试用 margin: 0 auto 来做这件事,但没有任何结果

CSS

.herobanner {
    height:500px;
    width:100%;background:#000;
    background-size:cover !important;
    background-repeat: no-repeat;
    background: url(../img/head.jpg) center
}

.herotext {
    color:#fff;
    text-align: center;
    margin: 0 auto;
}

HTML

<div class="herobanner">
    <h1 class="herotext">Hello</h1>
</div>

最佳答案

一个解决方案

.herobanner {
  height: 500px;
  width: 100%;
  background: #000;
  background-size: cover !important;
  background-repeat: no-repeat;
  background: url(../img/head.jpg) center;
  background: lightblue;
  display: table;
  border: 1px solid grey;
}
.herotext {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<div class="herobanner">
  <h1 class="herotext">Hello</h1>

</div>

关于html - 在背景图像上居中叠加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334819/

相关文章:

html - 如何填充使用特定颜色的 Bootstrap 网格系统分区的空 div?

html - 分别为表单图像按钮设置 CSS 样式?

javascript - 使用 AngularJs 使用 Json 回调填充 Select 字段

HTML/CSS : How to put a image in the bottom right corner and wrap text around it?

html - 为什么 height=100% 不起作用?

css - 在 Font Awesome 图标焦点上使用轮廓

php - eventResize 不适用于 fullCalendar2.6*

html - 没有绝对定位的堆叠列表项

html - 实时浏览器刷新 css/html 编辑

javascript - 按坐标绘制大厅平面图