vertical align center image in fixed size div

后端 未结 6 1873
别那么骄傲
别那么骄傲 2020-12-24 14:50

I have a div which is 145px X 145px. I have an img inside this dive. The img could be of any size (longest side being 130px). I would like the image to be centered verticall

6条回答
  •  长情又很酷
    2020-12-24 15:07

    Using the line-height property solved the problem for me.

    Reference: vertical-align image in div

    HTML:

    img_alt

    CSS:

    .img_thumb {
        float: left;
        height: 120px;
        margin-bottom: 5px;
        margin-left: 9px;
        position: relative;
        width: 147px;
        background-color: rgba(0, 0, 0, 0.5);
        border-radius: 3px;
        line-height:120px;
        text-align:center;
    }
    
    .img_thumb img {
        vertical-align: middle;
    }
    

提交回复
热议问题