I'm not sure if this is the best approach, but it works. Also, remember that the following code only works with a single resolution. Therefore, the image may be pixelated.
.fundo-login {
background-attachment: fixed; /* Faz com que a imagem fique fixa */
background-image: url('https://picsum.photos/354/300');
background-size: cover; /* Faz com que a imagem se estenda proporcionalmente */
background-position: center center; /* A propriedade anterior pode recortar a image. Por isso, usaremos o background-position para centralizar o corte e tentar capturar o foco da imagem. */
}
<body class="fundo-login">
<div>
Alguma besteira aqui dentro.
</div>
</body>
If you want to solve the mentioned problem (pixelated image), alternatively you can use this other approach. This will allow you to serve a different resolution image for each viewport.
.fundo-login {
background-attachment: fixed; /* fixa imagem */
background-image: url('https://picsum.photos/354/300');
background-size: cover;
background-position: center center;
}
@media (min-width: 530px) { /* Baixa imagem igual a anterior, mas com resolução de 530x420*/
.fundo-login {
background-image: url('https://picsum.photos/530/420');
}
}
@media (min-width: 720px) { /* Baixa imagem igual a anterior, mas com resolução de 720x530*/
.fundo-login {
background-image: url('https://picsum.photos/720/530');
}
}
@media (min-width: 1024px) { /* Baixa imagem igual a anterior, mas com resolução de 1024x780*/
.fundo-login {
background-image: url('https://picsum.photos/1024/780');
}
}
<body class="fundo-login">
<div>
Alguma besteira aqui dentro.
</div>
</body>
Note: To use the previous method, you must render the different resolutions in some appropriate program and then put them on the server. So in other words, the browser will not convert automatically, but will download according to the resolutions.
Note: Resolutions used are not defaults. So feel free to choose yours.