CSS: how to vertically and horizontally align an image?

后端 未结 7 944
青春惊慌失措
青春惊慌失措 2020-12-31 12:27

My page has space for an image that can be, say, a maximum 100x100 image. Users can upload any image dimension and the web application will resize it, maintaining aspect rat

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 12:50

    I know this is and old question already answered but now you can use flex

    CSS

    .container {
        display: flex;
        justify-content: center;
        align-items: center;
        border: 1px solid;
        width: 50vw;
        height: 50vw;
    }
    .container img
    {
        max-width:100%;
        max-height:100%;
    }
    

    Fiddle Demo

    you can also customize the size of your container, some browser may not support flex you can check it here caniuse

提交回复
热议问题