Text vertically and horizontally centered over a responsive image

后端 未结 3 1145
失恋的感觉
失恋的感觉 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:21

    I just added to the html

    <div align="center"></div>

    to surround your existing code to get the image to center

    hope that helps

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 05:37

    Please Try the below css for .overlay as in your fiddle

    .overlay {
        z-index: 99;
        width: 70%;
        /* height: 100%; */
        /* margin-left: 15%; */
        /* vertical-align: middle; */
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
    

    or this is the updated fiddle link http://jsfiddle.net/hLdbZ/284/

    0 讨论(0)
提交回复
热议问题