Image No Stretch or crop

前端 未结 3 510
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 04:22

My question is simple. Let\'s say I have two rectangular images. The first is 200px wide and 100px tall, and the second is 100px wide and 200px tall.

I want to display t

相关标签:
3条回答
  • 2021-01-21 05:04

    You should be able to use the max-width and max-height properties, like so:

    img {
        max-width: 150px;
        max-height: 150px;
    }
    

    This should constrain it to that box without stretching it.

    If you want to make sure that each appears in a square box of size 150px by 150px, you could contain each image in a container div, and force them to be exactly that size:

    <div class="container">
      <img>
    </div>
    

    with the following CSS:

    img {
        max-width: 150px;
        max-height: 150px;
    }
    .container {
        width: 150px;
        height: 150px;
    }
    
    0 讨论(0)
  • 2021-01-21 05:07

    What about:

    img {
        max-height:150px;
        max-width:150px
    }
    

    To achieve your 2nd question on making smaller images bigger, you can do with jQuery. CSS could work if you knew the photo orientation before hand and applied a different css class to those images... But this will work and then you don't need the CSS max-width stuff any more.

    <div class="container"><img src="images/DSC_0470.JPG" alt="Rad Image" /></div>
    
    <script>
        $(document).ready(
            function () {
                $('.container img').each(
                function () {
                    var theWidth = $(this).width();
                    var theHeight = $(this).height();
                    if (theWidth < theHeight) {
                        $(this).height(150);
                    }
                    else {
                        $(this).width(150);
                    }
    
                });
            });</script>
    
    0 讨论(0)
  • 2021-01-21 05:20

    Try this format...

    <img src="image.jpg" style="border:none;position:absolute; top:20px; left:50px; width:100px; height:200px;">
    
    0 讨论(0)
提交回复
热议问题