简体   繁体   English

使用css从ul li建表

[英]use css to build a table from ul li

I have this piece of html code.我有这段 html 代码。 This is a list of type UL/LI with span classes inside.这是一个 UL/LI 类型的列表,里面有 span 类。 I want it to be displayed as a table with boarders and not a list with bullets.我希望它显示为带有寄宿生的表格而不是带有项目符号的列表。

 <ul class="list"> <li> <span class="category">category1</span> <span class="name">Item Title1</span> <span class="price">40 EUR</span> <span class="url"><a href="url1">link to article</a></span> </li> <li> <span class="category">category2</span> <span class="name">item title2</span> <span class="price">55 EUR</span> <span class="url"><a href="url2">link to article</a></span> </li> </ul>

I have tried to write this css but the result is not good.我试过写这个 css 但结果不好。

 .list ul { width: 450px; position: relative; list-style-type: none; margin: 0; padding: 0; } .list ul:before, ul:after { text-align: center; display: block; border: 1px solid black; border-bottom: 0; width: 48%; } .list ul:before { content: 'col1'; border-right: 0; } .list ul:after { content: 'col2'; position: absolute; top: 0; left: 48%; margin-left: 1px; } .list li { text-align: right; width: 48%; float: left; border: 1px solid black; margin-bottom: -1px; } .list li:nth-child(even) { margin-left: -1px; }
 <ul class="list"> <li> <span class="category">category1</span> <span class="name">Item Title1</span> <span class="price">40 EUR</span> <span class="url"><a href="url1">link to article</a></span> </li> <li> <span class="category">category2</span> <span class="name">item title2</span> <span class="price">55 EUR</span> <span class="url"><a href="url2">link to article</a></span> </li> </ul>

One way to achieve this effect is to use CSS Grid .实现这种效果的一种方法是使用CSS Grid

Right at the very start (and this is normal whenever you want to reformat a list) it's a good idea to begin by applying the following styles to the parent <ul> / <ol> :一开始(当您想重新格式化列表时这是正常的),最好首先将以下样式应用于父级<ul> / <ol>

  • margin-left: 0
  • padding-left: 0
  • list-style-type: none

This acts as a very basic reset to the way either of these types of list are normally displayed.这是对这些类型列表中的任何一种通常显示方式的非常基本的重置。

Working Example:工作示例:

 .list { display: grid; grid-template-columns: 180px 180px; width: 360px; margin-left: 0; padding-left: 0; border: 1px solid rgb(0, 0, 0); list-style-type: none; } .list li { width: 180px; border: 1px solid rgb(0, 0, 0); box-sizing: border-box; } .list li span { display: block; padding: 6px 9px; border: 1px solid rgb(0, 0, 0); } .list li span.category { font-weight: bold; text-align: center; text-transform: uppercase; }
 <ul class="list"> <li> <span class="category">Category 1</span> <span class="name">Item Title 1</span> <span class="price">40 EUR</span> <span class="url"><a href="url1">Link to Article</a></span> </li> <li> <span class="category">Category 2</span> <span class="name">Item Title 2</span> <span class="price">55 EUR</span> <span class="url"><a href="url2">Link to Article</a></span> </li> </ul>

You are styling a list inside an element with the list class instead of the list itself here:您在此处使用列表类而不是列表本身为元素内的列表设置样式:

.list ul {
    width: 450px;           
    position: relative;

    list-style-type: none;
    margin: 0;
    padding: 0;
}

If you add a .list element outside the list, or refactor the style to point the list itself you'll be pointed in the right direction.如果您在列表外添加 .list 元素,或重构样式以指向列表本身,您将被指向正确的方向。

This piece, for example, will work with the following modification:例如,这件作品将进行以下修改:

.list {
    width: 450px;           
    position: relative;

    list-style-type: none;
    margin: 0;
    padding: 0;
}

Or you can also alter the html:或者您也可以更改 html:

<div class="list">
<ul>
<li>
<span class="category">category1</span>
<span class="name">Item Title1</span> 
<span class="price">40 EUR</span>
<span class="url"><a href="url1">link to article</a></span>
</li>   
<li>
<span class="category">category2</span>
<span class="name">item title2</span> 
<span class="price">55 EUR</span>
<span class="url"><a href="url2">link to article</a></span>
</li>   
</ul>
</div>

 <table border="1"> <tr> <td class="category">category1</td> <td class="name">Item Title1</td> <td class="price">40 EUR</td> <td class="url"><a href="url1">link to article</a></td> </tr> <tr> <td class="category">category2</td> <td class="name">item title2</td> <td class="price">55 EUR</td> <td class="url"><a href="url2">link to article</a></td> </tr> </table>

I didn't manage to build a correct table from this html piece.我没有设法从这个 html 片段构建一个正确的表格。 Instead I generated a table and managed to use list.js to filter the content.相反,我生成了一个表格并设法使用 list.js 来过滤内容。 This achieved what I wanted to do.这实现了我想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM