Vertically align an image inside a div with responsive height

后端 未结 10 2009
遇见更好的自我
遇见更好的自我 2020-11-22 02:47

I have the following code which sets up a container which has a height that changes with the width when the browser is re-sized (to maintain a square aspect ratio).

10条回答
  •  面向向阳花
    2020-11-22 03:35

    With flexbox this is easy:

    FIDDLE

    Just add the following to the image container:

    .img-container {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        display: flex; /* add */
        justify-content: center; /* add to align horizontal */
        align-items: center; /* add to align vertical */
    }
    

提交回复
热议问题