CSS grid wrapping

前端 未结 5 913
借酒劲吻你
借酒劲吻你 2020-12-04 04:53

Is it possible to make a CSS grid wrap without using media queries?

In my case, I have a non-deterministic number of items that I want placed in a grid and I want th

5条回答
  •  被撕碎了的回忆
    2020-12-04 05:32

    Here's my attempt. Excuse the fluff, I was feeling extra creative.

    My method is a parent div with fixed dimensions. The rest is just fitting the content inside that div accordingly.

    This will rescale the images regardless of the aspect ratio. There will be no hard cropping either.

    body {
        background: #131418;
        text-align: center;
        margin: 0 auto;
    }
    
    .my-image-parent {
        display: inline-block;
        width: 300px;
        height: 300px;
        line-height: 300px; /* Should match your div height */
        text-align: center;
        font-size: 0;
    }
    
    /* Start demonstration background fluff */
        .bg1 {background: url(https://unsplash.it/801/799);}
        .bg2 {background: url(https://unsplash.it/799/800);}
        .bg3 {background: url(https://unsplash.it/800/799);}
        .bg4 {background: url(https://unsplash.it/801/801);}
        .bg5 {background: url(https://unsplash.it/802/800);}
        .bg6 {background: url(https://unsplash.it/800/802);}
        .bg7 {background: url(https://unsplash.it/802/802);}
        .bg8 {background: url(https://unsplash.it/803/800);}
        .bg9 {background: url(https://unsplash.it/800/803);}
        .bg10 {background: url(https://unsplash.it/803/803);}
        .bg11 {background: url(https://unsplash.it/803/799);}
        .bg12 {background: url(https://unsplash.it/799/803);}
        .bg13 {background: url(https://unsplash.it/806/799);}
        .bg14 {background: url(https://unsplash.it/805/799);}
        .bg15 {background: url(https://unsplash.it/798/804);}
        .bg16 {background: url(https://unsplash.it/804/799);}
        .bg17 {background: url(https://unsplash.it/804/804);}
        .bg18 {background: url(https://unsplash.it/799/804);}
        .bg19 {background: url(https://unsplash.it/798/803);}
        .bg20 {background: url(https://unsplash.it/803/797);}
    /* end demonstration background fluff */
    
    .my-image {
        width: auto;
        height: 100%;
        vertical-align: middle;
        background-size: contain;
        background-position: center;
        background-repeat: no-repeat;
    }

提交回复
热议问题