How to resize an image to fit in the browser window?

后端 未结 14 1065
再見小時候
再見小時候 2020-11-28 19:00

This seems trivial but after all the research and coding I can\'t get it to work. Conditions are:

  1. The browser window size is unknown. So please don\'t propose
相关标签:
14条回答
  • 2020-11-28 19:20

    Update 2018-04-11

    Here's a Javascript-less, CSS-only solution. The image will dynamically be centered and resized to fit the window.

    <html>
    <head>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            .imgbox {
                display: grid;
                height: 100%;
            }
            .center-fit {
                max-width: 100%;
                max-height: 100vh;
                margin: auto;
            }
        </style>
    </head>
    <body>
    <div class="imgbox">
        <img class="center-fit" src='pic.png'>
    </div>
    </body>
    </html>
    

    The [other, old] solution, using JQuery, sets the height of the image container (body in the example below) so that the max-height property on the image works as expected. The image will also automatically resize when the client window is resized.

    <!DOCTYPE html>
    <html>
    <head>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            .fit { /* set relative picture size */
                max-width: 100%;
                max-height: 100%;
            }
            .center {
                display: block;
                margin: auto;
            }
        </style>
    </head>
    <body>
    
    <img class="center fit" src="pic.jpg" >
    
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" language="JavaScript">
        function set_body_height() { // set body height = window height
            $('body').height($(window).height());
        }
        $(document).ready(function() {
            $(window).bind('resize', set_body_height);
            set_body_height();
        });
    </script>
    
    </body>
    </html>
    

    Note: User gutierrezalex packaged a very similar solution as a JQuery plugin on this page.

    0 讨论(0)
  • 2020-11-28 19:24

    I know there's already a few answers here, but here is what I used:

    max-width: 100%;
    max-height: 100vh;
    width: auto;
    margin: auto;
    
    0 讨论(0)
  • 2020-11-28 19:26
    width: 100%;
    overflow: hidden;
    

    I believe that should do the trick.

    0 讨论(0)
  • 2020-11-28 19:26

    Use this code in your style tag

    <style>
    html {
      background: url(imagename) no-repeat center center fixed;
      background-size: cover;
      height: 100%;
      overflow: hidden;
    }
    </style>
    
    0 讨论(0)
  • 2020-11-28 19:28

    I had a similar requirement, and had to do it it basic CSS and JavaScript. No JQuery available.

    This is what I got working.

    <html>
          <head>
                <style>
                       img {
                              max-width: 95% !important;
                              max-height: 95% !important;
                           }
                </style>
                <script>
                       function FitImagesToScreen() {
                          var images = document.getElementsByTagName('img');
                          if(images.length > 0){
                             for(var i=0; i < images.length; i++){
                                 if(images[i].width >= (window.innerWidth - 10)){
                                     images[i].style.width = 'auto';
                                   }
                                }
                             }
                       }
                 </script>
          </head>
          <body onload='FitImagesToScreen()'>
          ----    
          </body>
    </html>
    

    Note : I haven't used 100% for image width as there was always a bit of padding to be considered.

    0 讨论(0)
  • 2020-11-28 19:30

    For the future generations, if you want a solution that answers 1-6 and does 7 in a way that allows resize beyond to original size, I have developed a complete solution for this problem:

    <!DOCTYPE html>
    <html>
      <body style="overflow:hidden; margin:0; text-align:center;">
        <img src="https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_2500kB.jpg" style="height:100vh; max-width:100%; object-fit: contain;">
      </body>
    </html>

    0 讨论(0)
提交回复
热议问题