UL+CSS for grid layout

2019-03-15 01:15发布

I have a server-generated html like:

<ul>
    <li><!-- few nested elements that form a block --></li>
    <li><!-- few nested elements that form anaother block --></li>
    <li><!-- etc, X times --></li>
</ul>

All blocks have known width 200px and unknown height. I want <li> to be arranged in table-like fashion like this:

alt text

What I have for now is following css:

li {
    display: block;
    width: 200px;
    float: left;
    margin: 10px;
}

All is fine except that columns don't get equal height. And in example above block #4 “snatch” at #1 and the result isn't what I'm trying to achieve:

alt text

Is there any pure-CSS cross-browser way that will allow grid layout I want and will not enforce markup change?

标签: html css layout
3条回答
霸刀☆藐视天下
2楼-- · 2019-03-15 01:17

In your example you seem to want to give each row the same height of the largest li in that row. If this height is variable what you want is only possible with nth-child:

li:nth-child(3n+0) { clear: left; }
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-15 01:19

Option 1: Give them explicit heights

Options 2: Use nth-child (which has limited support)

查看更多
SAY GOODBYE
4楼-- · 2019-03-15 01:40

Inline blocks could perhaps be useful here.

查看更多
登录 后发表回答