我有一个按字母顺序排列的国家列表,如:
Albania
Andorra
Armenia
Austria
Azerbaijan
Belarus
Belgium
Bosnia and Herzegovina
Bulgaria
Croatia
Cyprus
Czech Republic
Denmark
Estonia
Finland
France
Georgia
Germany
Greece
...
我在列表中显示它们:
<ul class="mylist">
<li>Albania</li>
<li>Andorra</li>
<li>Armenia</li>
<li>Austria</li>
<li>Azerbaijan</li>
...
</ul>
列表在大屏幕上显示4列:
ul.mylist li {
float: left;
width: 25%;
}
在小型显示器上有两个:
ul.mylist li {
float: left;
width: 50%;
}
这将导致类似列表的结果(大显示):
Albania Andorra Armenia Austria
Azerbaijan Belarus Belgium Bosnia and Herzegovina
Bulgaria Croatia...
好的,现在到棘手的部分:显示的国家的计数可能会因筛选选项而有所不同。我想用另一种方式(从上到下,从左到右)对这些国家进行排序,如下所示:
Albania Bosnia and Herzegovina Finland [4th col]
Andorra Bulgaria France ...
Armenia Croatia Georgia
Austria Cyprus Germany
Azerbaijan Czech Republic ...
Belarus Denmark
Belgium Estonia
如果css将列表折叠成小屏幕上的两列,怎么做呢?
最佳答案
为此使用CSS3 multi-column layout。Browser support is questionable所以请使用供应商前缀。
ul {
margin: 0;
padding: 0;
}
li {
display: inline-block;
width: 100%;
}
/* default */
ul {
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
}
/* < 800px */
@media screen and (max-width: 799px) {
ul {
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
}
}
/* < 400px */
@media screen and (max-width: 399px) {
ul {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
}
<ul>
<li>Albania</li>
<li>Andorra</li>
<li>Armenia</li>
<li>Austria</li>
<li>Azerbaijan</li>
<li>Belarus</li>
<li>Belgium</li>
<li>Bosnia and Herzegovina</li>
<li>Bulgaria</li>
<li>Croatia</li>
<li>Cyprus</li>
<li>Czech Republic</li>
<li>Denmark</li>
<li>Estonia</li>
<li>Finland</li>
<li>France</li>
<li>Georgia</li>
<li>Germany</li>
<li>Greece</li>
</ul>