How can I vertically center text over a responsive image?

前端 未结 2 1692
既然无缘
既然无缘 2021-02-10 11:15

How can I get the caption text on these images to move around when then the browser window is resized? My implementation is jicky and I need a way to keep the text from sliding

2条回答
  •  被撕碎了的回忆
    2021-02-10 11:23

    Just add one class to parent container, make it's position relative.

    .img-container {
      position:relative;
    }
    

    And then make homeImageLink absolute and give top at around 45%..

    It will make it vertically centered..

    .homeImageLink {
       position: absolute; 
       top: calc(50% - 24px); //24px is font size of H1 I assume 
       left: 0;
       text-align: center; 
       width: 100%; 
    }
    

    Demo here : http://codepen.io/anon/pen/bJadE

提交回复
热议问题