【问题标题】:How to get a tree in HTML using pure CSS如何使用纯 CSS 在 HTML 中获取树
【发布时间】:2013-02-17 14:43:28
【问题描述】:

我正在尝试关注这个tutorial,到目前为止,这里是my code

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

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

【问题讨论】:

标签: javascript html css tree


【解决方案1】:

这是一个只使用伪元素和边框的尝试:

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;
}

demo(已编辑)

【讨论】:

  • 对您的演示稍作修改。如果您注意到,当最后一个元素不在第一或第二缩进级别时,如现在所示,有多余的行see here。如果您在此处查看底部的新 CSS,您可以看到它在没有额外行的情况下如何反映在结果中:dabblet.com/gist/6878696
  • @Joseph margin-left: 0 0 0 10px => margin: 0 0 0 10px?
【解决方案2】:

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

它写在教程的最后。也许这就是它不起作用的原因。

【讨论】:

    【解决方案3】:

    我相信我已经解决了: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;
    }
    

    【讨论】:

      【解决方案4】:

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

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

      感谢@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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-26
        • 2021-08-21
        • 1970-01-01
        • 2020-12-27
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        相关资源
        最近更新 更多