html - hero div中的居中定位文本

标签 html css twitter-bootstrap responsive-design css-position

在这段代码中,文本没有垂直居中放置在 div 上。 我不明白,因为我给了职位 50%。 对于图像,我不能给出我想要的自定义高度吗?

在我的照片上,顶部的蓝线是导航栏,不要介意。

<section class="bgimage-fooldal">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hero-text-fooldal">
        <h2 class="hero_title">Hello, world! Full width Hero-unit header</h2>
        <p class="hero_desc">This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique. Vevőidről, szállítóidról online információt nyerhetsz, így értesülhetsz arról.</p>
        <p><a href="#" class="btn btn-large hero_to_link">Árajánlat kérése »</a></p>
      </div>
    </div>
  </div>
</section>


/*************************** Hero css ****************************/
.bgimage-fooldal {
  width:100%;
  height:400px;
  background: url('../images/assets/teszt-1.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size:cover;
  background-attachment: fixed;
}
.hero-text-fooldal {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

}

最佳答案

你需要给 position:relative 到 bgimage-fooldal 类,这样它就会根据你的 div 高度垂直居中

.bgimage-fooldal {
  width:100%;
  height:400px;
  background: url('https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size:cover;
  background-attachment: fixed;
  position:relative;
}
.hero-text-fooldal {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

}
<section class="bgimage-fooldal">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hero-text-fooldal">
        <h2 class="hero_title">Hello, world! Full width Hero-unit header</h2>
        <p class="hero_desc">This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique. Vevőidről, szállítóidról online információt nyerhetsz, így értesülhetsz arról.</p>
        <p><a href="#" class="btn btn-large hero_to_link">Árajánlat kérése »</a></p>
      </div>
    </div>
  </div>
</section>

关于html - hero div中的居中定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606246/

相关文章:

html - 在元素之间添加空间,使背景不水平拉伸(stretch)

javascript - :file on fileSelect not working within bootstrap modal

php - 如何搜索html标签并删除它

javascript - 处理缓存 beyong Nginx 服务器和 webpack js 和 css 版本控制

CSS 位置 : fixed

css - 即 z-index 不出现在元素后面

javascript - 更改大型 Bootstrap 模态的高度

ruby - Rails 3.2.3 + Twitter Bootstrap + Nav-Tabs : How to show a specific tab?

javascript - 将每个字母表定位到每个答案容器但不添加新的答案容器 - 使用 AngularJs 拖放

javascript - 获取 div 的高度,但不是以像素为单位