Keep unknown number of divs centered, max 4 per row

佐手、 提交于 2019-12-06 02:12:56

Give width & text-align to your .row div

.row{
    display:block;
    max-width:1000px;
    text-align:center;
    margin:0 auto;
}

Then make your circles inline block elements

.small-3{
    display:inline-block;
    width:200px;
    margin:0 25px 25px;
}

Width of .row div should be equal to resulting width of 4 cirlce. In above example it is 1000 since (200 + 25 + 25)*4 = 1000. Use your width here.

You have to set the width of the divs with the circles, make them inline-blocks and center the parent div.

#circle4 {
    text-align: center;
}

#circle4 > div {
    display: inline-block;
    width: 240px;
    height: 240px;

    /**
     * Update
     */
    float: left;
}

#circle4 > div:nth-of-type(4n) {
    clear: right;
}

In order to keep 4 items in one row maximum, you have to give them a % width so only 4 elements can fit horizontaly like this :

CSS :

#circle4{
    text-align: center;
}

.small-3{
    display:inline-block;
    width:23%;
}

You can't fit more that 4 23% width items in one row because :

4*23 = 92%

which fits in 100% and

5 * 23 = 115% 

which can't fit in 100%


If you want to go one step further and make the content of your divs (images) responsive as well, you should add this CSS :

.grow img{
    width:100%;
    height:auto;
}

DEMO

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!