How to position text over an image in css

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

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

7条回答
  •  天涯浪人
    2020-11-22 04:02

    This is another method for working with Responsive sizes. It will keep your text centered and maintain its position within its parent. If you don't want it centered then it's even easier, just work with the absolute parameters. Keep in mind the main container is using display: inline-block. There are many others ways to do this, depending on what you're working on.

    Based off of Centering the Unknown

    Working codepen example here

    HTML

    Your Text is responsive and centered

    CSS

    .containerBox {
        position: relative;
        display: inline-block;
    }
    .text-box {
        position: absolute;    
        height: 100%;
        text-align: center;    
        width: 100%;
    }
    .text-box:before {
       content: '';
       display: inline-block;
       height: 100%;
       vertical-align: middle;
    }
    h4 {
       display: inline-block;
       font-size: 20px; /*or whatever you want*/
       color: #FFF;   
    }
    img {
      display: block;
      max-width: 100%;
      height: auto;
    }
    

提交回复
热议问题