css: fit rounded (circle) image (do not stretch it)

后端 未结 5 768
广开言路
广开言路 2020-12-29 04:50

For example I have such images:

and css:

.company-header-avatar{
    width: 60px;
    height: 60px;
    -webkit-border-radius: 60px;
    -w         


        
5条回答
  •  天命终不由人
    2020-12-29 05:43

    if you use jquery than check this jsfiddle. : http://jsfiddle.net/73h7try9/2/

    you js should:

    $('.image_cover').each(function(){
    var imageWidth = $(this).find('img').width();
    var imageheight = $(this).find('img'). height();
      if(imageWidth > imageheight){
        $(this).addClass('landscape');
      }else{
        $(this).addClass('potrait');
      }
    })
    

    your css should :

    .company-header-avatar{
        width: 100%;
        height: auto;
        -webkit-background-clip: padding-box;
        -moz-background-clip: padding;
        background-clip: padding-box;
     }
     .landscape .company-header-avatar{ 
        width: auto;
        height: 100%;
     }
     .potrait .company-header-avatar{ 
        width: 100%;
        height: auto;
     }
     .image_cover{ 
        width:60px; 
        height:60px; 
        border-radius:50%; 
        overflow:hidden;
        float: left;    
        margin: 7px 0 0 5px;
     }
    

    your html should:

     

提交回复
热议问题