Centering image inside a div using CSS instead of jquery

后端 未结 4 1489
滥情空心
滥情空心 2021-01-19 17:33

I\'m trying to fit any sized image inside a (always) square div, and retain the aspect when the size of the parent div changes. Here\'s what I\'m doing:

CSS:

<
4条回答
  •  悲哀的现实
    2021-01-19 18:05

    You could do something like this:

    http://jsfiddle.net/vL95x81o/5/

    
    
    .photo_container { width: 250px; height: 250px; overflow: hidden; border-style: solid; border-color: green; float:left; margin-right:10px; position: relative; } .photo { height: 100%; width: 100%; } .photo--1 { background: url('https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/ECurtis.jpg/224px-ECurtis.jpg') no-repeat center center; background-size: cover; } .photo--2 { background: url('https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Altja_j%C3%B5gi_Lahemaal.jpg/220px-Altja_j%C3%B5gi_Lahemaal.jpg') no-repeat center center; background-size: cover; }

    I have changed the imgs to divs with background images and used background position cover with the image being centered.

提交回复
热议问题