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

前端 未结 14 2526
粉色の甜心
粉色の甜心 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:34

    Had the same issue. The problem for me was that the height property was also already defined elsewhere, fixed it like this:

    .img{
        max-width: 100%;
        max-height: 100%;
        height: inherit !important;
    }
    
    0 讨论(0)
  • 2020-11-28 21:35

    I use this for a rectangular container with height and width fixed, but with images of different sizes.

    img {
      max-width: 95%;
      max-height: 15em;
      width: auto !important;
    }
    
    0 讨论(0)
  • 2020-11-28 21:37

    Nowadays one can use vw and vh units, which represent 1% of the viewport's width and height respectively.

    https://css-tricks.com/fun-viewport-units/

    So, for example:

    img {
      max-width: 100vw;
      max-height: 100vh;
    }
    

    ... will make the image as wide as tall as possible, maintaining aspect ratio, but without being wider or higher than 100% of the viewport.

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

    Its best to use auto on the dimension that should respect the aspect ratio. If you do not set the other property to auto, most browsers nowadays will assume that you want to respect the aspect ration, but not all of them (IE10 on windows phone 8 does not, for example)

    width: 100%;
    height: auto;
    
    0 讨论(0)
  • 2020-11-28 21:39

    If you only define one dimension on an image the image aspect ratio will always be preserved.

    Is the issue that the image is bigger/taller than you prefer?

    You could put it inside a DIV that is set to the maximum height/width that you want for the image, and then set overflow:hidden. That would crop anything beyond what you want.

    If an image is 100% wide and height:auto and you think it's too tall, that is specifically because the aspect ratio is preserved. You'll need to crop, or to change the aspect ratio.

    Please provide some more information about what you're specifically trying to accomplish and I'll try to help more!

    --- EDIT BASED ON FEEDBACK ---

    Are you familiar with the max-width and max-height properties? You could always set those instead. If you don't set any minimum and you set a max height and width then your image will not be distorted (aspect ratio will be preserved) and it will not be any larger than whichever dimension is longest and hits its max.

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

    Simple solution:

    min-height: 100%;
    min-width: 100%;
    width: auto;
    height: auto;
    margin: 0;
    padding: 0;
    

    By the way, if you want to center it in a parent div container, you can add those css properties:

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    

    It should really work as expected :)

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