Make image fill div completely without stretching

后端 未结 13 1650
情话喂你
情话喂你 2020-11-30 04:22

I have large images of varying dimensions that need to completely fill 240px by 300px containers in both dimensions. Here is what I got right now, which only works for one d

相关标签:
13条回答
  • 2020-11-30 04:32

    I used this plugin that accounts for any ratio. It also requires imagesloaded plugin to work. This would be useful for numerous images across a site needing this treatment. Simple to initiate too.

    https://github.com/johnpolacek/imagefill.js/

    0 讨论(0)
  • 2020-11-30 04:35

    The following solution is very short and clean if you need to insert img tag into div tag:

    .container, .container img 
    {
    	max-height: 300px;
    	max-width: 240px;
    }
    Try to open every image into another page you will notice that originals are all different sized but none is streched, just zoomed:
    <p></p>
    
    <div class="container"><img src="https://www.gentoo.org/assets/img/screenshots/surface.png" /></div>
    <p></p>
    <div class="container"><img src="https://cdn.pixabay.com/photo/2011/03/22/22/25/winter-5701_960_720.jpg" /></div>
    <p></p>
    <div class="container"><img src="https://cdn.arstechnica.net/wp-content/uploads/2009/11/Screenshot-gnome-shell-overview.png" /></div>
    <p></p>
    <div class="container"><img src="http://i.imgur.com/OwFSTIw.png" /></div>
    <p></p>
    <div class="container"><img src="https://www.gentoo.org/assets/img/screenshots/surface.png" /></div>
    <p></p>
    <div class="container"><img src="https://freebsd.kde.org/img/screenshots/uk_maximignatenko_kde420-1.png" /></div>
    <p></p>
    <div class="container"><img src="https://i.ytimg.com/vi/9mrOgkYje0s/maxresdefault.jpg" /></div>
    <p></p>
    <div class="container"><img src="https://upload.wikimedia.org/wikipedia/commons/5/59/Linux_screenshot.jpg" /></div>
    <p></p>

    Also, if you don't need to use a div you can just write an even shorter css:

     img
        {
            max-height: 300px;
            max-width: 240px;
        }
    
    0 讨论(0)
  • 2020-11-30 04:43

    It works if you add the following to the parent div for img styling; https://jsfiddle.net/yrrncees/10/

    .container img {
    position: relative;
    vertical-align: middle;
    top: 50%;
    -webkit-transform: translateY(-50%);
    min-height: 100%;
    min-width: 100%;
    object-fit:cover;
    }
    
    0 讨论(0)
  • 2020-11-30 04:44

    You should try this:

    img {
        min-width:100%;
        min-height:100%;
    }
    
    0 讨论(0)
  • 2020-11-30 04:45

    For anyone looking to do this that doesn't have dynamic images, here's an all-CSS solution using background-image.

    <div class="container" 
        style="background-image: url('http://placehold.it/300x1500'); 
        background-size: cover; background-position: center;">
    </div>
    <div class="container" 
        style="background-image: url('http://placehold.it/1500x300'); 
        background-size: cover; background-position: center;">
    </div>
    

    The "background-size: cover" makes it so that the image scales to cover all of the div while maintaining the aspect ratio. The CSS could also be moved to a CSS file. Although if it's dynamically generated, the background-image property will have to stay in the style attribute.

    0 讨论(0)
  • 2020-11-30 04:45

    Here is another solution I found, that no need to seperate portraid or landscape or scripting.

      <div class="container">
        <img src="http://placehold.it/500x500" class="pic" />
      </div>
    

    CSS

    .container{
      position: relative;
      width: 500px;
      height: 300px;
      margin-top: 30px;
      background: #4477bb;
    }
    .pic{
        max-width: 100%;
        width: auto;
        max-height: 100%;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
    }
    

    Here it is, it works well...

    https://jsfiddle.net/efirat/17bopn2q/2/

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