Vertical centering variable height image while maintaining max-width/height

后端 未结 3 1518
无人及你
无人及你 2020-11-30 08:37

I want to center an image of unknown width/height on a page, while making sure that it shrinks if it is bigger than the page (i.e. use max-width/max-heigh

相关标签:
3条回答
  • 2020-11-30 08:50

    Here is one way with javascript:

    <html>
    <head>
    <style>
      html, body{
        height:100%;
        margin:0;
        border:0;
        padding:0;
        background:#000;
      }
      body{
        position:relative;
      }
      img{
        border:0;
        padding:0;
        margin:0 auto;
        max-width:100%;
        max-height:100%;
        display:block;
        position:absolute;
      }
    </style>
    </head>
    <body>
      <img />
      <script>
        (function(){
          var imgs = [
              'http://farm3.static.flickr.com/2396/2078094921_ee60c42d0f.jpg',
              'http://farm6.static.flickr.com/5242/5353846171_9169f810dc_b.jpg',
              'http://farm6.static.flickr.com/5284/5336493514_8e41696b66_b.jpg'
            ],
            img = document.getElementsByTagName('IMG')[0],
            getStyle = function(elm){
              return window.getComputedStyle ? window.getComputedStyle(elm) : elm.currentStyle;
            },
            bodyStyle = getStyle(document.body),
            toInt = function(pxSize){
              return +pxSize.replace(/px/,'');
            },
            chgImg = function(){
              img.src = imgs[i++ % imgs.length];
              img.onload = function(){
                var imgStyle = getStyle(img);
                img.style.left = ( toInt(bodyStyle.width)  - toInt(imgStyle.width) ) / 2 + 'px';
                img.style.top =  ( toInt(bodyStyle.height) - toInt(imgStyle.height) ) / 2 + 'px';
                img.onload = null;
              };
            },
            i = 0;
    
          chgImg();
          setInterval(chgImg, 3000);
        })();
      </script>
    </body>
    </html>​
    
    0 讨论(0)
  • 2020-11-30 08:58

    Try something like this...

    Demo: http://jsfiddle.net/wdm954/jYnk8/1/

    $(function() {
    
        h = $('.img').height();
        w = $(window).height();
    
        if (h < w) { 
            $('.img').css('margin-top', (w - h) /2 + "px");
        }
        else {
            $('.img').height(w);
        }
    
    });
    

    (You can test different sizes by changing some CSS I have commented out.)

    0 讨论(0)
  • 2020-11-30 09:09

    This should prove to work quite well... no JavaScript necessary :)

    See the working demo on jsFiddle.

    CSS

    /* Don't Change - Positioning */
    .absoluteCenter {
     margin:auto;
     position:absolute;
     top:0;
     bottom:0;
     left:0;
     right:0;
    }
    
    /* Sizing */
    img.absoluteCenter {
     max-height:100%;
     max-width:100%;
    }
    

    HTML

    <img class="absoluteCenter" src="PATHTOIMAGE">
    

    Note: This class can be used for anything quite easily. If you use this for something other than an image, make sure to add a TAG.absoluteCenter CSS rule with a max-height and max-width of your choosing (where TAG is the HTML tag you're using [e.g. div.absoluteCenter] and max-width/max-height is less than 100%).

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