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

后端 未结 14 1154
再見小時候
再見小時候 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.

    
    
        
    
    
    

    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.

    
    
    
        
    
    
    
    
    
    
    
    
    
    
    

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

提交回复
热议问题