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

前端 未结 14 2637
粉色の甜心
粉色の甜心 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条回答
  •  猫巷女王i
    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 :)

提交回复
热议问题