Centering image inside a div using CSS instead of jquery

后端 未结 4 1485
滥情空心
滥情空心 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:13

    Use background images.

    .photo_container {
        width: 250px;
        height: 250px;
        overflow: hidden;
        border-style: solid;
        border-color: green;
        float:left;
        margin-right:10px;
        position: relative;
    }
    .photo_container a{
       
      /* Set the background size to cover to scale the image */
      background-size: cover;
    
      /* Position the image in the center */
      background-position: 50% 50%;
    
      background-repeat: no-repeat;
    
        position:absolute;
      width: 100%;
      height: 100%;
    }
    
    
    
    
    

提交回复
热议问题