How to get tiles centered and left-justified at the same time

前端 未结 9 1910
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 10:33

I have a container of tiles (or divs) and I want the container to be centered, while the tiles are left justified in the container.

so if the window is small:

<
9条回答
  •  遥遥无期
    2020-11-30 11:04

    Answer the same like Danield's, but in SCSS:

    *
    {
        margin:0;padding:0;
    }
    
    $item-width:200px;
    $item-height:100px;
    $margin: 15px;
    $min-cols:2;
    $max-cols:6; //set an upper limit of how may columns you want
                 //to write the media queries forss to css
    
    @for $i from $min-cols to $max-cols + 1 {
      $index-width: $i*$item-width;
      @media (min-width:$index-width) {
        #content {
          width: $index-width;
        }
      }
    }
    
    
    #content {
        margin:0 auto;
        overflow: auto;
        min-width: $min-cols * $item-width;
        max-width: $max-cols * $item-width;
        display: block;
        list-style:none;
        background: aqua;
    }
    .box {
        float: left;
        height: $item-height - 2 *$margin;
        width: $item-width - 2*$margin;
        margin:$margin;
        background-color:blue;
    }
    

    It generates the same CSS like his solution.

    Working codepen example here.

提交回复
热议问题