Background image 100%

4

I've always had this curiosity, how to leave the background image 100% according to the monitor (We all know that there are several measures). So I was told that it would be a script in JS or jQuery, I do not remember very well, and it worked as follows:

You create several images with the following measures:

1024x768-
1280x768-
1360x768-
1366x768-

They are resolutions of my notebook, they might have other measures.

When you access the site, this script reads the measurement and loads that image. Is that the way it even works? Would you be able to do this in CSS?

Example on a website: link

    
asked by anonymous 16.10.2014 / 03:02

3 answers

5

There are several techniques for getting an image to occupy the full width and height of the screen. Each technique is considered appropriate for a given scenario as we can handle the problem via JavaScript, CSS or server side.

In this answer solutions make use of JavaScript with jQuery feature.

jQuery

Image depending on screen width

This plugin works the way that you indicate in the question, ie it makes use of several images loading the most appropriate according to the width of the screen:

(function() {

var win = $(window);

win.resize(function() {

    var win_w = win.width(),
        win_h = win.height(),
        $bg    = $("#bg");

    // Carregar imagem de fundo mais estreita com base na
    // largura do ecrã, mas nunca carregar nada mais estreito
    // do que o que já está carregado, se alguma coisa estiver.
    var available = [
      1024, 1280, 1366,
      1400, 1680, 1920,
      2560, 3840, 4860
    ];

    var current = $bg.attr('src').match(/([0-9]+)/) ? RegExp.$1 : null;

    if (!current || ((current < win_w) && (current < available[available.length - 1]))) {

      var chosen = available[available.length - 1];

      for (var i=0; i<available.length; i++) {
        if (available[i] >= win_w) {
          chosen = available[i];
          break;
        }
      }

      // Definir a nova imagem
      $bg.attr('src', '/img/bg/' + chosen + '.jpg');

      // para testar...
      // console.log('Chosen background: ' + chosen);

    }

    // Determinar se a largura ou a altura deve ser de 100%
    if ((win_w / win_h) < ($bg.width() / $bg.height())) {
      $bg.css({height: '100%', width: 'auto'});
    } else {
      $bg.css({width: '100%', height: 'auto'});
    }

  }).resize();

})(jQuery);

Code removed from the CSS-Tricks website in this article:

Perfect Full Page Background Image | CSS-Tricks

Vegas Background jQuery Plugin

Very easy to use, it allows us to transform a simple <img/> into an image to occupy 100% X 100% of the screen, as well as to indicate the image in the PlugIn :

  • Include Script after adding jQuery :

    <script type="text/javascript" src="/vegas/jquery.vegas.js"></script>
    
  • Include CSS :

    <link rel="stylesheet" type="text/css" href="/vegas/jquery.vegas.css" />
    
  • Start PlugIn :

    $(function() {
      $.vegas({
        src:'/images/background.jpg'
      });
    });
    

    Vegas - Demo

  • 16.10.2014 / 12:18
    4

    You can do this with CSS as follows: link

    #banner{
        background:url('https://dq197.infusionsoft.com/Download?Id=2300') no-repeat;
        background-size:contain;
        width:100%;
        height:260px;
    }
    

    The value contain (English) in the background-size property allows:

      

    This keyword specifies that the background image should be sized to be as large as possible, while ensuring that its dimensions are less than or equal to the corresponding dimensions of the background positioning zone.

        
    28.10.2014 / 06:53
    3

    You can do it by CSS like this:

    body {
        background: url(caminho_para_a_imagem);
        background-size: cover;
    }
    

    cover: Resizes the image to completely fill the background area, maintaining the aspect ratio.

    If you do not care about the ratio, you can use:

    body {
        background: url(caminho_para_a_imagem);
        background-size: 100% 100%;
    }
    

    There are other parameters , choose the one that best suits your needs.

        
    16.10.2014 / 05:55