Text vertically and horizontally centered over a responsive image

后端 未结 3 1211
失恋的感觉
失恋的感觉 2020-12-19 04:42

I\'m looking to center text both vertically and horizontally over an image that grows when the page gets wider.

I originally had the image set as the background for

3条回答
  •  春和景丽
    2020-12-19 05:34

    You can use CSS background-size to set the width to 100% and the height will be calculated to maintain aspect ratio.

    Here's a fiddle using that technique.

    If you want the image as an HTML element then I suggest you set it's position to absolute and use the same method of disply:table-cell to center the overlay:

    Here's a fiddle using that method, this one stretches the image because of the max-height.

提交回复
热议问题