Center Oversized Image in Div

前端 未结 12 1638
予麋鹿
予麋鹿 2020-11-27 08:52

I have been trying to sort out how to center an oversized image within a div using css only.

We are using a fluid layout, so the width of the image containers varies

12条回答
  •  旧巷少年郎
    2020-11-27 09:44

    Try something like this. This should center any huge element in the middle vertically and horizontally with respect to its parent no matter both of their sizes.

    .parent {
        position: relative;
        overflow: hidden;
        //optionally set height and width, it will depend on the rest of the styling used
    }
    
    .child {
        position: absolute;
        top: -9999px;
        bottom: -9999px;
        left: -9999px;
        right: -9999px;
        margin: auto;
    
    }
    

提交回复
热议问题