按字母顺序垂直块对 html ul/li 列表进行排序
这是我时不时遇到的一个问题,我通常尝试从后端角度解决这个问题,但想知道其他人是否找到了一个神奇的解决方案来在前端解决这个问题。
给定一个 ul/li 列表,在标记中按字母顺序提供,从 az:
<ul>
<li>Alpha</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
<li>Juliet</li>
<li>Kilo</li>
<li>Lima</li>
<li>Mike</li>
<li>November</li>
<li>Oscar</li>
<li>Papa</li>
<li>Quebec</li>
<li>Romeo</li>
<li>Sierra</li>
<li>Tango</li>
<li>Uniform</li>
<li>Victor</li>
<li>Whiskey</li>
<li>X-ray</li>
<li>Yankee</li>
<li>Zulu</li>
</ul>
通常,很容易将项目向左浮动并以块的形式在视觉上水平对它们进行排序,如下所示:
但是,要获取列,如下所示:
我总是不得不将 HTML 分解为单独的实体,例如上面示例中的四个单独的
元素。有没有一种方法可以将所有内容保留在一个 ul 列表中,而无需任何额外的标记,仅使用 CSS(无 JavaScript)来获得像上面第二张图一样的柱状外观?我的猜测是“不”,但我以前见过一些魔法,我想更明确地回答这个问题。
Here's a problem I run into every now and then, that I usually try and solve from a back end perspective, but would like to know if there's a magic solution others have found to solve this on the front end.
Given a ul/li list, provided in the markup alphabetically, from a-z:
<ul>
<li>Alpha</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
<li>Juliet</li>
<li>Kilo</li>
<li>Lima</li>
<li>Mike</li>
<li>November</li>
<li>Oscar</li>
<li>Papa</li>
<li>Quebec</li>
<li>Romeo</li>
<li>Sierra</li>
<li>Tango</li>
<li>Uniform</li>
<li>Victor</li>
<li>Whiskey</li>
<li>X-ray</li>
<li>Yankee</li>
<li>Zulu</li>
</ul>
Typically, it's very easy to float the items left and sort them visually horizontal in blocks, like such:
However, to get columns, like this:
I've always had to break the HTML up into separate entities, such as four separate <ul>
elements in the above example.
Is there a way to keep it all in one ul list without any additional markup, using just CSS (no JavaScript) to get a columnar look like the second image above? My guess is "no," but I've seen some magic before, and I'd like to answer this more definitively.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
恐怕还没有。
CSS3 有一些不错的功能,例如 http://www.quirksmode.org/css/multicolumn。 html 但 Internet Explorer 不支持它,我不知道 IE9 中的支持如何(根据 Microsoft 目前测试版中不存在)
Not yet I´m afraid.
CSS3 has some nice features, see for example http://www.quirksmode.org/css/multicolumn.html but Internet Explorer does not support it and I don´t know how the support in IE9 will be (according to Microsoft non-existent in the beta at the moment)
您可以使用CSS3多列布局及其polyfill:https://github。 com/gryzzly/CSS-Multi-column-Layout-Module-Polyfill
You can use CSS3 Multicolumn Layouts, and its polyfill: https://github.com/gryzzly/CSS-Multi-column-Layout-Module-Polyfill
我今天需要它并写了这个 python 等效项。您可以使用本机 javascript 编写。
https://github.com/berkantaydin/vertical-alphabetical-sorting-with-columns
I needed it today and wrote this python equivalent. You can write with native javascript.
https://github.com/berkantaydin/vertical-alphabetical-sorting-with-columns
如果您在
PHP
中执行此操作,则可以使用简单的计数器和%
。我已经成功地在WordPress
中实现了这一点,获取了自定义分类法列表,如下所示:您还可以循环遍历数组并实现相同的效果。输出看起来像这样:
If you're doing it in
PHP
you can use a simple counter and%
. I've managed to accomplish this inWordPress
getting a list of custom taxonomies like so:You can also loop through an array and acheive the same thing. Output looks something like: