繁体   English   中英

如何使用纯CSS获取HTML树

[英]How to get a tree in HTML using pure CSS

我正在尝试按照本教程 ,这是我的代码到目前为止。

本教程的结尾显示分支中的最后一个节点后面没有任何垂直条。 但我无法让它以这种方式运作! 如果我做错了什么想法,或者教程遗漏了什么!

我甚至尝试过:last-child伪类,如教程中所示,但得到了相同的结果。

错误的CSS树

这是尝试仅使用伪元素和边框:

ul, li{     
    position: relative;    
}

/* chop off overflowing lines */
ul{
    overflow: hidden;
}

li::before, li::after{
    content: '';
    position: absolute;
    left: 0;
}

/* horizontal line on inner list items */
li::before{
    border-top: 1px solid #333;
    top: 10px;
    width: 10px;
    height: 0;
}

/* vertical line on list items */    
li::after{
    border-left: 1px solid #333;
    height: 100%;
    width: 0px;
    top: -10px;
}

/* lower line on list items from the first level 
   because they don't have parents */
.tree > li::after{
    top: 10px;
}

/* hide line from the last of the first level list items */
.tree > li:last-child::after{
    display:none;
}

演示 (编辑)

遗憾的是,伪类在即将发布的CSS3规范中定义,目前很少有Web浏览器支持它。

它是在教程结束时编写的。 也许这就是它不起作用的原因。

我相信我已修好它: https//github.com/gurjeet/CSSTree/pull/1

我修改了CSS以删除背景并将边距更改为填充。

ul.tree, ul.tree ul {
    list-style-type: none;
    margin:0;
    padding:0;
}

ul.tree ul {
    padding-left: 1em;
    background:  url(vline.png) repeat-y;
}

ul.tree li {
    margin:0;
    padding: 0 1.2em;
    line-height: 20px;
    background: url(node.png) no-repeat;
    color: #369;
    font-weight: bold;
}

/* The #fff color background is to hide the previous background image*/
ul.tree li.last {
    background: #fff url(lastnode.png) no-repeat;
}

ul.tree ul:last-child {
    background: none;
}

谢谢大家,有用的答案让我读了更多,最后我读了这篇文章并删除了对JavaScript的所有依赖,并使用nth-last-of-type伪选择器将特殊背景应用于最后的li项目列表(忽略最后一个li之后的ul)。

最终的代码在这里 我会接受我自己的答案,除非有人指出它有些问题。 (在这个阶段,我认为与旧浏览器的兼容性并不重要。)

感谢@Aram的回答。 @OneTrickPony,你的答案超过了这个菜鸟头:)我确信它做对了,但对我来说有点太复杂了。

<style type="text/css">
/* ul[class=tree] and every ul under it loses all alignment, and bullet
 * style.
 */
ul.tree, ul.tree ul {
    list-style-type: none;
    margin:0;
    padding:0;
}

/* Every ul under ul[class=tree] gets an indent of 1em, and a background
 * image (vertical line) applied to all nodes under it (repeat-y)
 */
ul.tree ul {
    padding-left: 1em;
    background:  url(vline.png) repeat-y;
}

/* ... except the last ul child in every ul; so no vertical lines for
 * the children of the last ul
  */
ul.tree ul:last-child {
    background: none;
}

/* Every li under ul[class=tree]
 *  - gets styling to make it bold and blue, and indented.
 *  - gets a background image (tilted T), to denote that its a node
 *  - sets height to match the height of background image
 */
ul.tree li {
    margin:0;
    padding: 0 1.2em;
    background: url(node.png) no-repeat;
    line-height: 20px;
    color: #369;
    font-weight: bold;
}

/*  The last li gets a different background image to denote it as the
 *  end of branch
 */
ul.tree li:nth-last-of-type(1) {
    background: url(lastnode.png) no-repeat;
}

暂无
暂无

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

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