【问题标题】:Centering Text over Image在图像上居中文本
【发布时间】:2017-07-23 00:18:18
【问题描述】:

这是我总结的代码

HTML:

<div class="wrap">
      <div>
      <img src="Pictures/titlepic.jpg" width="1035" height="200">
      <h1 class="text_over_image">Welcome to my Sandbox</h1>
      </div>
</div>

CSS:

.wrap{
    width: 1060px;
    margin: auto;
    }
.text_over_image{
    position: absolute;
    top: 20px;
    }   

我尝试了left: 50%text-align:center,任何数量的事情都没有运气。我不想做left: 50px(或任何需要的值),因为看不见的是文本的长度会根据 Javascript 值而变化。所以标题可能很短,也可能很长,无论如何我都希望它居中。

想法?

【问题讨论】:

  • 你需要把图片放在你的div的背景中

标签: html css image text


【解决方案1】:

试试下面的css:

.text_over_image{
    position: absolute;
    top: 0;
    left:0;
    right:0;
    bottom:0;
 }  

===编辑===

.wrap {
    width: 1060px;
    height:auto;
    margin: auto;
    text-align:center;
    position:relative;
}
.text_over_image {
    position: absolute;
    margin: auto;
    top: 0;
    left:0;
    right:0;
    bottom:0;
    color:#fff;
    height:100px;
}

你去JsFiddle

【讨论】:

  • 很遗憾,它没有达到您可能想的效果:d.pr/i/huk4
  • @ChaseWestlye 嗯 2 分钟让我修补一下。
【解决方案2】:

div.counter {
    position: relative;
    display: inline-block;
}
div.counter span {
    position: absolute;    
    text-align: center;
    height: 100%;
    width: 100%;
}
div.counter span:before {
   display: inline-block;
   vertical-align: middle;
   height: 100%;
   content: '';
}
<div class="counter">
    <span>TEXT</span>
    <img src="http://placehold.it/100x100"/>
</div>

【讨论】:

    【解决方案3】:

    HTML

    <div class="wrap">
          <div>
           <h1 class="text_over_image">Welcome to my Sandbox</h1>
          </div>
    </div>
    

    CSS

    .wrap
    {
    background-image:url(Pictures/titlepic.jpg);
    width:1035px;
    height:200px;
    }
    .text_over_image
    {
    text-align:center;
    line-height:200px;
    }
    

    【讨论】:

    • 我想你搞定了... 文本居中但背景图像不再出现: CSS: d.pr/i/7hMZ HTML: d.pr/i/dzeK Render: d.pr/i/bCBR Ideas?
    • 高度是动态的,行高200px不会导致文字居中。
    猜你喜欢
    • 2013-07-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多