Search code examples
htmlcssimagestyleswidth

How to make the max width inherent OR initial (whichever comes first) in css?


I'm trying to have an image auto resize until it fills a div or reaches its initial size. I want it to grow as the div grows but stop when its initial size is reached. I also want it to retain its original aspect ratio the entire time. Is this possible only using css?


Solution

  • Please add some additional detail. If this is as straightforward as you describe, just don't set a width in CSS, and apply a max-width;

    img{
       max-width:100%
    }
    

    and it won't exceed it's natural width. See fiddle - https://jsfiddle.net/6hLz5em2/