How to position text over an image in css

后端 未结 7 863
囚心锁ツ
囚心锁ツ 2020-11-22 03:38

How do I center a text over an image in css?

相关标签:
7条回答
  • 2020-11-22 04:29

    as of 2017 this is more responsive and worked for me. This is for putting text inside vs over, like a badge. instead of the number 8, I had a variable to pull data from a database.

    this code started with Kailas's answer up above

    https://jsfiddle.net/jim54729/memmu2wb/3/

    My HTML

    <div class="containerBox">
      <img class="img-responsive"    src="https://s20.postimg.org/huun8e6fh/Gold_Ring.png">
      <div class='text-box'>
       <p class='dataNumber'> 8 </p>
      </div>
    </div>
    

    and my css:

    .containerBox {
      position: relative;
      display: inline-block;
    }
    
    .text-box {
      position: absolute;
      height: 30%;
      text-align: center;
      width: 100%;
      margin: auto;
      top: 0;
      bottom: 0;
      right: 0;
      left: 0;
      font-size: 30px;
    }
    
    .img-responsive {
      display: block;
      max-width: 100%;
      height: 120px;
      margin: auto;
      padding: auto;
    }
    
    .dataNumber {
      margin-top: auto;
    }
    
    0 讨论(0)
提交回复
热议问题