【问题标题】:How to vertically center page title text within banner image (no flexbox/grid)? [duplicate]如何在横幅图像中垂直居中页面标题文本(无弹性框/网格)? [复制]
【发布时间】:2020-12-11 18:04:05
【问题描述】:

我正在处理仅 CSS 的 psd 转换,我需要将横幅文本垂直居中在横幅图像的中间。有什么建议如何在不使用 flexbox 和 grid 的情况下完成?

header {
    background-image: url(../assets/main-banner.png);
    background-size: cover;
    background-repeat: no-repeat;
    /* viewport (screen height), the el is now taking 90% of the browser height */
    /* We are doing this since the <header> doesn't have a ton
  of content and we want to be able to see the background image. */
    height: 90vh;
    padding-top: 30px;

}

h1 {
  font-family: 'Iceberg', cursive;
  font-size: 8rem;
  text-align: center;;
  line-height: 80px;
  text-transform: uppercase;
}

header h2 {
  font-family: 'Montserrat', sans-serif;
  font-size: 3.5rem;
  text-align: center;
  text-transform: none;
  background-color: rgb(146, 0, 255);

}
<header class="clearfix">
  <div class="wrapper">
  <!-- Navigation STARTS-->
    <nav class="main-menu">
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">About</a></li>
        <li><a href="">Blog</a></li>
        <li><a href="">Contact</a></li>
      </ul>
    </nav>
    <!-- Navigation ENDS-->
    <div class="banner-content">
      <h1>Thunder<br>Force<br> Stadium</h1>
      <h2>presented by hot cops</h2>
    </div>
  </div>
</header>

【问题讨论】:

    标签: html css layout responsive-design css-selectors


    【解决方案1】:

    正如我提到的,这只是垂直居中,所以为了固定水平位置,我做了以下操作:

    header {
        position: relative;
    }
    
    .banner-content {
       position: absolute;
       top: 50%;
       left: 50%;
       transform: translate(-50%, -50%);
    }
    

    【讨论】:

      【解决方案2】:

      如果您想垂直居中横幅内容,您可以尝试添加以下样式:

      header {
          ... // your other syling
          position: relative;
      }
      
      .banner-content {
         position: absolute;
         top: 50%;
         transform: translateY(-50%);
      }
      

      【讨论】:

      • 嗨,特里,我添加了您建议的这个 sn-p,但文本未在中心(水平)对齐,并且以前是使用 text-align: center。任何关于如何解决此问题的建议,因为现在此属性不再起作用。
      • 啊,我以为你只希望它垂直居中,我的错。尝试添加left: 50% 并将转换更改为transform: translate(-50%, -50%)
      • 是的,我想要它垂直和水平 - 应该澄清。然而,这真的很有帮助 - 我很感激!
      猜你喜欢
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2014-09-14
      • 2014-01-28
      • 2020-12-11
      • 2017-03-05
      • 2018-08-13
      • 1970-01-01
      相关资源
      最近更新 更多