Force bootstrap responsive image to be square

前端 未结 5 1216
南笙
南笙 2020-12-12 20:13

Images are created in a loop with:

5条回答
  •  醉酒成梦
    2020-12-12 20:49

    You can do this :

    1. wrap the image in a container with padding-bottom:100%; overflow:hidden; position:relative
    2. position the image absolutely inside that container.

    FIDDLE

    Explanation :

    Padding top/bottom (like margin top/bottom) is calculated according to the width of parent element.As the .image div has the same width as its parent, setting padding-bottom:100%; give it the same height as its width so it is square (its content needs to be absolutely positioned or floated so it doesn't change the parent's size).

    HTML:

    
    

    CSS:

    .image{
        position:relative;
        overflow:hidden;
        padding-bottom:100%;
    }
    .image img{
        position:absolute;
    }
    

提交回复
热议问题