How to center and crop an image to always appear in square shape with CSS?

后端 未结 9 1351
[愿得一人]
[愿得一人] 2020-12-02 06:19

I need to always crop a random-sized image to a square 160x160 using only CSS. The images should stay centered when cropped.

My markup should be:

<         


        
9条回答
  •  我在风中等你
    2020-12-02 07:05

    div { width: 200px; height: 200px; overflow: hidden; margin: 10px; position: relative; } .crop { position: absolute; left: -100%; right: -100%; top: -100%; bottom: -100%; margin: auto; height: auto; width: auto; }

    http://jsfiddle.net/J7a5R/56/

提交回复
热议问题