How to make a background image fill the entire screen without losing image resolution

Asked

Viewed 52,643 times

2

I have a part on my site where I need to put a background image occupying the entire screen, both wide and high. I managed to do this with css.

.intro {
  display: table;
  width: 100%;
  height: 100%;
  padding: 100px 0;
  text-align: center;
  color: white;
  background: url(../img/Banner-Site.png) no-repeat bottom center scroll;
  background-position: 30% 45%;
  background-color: black;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  background-size: cover;
  -o-background-size: cover;
}

But the problem is that it ends up popping the image, I mean, I lose the resolution and sharpness of the image. I’ve tried putting the image in a bigger size, but it doesn’t solve.

I have a monitor with a resolution of 2560 x 1080. I tested putting the images in a size of 3000 x 1000. And smaller sizes too. All images are in PNG, saved in Photoshop as web file.

Can someone help me?

  • Read the question: http://meta.pt.stackoverflow.com/q/5483/132 and the answer: http://meta.pt.stackoverflow.com/a/5505/132.

  • put the image information as well. size, weight and format.

  • I have a monitor with a resolution of 2560 x 1080. Test by placing the images in a size of 3000 x 1000. And in smaller sizes as well. All images are in PNG, saved in Photoshop as web file.

  • Ever used a max-width:100%; ?

  • Already tried, I put max-width:100%; the image disappears and everything is white.

2 answers

2


Do you really want the image to occupy the entire viewport? If so, it has a relative measurement unit, which is widely used in responsive design. The vh and vw.

Many web responsive design techniques rely heavily on rules percentage. However, percentage HSC measures are not always the best solution for all problems. width in CSS is related to nearest element-ancestal. But what if you wanted to use width or the height of the viewport instead of the width of the parent element? That’s exactly what the vh and vw units provide.

The measure vh is equal to 1/100 of the height of the viewport. So, for example, if the height of the navigator is 900px, 1vh equals 9px and, similarly, if the width of the viewport is 750px, 1vw is 7.5px.

There are infinite possibilities of use with these units. For example, "sliders" of full height (full-height) could be achieved with a single line of CSS:

.slide {
    height: 100vh;
}

SOURCE:http://desenvolvimentoparaweb.com/css/unidades-css-rem-vh-vw-vmin-vmax-ex-ch/

* {
  padding:0;
  margin:0;
}
.intro {
  display: table;
  width: 100%;
  height: 100vh;
  padding: 100px 0;
  color: white;
  background: url('http://lab27.blob.core.windows.net/wordpress/2016/05/css-code.jpeg') no-repeat bottom center scroll;
  background-position: 30% 45%;
  background-size: cover;
}
<div class="intro"></div>

  • 1

    That way it worked @Gilmar. Thank you so much for your help.

  • 1

    Very well explained Gilmar !! Thank you very much.

0

Hello! I want to help, but I need a little more information. I saw that you provided the information about the image. But I right here with a random image of the Internet had no problems with the code and it seemed normal. Could you provide the image? Or describe what you are trying to do? Do you want to cover the entire screen with the image? I’m sorry to put it here in answer form, but I’m not at liberty to comment.

EDIT

If to occupy the entire screen, it needs to be in display: table?

That would work:

.intro {
    display: block;
    width: 100vw;
    height: 100vh;
    padding: 0;
    text-align: center;
    color: white;
    background: url(../img/Banner-Site.png) no-repeat bottom center scroll;
    background-position: 30% 45%;
    background-color: black;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
}

EDIT 2

If the image is large enough, you can replace the cover for contain. There is less possibility of distortion. The cover forces the image to fit whole on the screen, already the contain fits her in the best way possible.

Personally recommend a much wider image than high for desktop and a higher version than wide for mobile. And this way use the contain not to distort.

EDIT 3

body {
    margin: 0;
}
.intro {
    display: block;
    width: 100vw;
    height: 100vh;
    padding: 0;
    text-align: center;
    color: white;
    background: url(../img/Banner-Site.png) no-repeat bottom center scroll;
    background-color: black;
    -webkit-background-size: contain;
    -moz-background-size: contain;
    background-size: contain;
    -o-background-size: contain;
}

These stripes on the sides I would have to see in your image, because here I am not able to simulate, but at first I imagine it may be of the margin created by body. But in question the responsiveness, so I said make versions. You would have to work with Media Queries and versions of the same image to better adapt to mobile or tablet. Maybe this part is better if you do a little research to better understand, but what they do is adapt their CSS to different types of screens, like smartphones and tablets.

  • So @Leonvfx, I did the test here putting the 'display: block' and taking the cover and putting the 'contain'. We change the image here to a much higher resolution, and save the image in 32 bits, png. Doing so the sharpness and resolution of the image was perfect, but not this entire screen, even if I save the image in a resolution of '2800 x 1300'. He gets the sides with "black tajas" And this way I also had problem with the responsive, the smaller, less space he has, and I need him to fill everything, even in the responsive.

  • And it worked all right? I just don’t recommend saving in png unless there will be transparency, because it weighs a lot more than a jpg.

  • Doing so the sharpness and resolution of the image was perfect, but not this entire screen, even if I save the image in a resolution of '2800 x 1300'. He gets the sides with "black stripes" And this way I also had problem with the responsive, the smaller, less space he has, and I need him to fill everything, even in the responsive.

  • See my EDIT 3! :)

  • So @Leon, I tested here by putting 'width: 100%; height: 100vh;' and I went back to cover and it worked. I couldn’t put the image 100% sharp, but it’s improved to about 95%. And it’s filling the whole screen. Thank you very much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.