CSS: 100% width or height while keeping aspect ratio?

前端 未结 14 2527
粉色の甜心
粉色の甜心 2020-11-28 21:05

Currently, with STYLE, I can use width: 100% and auto on the height (or vice versa), but I still can\'t constrain the image into a specific positio

相关标签:
14条回答
  • 2020-11-28 21:42

    Some years later, looking for the same requirement, I found a CSS option using background-size.

    It is supposed to work in modern browsers (IE9+).

    <div id="container" style="background-image:url(myimage.png)">
    </div>
    

    And the style:

    #container
    {
      width:  100px; /*or 70%, or what you want*/
      height: 200px; /*or 70%, or what you want*/
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
    }
    

    The reference: http://www.w3schools.com/cssref/css3_pr_background-size.asp

    And the demo: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size

    0 讨论(0)
  • 2020-11-28 21:43

    Not to jump into an old issue, but...

    #container img {
          max-width:100%;
          height:auto !important;
    }
    

    Even though this is not proper as you use the !important override on the height, if you're using a CMS like WordPress that sets the height and width for you, this works well.

    0 讨论(0)
  • 2020-11-28 21:44

    One of the answers includes the background-size: contain css statement which I like a lot because it is straightforward statement of what you want to do and the browser just does it.

    Unfortunately sooner or later you are going to need to apply a shadow which unfortunately will not play well with background image solutions.

    So let's say that you have a container which is responsive but it has well defined boundaries for min and max width and height.

    .photo {
      max-width: 150px;
      min-width: 75px;
      max-height: 150px;
      min-height: 75px;
    }
    

    And the container has an img which must be aware of the pixels of the height of the container in order to avoid getting a very high image which overflows or is cropped.

    The img should also define a max-width of 100% in order first to allow smaller widths to be rendered and secondly to be percentage based which makes it parametric.

    .photo > img {
      max-width: 100%;
      max-height: 150px;
      -webkit-box-shadow: 0 0 13px 3px rgba(0,0,0,1);
      -moz-box-shadow:    0 0 13px 3px rgba(0,0,0,1);
      box-shadow:         0 0 13px 3px rgba(0,0,0,1);
    }
    

    Note that it has a nice shadow ;)

    Enjoy a live example at this fiddle: http://jsfiddle.net/pligor/gx8qthqL/2/

    0 讨论(0)
  • 2020-11-28 21:45

    Simple elegant working solution:

    img {
      width: 600px;  /*width of parent container*/
      height: 350px; /*height of parent container*/
      object-fit: contain;
      position: relative;
      top: 50%;
      transform: translateY(-50%);
    }
    
    0 讨论(0)
  • 2020-11-28 21:46

    By setting the CSS max-width property to 100%, an image will fill the width of it's parenting element, but won’t render larger than it's actual size, thus preserving resolution.

    Setting the height property to auto maintains the aspect ratio of the image, using this technique allows static height to be overridden and enables the image to flex proportionally in all directions.

    img {
        max-width: 100%;
        height: auto;      
    }
    
    0 讨论(0)
  • 2020-11-28 21:52

    This is a very straightforward solution that I came up with after following conca's link and looking at background size. It blows the image up and then fits it centered into the outer container w/o scaling it.

    <style>
    #cropcontainer
    {
      width:  100%; 
      height: 100%; 
      background-size: 140%;
      background-position: center;
      background-repeat: no-repeat;
    }
    </style>
    
    <div id="cropcontainer" style="background-image: url(yoururl); />
    
    0 讨论(0)
提交回复
热议问题