1

Is there any way to make responsive text inside an image? I would like to do it without using media queries. I already tried this Horizontal centering text over image via CSS but it does not work.

The problem (pic. down) is when reaching small screen values, the text goes outside the image and it does not resize it. With media queries I could do it just by changing the size of the text, but is not any other way to do it?

Text outside image

HTML code:

<div class="wrapper-pig">
     <center><img src="http://s24.postimg.org/4axzhhujp/schwein.png" style="width: 70%; height=auto;"/></center>
      <div class="pig-text" >
           <span style="color:white;" >9563</span>
       </div>

</div>

CSS code:

.wrapper-pig{
   width: 100%;
   background-color: yellow;
   position: relative;
   box-sizing: border-box;

}

.pig-text{
    font-size: 160%;
    position: absolute;
    display: inline-block;
    left: 40%;
    top: 40%;
}

Please, any help?

I prepared a fiddle to show the problem better: My fiddle

Community
  • 1
  • 1
karlihnos
  • 415
  • 1
  • 7
  • 22

3 Answers3

3

You can make this to center perfectly:

.pig-text{
  font-size: 160%;
  position: absolute;
  display: inline-block;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
2

I recommend to use a responsive font size, check out this here

https://jsfiddle.net/f11y74ms/

The hint comes from

Responsive font size according to window width, is it possible with CSS?

The commands are vh, vw, vmin, so the smallest viewpoint is taken as a reference.

Community
  • 1
  • 1
rst
  • 2,510
  • 4
  • 21
  • 47
0

You can try what RobertStettler has said.

You can use viewport value instead of ems, pxs or pts.

1vw = 1% of viewport width

1vh = 1% of viewport height

1vmin = 1vw or 1vh, whichever is smaller

1vmax = 1vw or 1vh, whichever is larger

.schwein-text{
    font-size: 4vw;
    position: absolute;
    display: inline-block;
    left: 40%;
    top: 40%;
}

Working Demo here

Shrinivas Pai
  • 7,491
  • 4
  • 29
  • 56