【问题标题】:Positioning text over scalable image在可缩放图像上定位文本
【发布时间】:2018-02-10 14:06:49
【问题描述】:

#hero-container {
    display: flex;
    position: relative;
    height: 520px;
}

.hero {
    position: absolute;
    left: 0;
    top: 0;
}
  
#text {
    z-index: 100;
    position: absolute;
    color: white;
    font-size: 3vw;
    font-weight: bold;
    left: 150px;
    top: 40px;
}
    
#text2 {
    z-index: 100;
    position: absolute;
    color: white;
    font-size: 2vw;
    font-weight: bold;
    left: 150px;
    top: 70px;
}
    
#hero-container img {
    display: flex;
    width: 100%;
}
<html>
  <head>
  </head>
  <body>
    <div id="hero-container">
      <img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg"></img>
      <p id="text">
        First text here
      </p>
      <p id="text2">
        Secondary
      </p>
    </div>
  </body>
</html>

我有一个 width:100% 的图像,在一个固定的容器中。因为我的图像调整了大小,所以文本没有跟随并保持在相同的位置,从而在容器上移动。

如何在不添加 1000 个媒体查询的情况下使文本保持在图像的中心(垂直)和左侧,并有几个像素填充(水平)?

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    您可以大大简化您的 CSS:

    1. 移除 flex(除非有特定原因需要它)。
    2. 将要垂直居中的文本包装在divtranslateY(-50%) 中。
    3. 确保图像周围的包装具有position:relative

    #hero-container {
      position: relative;
    }
    
    #center-text {
      z-index: 100;
      position: absolute;
      color: white;
      font-weight: bold;
      left: 15%;
      top: 50%;
      transform: translateY(-50%);
    }
      
    #text {
      font-size: 3vw;
      margin: 0;
    }
        
    #text2 {
      font-size: 2vw;
      margin: 0;
    }
        
    #hero-container img {
      width: 100%;
      height: auto;
    }
    <div id="hero-container">
      <img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg" />
      <div id="center-text">
        <p id="text">
          First text here
        </p>
        <p id="text2">
          Secondary
        </p>
      </div>
    </div>

    如果您使用更标准的字体大小(而不是 vw),这将完全响应您的分辨率,并且应该在 IE10+(或带有 -ms-transform 前缀的 IE9+)中工作。

    【讨论】:

      【解决方案2】:

      您可以将图像显示为background-image#hero-container

      那么你就不需要在文本上使用position: absolute了。

      您可以以百分比值将padding 添加到容器中 - 这将比固定像素更好地缩放。您可以将我在 sn-p 中使用的值编辑为适合您的值。

      #hero-container {
        display: flex;
        flex-direction: column;
        height: 520px;
        background: url(https://vreauperle.ro/images/banners/hero-image.jpg);
        background-size: contain;
        background-repeat: no-repeat;
        padding: 5%;
      }
      
      #hero-container p {
        color: white;
        font-weight: bold;
        margin: 0;
      }
      
      #text {
        font-size: 3vw;
        
      }
      
      #text2 {
        font-size: 2vw;
      }
      <div id="hero-container">
        <p id="text">
          First text here
        </p>
        <p id="text2">
          Secondary
        </p>
      </div>

      【讨论】:

      • 关闭,但我相信 OP 想要文本居中,无论视口是什么。查看乔纳森的回答
      【解决方案3】:

      如果您根据 '%' 而不是 'px' 应用 lefttop css 属性> 然后你会得到结果。 Fiddle Is Here

      #hero-container {
          display: flex;
          position: relative;
          height: 520px;
        }
      
        .hero {
          position: absolute;
          left: 0;
          top: 0;
        }
        
        #text {
          z-index: 100;
          position: absolute;
          color: white;
          font-size: 3vw;
          font-weight: bold;
          left: 30%;
          top: 0;
        }
          
          #text2 {
      		  z-index: 100;
      		  position: absolute;
      		  color: white;
      		  font-size: 2vw;
      		  font-weight: bold;
      		  left: 30%;
      		  top: 11%;
          }
          
          #hero-container img {
      		  display: flex;
            width: 100%;
          }
      <html>
        <head>
        </head>
        <body>
        <div id="hero-container">
      	  <img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg"></img>
      	  <div class='txt_wrap'>
                <p id="text">
      		  First text here
      	  </p>
      	  <p id="text2">
      		  Secondary
      	  </p>
            </div>
            
        </div>
        </body>
      </html>

      【讨论】:

      • 在大屏幕上打开页面,或者去展开sn-p。文本混合在一起。您需要在 text1 和 text2 之间提供一些填充
      【解决方案4】:

      将元素垂直和水平居中对齐的代码。

        p {
           position : absolute;
           left: 50%;
           top: 50%;
          transform: translate(-50%, -50%);
           }
      

      在您的情况下,由于您只想垂直居中,请删除 left:50% 并使用 translateY(-50%)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        相关资源
        最近更新 更多