Draw a Tree Structure With Only CSS
2016-05-26 12:49
531 查看
Sometimes you have data in a tree structure. Common candidates are hierarchies in a company, module architecture in a system or files on disk. It would be really nice to be able to generate an HTML page with this data neatly rendered so as to actually look
like a tree structure. In other words, we'll be shooting for something like this*:
![](https://two-wrongs.com/static/image/css-tree.png)
That's actually drawn with only CSS, using the pseudo-elements
We start with a simple HTML list.
With this out of the way†, let's get into the CSS. First we need to set
up the list and container so it doesn't interfere with our tree drawing.
Then we take our pseudo-elements, make sure they are empty and move them to the left of the list elements.
The
...and the vertical lines use the
The icing on the cake is that the last item on every level has just a very short line leading to it, and nothing further. This makes it look very nice.
And that's it! I think it's fascinating something like that is possible.
原文链接:https://two-wrongs.com/draw-a-tree-structure-with-only-css
like a tree structure. In other words, we'll be shooting for something like this*:
![](https://two-wrongs.com/static/image/css-tree.png)
That's actually drawn with only CSS, using the pseudo-elements
::beforeand
::after.
We start with a simple HTML list.
<div class="clt"> <ul> <li> Fruit <ul> <li> Red <ul> <li>Cherry</li> <li>Strawberry</li> </ul> </li> <li> Yellow <ul> <li>Banana</li> </ul> </li> </ul> </li> <li> Meat <ul> <li>Beef</li> <li>Pork</li> </ul> </li> </ul> </div>
With this out of the way†, let's get into the CSS. First we need to set
up the list and container so it doesn't interfere with our tree drawing.
.clt, .clt ul, .clt li { position: relative; } .clt ul { list-style: none; padding-left: 32px; }
Then we take our pseudo-elements, make sure they are empty and move them to the left of the list elements.
.clt li::before, .clt li::after { content: ""; position: absolute; left: -12px; }
The
::beforepseudo-element is used to draw the horizontal lines.
.clt li::before { border-top: 1px solid #000; top: 9px; width: 8px; height: 0; }
...and the vertical lines use the
::afterpseudo-element.
.clt li::after { border-left: 1px solid #000; height: 100%; width: 0px; top: 2px; }
The icing on the cake is that the last item on every level has just a very short line leading to it, and nothing further. This makes it look very nice.
.clt ul > li:last-child::after { height: 8px; }
And that's it! I think it's fascinating something like that is possible.
原文链接:https://two-wrongs.com/draw-a-tree-structure-with-only-css
相关文章推荐
- 如何提升我的HTML&CSS技术,编写有结构的代码
- CSS 3 中 -webkit-, -moz-, -o-, -ms- 这些私有前缀
- CSS3选择器学习笔记
- 图解CSS3制作圆环形进度条的实例教程
- CSS的margin属性在页面布局中的使用攻略
- cssViewer牛逼的chrome插件
- css四可见,部分可见和重叠半透明
- 来看看css3中的box-shadow
- css3之渐变
- Material Design-Button的新式样式初探
- gulp构建指令
- NPOI之Excel——合并单元格、设置样式、输入公式
- NPOI之Excel——合并单元格、设置样式、输入公式
- css 优先级总结
- 详解rotate3()
- css-三边框,外边距和内边距
- 使用CSS3 :nth-child(n) 選取器教學
- css:使用笔记(不断更新中...)
- css二阴影+第三维度z-index
- CSS外边距合并