text centered on img with responsive and within a link

0

I have a problem and that is that I can not leave a text that goes inside an image and is inside a link, when I start to do the responsive the text goes too up, some way to power fix it?

<div class="container">
  <a href="url">

    <img class="img" src="D:\prueba.jpg">

    <div class="texto">
        Texto Centrado 
    </div>

  </a>
</div>

.container {
    position: relative;
}

.img {
    float: left;
    object-fit: cover;
    width: 100%;
    height: 1080px;

}

.texto {
    position: absolute;
    text-align: center;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    font-size: 50px;
    color: white;
    font-weight: 800;
    margin-top: 25%;
}
    
asked by lujan 01.09.2018 в 12:22
source

1 answer

1

That happens to you because the percentages are calculated with respect to the width and not the height, so when dissimulating the width of the screen the margin-top is smaller and the text goes up, to solve it you have two ways, to put a margin-top in px so that it does not move, but that would look bad in small devices, the other way would be to calculate the height of the window with js and then give that value to the margin-top of the text. I have not been able to give you an example but doing that with js or jquey is not very complicated, anyway if you need an example, tell me and when I have time I try to help you better.

    
answered by 03.09.2018 / 00:37
source