Text over the image/button

后端 未结 8 1707
灰色年华
灰色年华 2021-01-22 02:43

How to make text over the image/button using html/css.

Eg: I have a button(Login Button). I want the text \"Login\" to be placed over that button. The text should be cen

8条回答
  •  旧时难觅i
    2021-01-22 03:10

    Centering in front of an image

    If you need to center text over an image, you might want to wrap your img in a div, use positioning on the img itself as well as the text.

    You would then be able to center this using transforms (supporting multi-lined text).

    demo

    .wrap {
      position: relative;
      display: inline-block;
      height: 300px;
      width: 300px;
    }
    img {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
    }
    p {
      position: absolute;
      top: 50%;
      left: 50%;
      display: inline-block;
      transform: translate(-50%, -50%);
      font-size: 20px;
      color: tomato;
      font-weight: bold;
    }

    some text


    Images with buttons

    Since button elements are centered automatically by default, you're issue would be to add an image to the button.

    This can be achieved using the background-image property.

    So in your CSS use something like:

    background-image:url(http://lorempixel.com/200/200);
    

    adjusting the size using

    background-size:100% 100%;
    

    to stretch it to the full size of the button.

提交回复
热议问题