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

前端 未结 9 1904
爱一瞬间的悲伤
爱一瞬间的悲伤 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:20

    Try this:

    .tiles: { 
      display: inline-block;
      margin: 0 auto;
      max-width: 90%;
    }
    

    No verticall margin, auto margin for horizontal sides!

    0 讨论(0)
  • 2020-11-30 11:24

    Looks like there is solution (though it is described in Russian and I haven't found a translation).
    Demo can be seen here.

    0 讨论(0)
  • 2020-11-30 11:25

    Unless you want to use Javascript you could use the media query (lots of them):

    #parent{ width: 100px; margin: 0 auto;padding:0;}
    .tile{width: 80px; float:left;padding:10px;outline:2px dashed red;}
    @media screen and (max-width:200px)
    @media screen and (min-width:201px) and (max-width:300px){
        #parent{ width: 200px;}
    }
    @media screen and (min-width:301px) and (max-width:400px){
        #parent{ width: 300px;}
    }
    @media screen and (min-width:401px){
        #parent{ width: 400px;}
    }
    

    The problem is that you need to know how many of the tiles fit into the container to set a tight fitting width to the container, but that is a bottom up information and not how cascading works. If you want a more elegant solution you need to use JS on resize events, calculate how many boxes fit into one line and set the width of the container.

    0 讨论(0)
提交回复
热议问题