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

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

    What about this?

    http://jsfiddle.net/cHTVd/1/

    enter image description here

    You have to set display: inline-block for the container too:

    body { text-align: center; }
    
    #container { 
        width: 250px; 
        border: solid green 3px; 
        display: inline-block; 
        text-align: left; 
    }
    
    .tile { width: 100px; 
        border: solid red 3px;
        display: inline-block;
        margin: 8px;
    }
    

    EDIT: Giving container relative width is easy - http://jsfiddle.net/cHTVd/3/

    I am afraid that "reverse justify" would have to be done with JS. CSS text-align has only four values: left | right | center | justify. It's trivial to change it to justify - http://jsfiddle.net/cHTVd/4/. For the "reverse justify" you would probably need some javascript work similar to this: http://jsfiddle.net/yjcr7/2/.

提交回复
热议问题