JSFiddle - React, Tailwind, and code Playground
HTML
<div id="container">
<div id="tree"></div>
</div>
CSS
/* Thee */
body{background:#fff;}
* {
padding: 0;
}
.tree ul {
padding-top: 20px; position: relative;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
float: left; text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li::before, .tree li::after{
content: '';
position: absolute; top: 0; right: 50%;
border-top: 1px solid #ccc;
width: 50%; height: 20px;
}
.tree li::after{
right: auto; left: 50%;
border-left: 1px solid #ccc;
}
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}
.tree li:only-child{ padding-top: 0;}
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
.tree li:last-child::before{
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
.tree ul ul::before{
content: '';
position: absolute; top: 0; left: 50%;
border-left: 1px solid #ccc;
width: 0; height: 20px;
}
.tree li a{
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #666;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li a:hover, .tree li a:hover+ul li a {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
.tree li a:hover+ul li::after,
.tree li a:hover+ul li::before,
.tree li a:hover+ul::before,
.tree li a:hover+ul ul::before{
border-color: red;
}
JavaScript
'use strict';
/*
var treeDataJs = {
name : "TreeData.js",
author : "Raphael Amorim",
email : "[email protected]",
github : "https://github.com/raphamorim/treeStructure"
};
*/
/* Receive data and create tree */
function TreeData (data, select) {
var main = document.querySelector(select);
var treecanvas = document.createElement('div');
treecanvas.className = 'tree';
var treeCode = buildTree(data, Object.keys(data)[0]);
treecanvas.innerHTML = treeCode;
main.appendChild(treecanvas);
}
/* Recursive function to build tree structure :) */
function buildTree (obj, node) {
var treeString = "<li><a href='#'>" + obj[node].value + "</a>";
var sons = [];
for (var i in obj) {
if (obj[i].parent == node)
sons += i;
}
if (sons.length > 0) {
treeString += "<ul>";
for (var i in sons) {
treeString += buildTree(obj, sons[i]);
}
treeString += "</ul>";
}
return treeString;
}
var tree = {
father : {value : "徳川家家系図", parent : ""},
a : {value : "家康", parent : "father"},
b : {value : "延安", parent : "a"},
c : {value : "秀忠", parent : "a"},
d : {value : "忠長", parent : "c"},
e : {value : "秀康", parent : "a"},
f : {value : "家光", parent : "c"},
g : {value : "家綱", parent : "f"},
h : {value : "千姫", parent : "c"},
i : {value : "初姫", parent : "c"},
j : {value : "綱重", parent : "f"},
k : {value : "家宣", parent : "j"},
p : {value : "清武", parent : "j"},
l : {value : "綱吉", parent : "g"},
m : {value : "家宣", parent : "l"},
n : {value : "徳松", parent : "l"},
o : {value : "鶴姫", parent : "l"},
q : {value : "家継", parent : "m"}
};
TreeData(tree, "#tree");