使用 CSS3 伪元素格式化图表
Using CSS3 Pseudo elements to Format a Chart
我正在使用无序列表构建祖先图表,但我无法让它在屏幕上正确显示。我开始使用来自 CSS3 Family Tree 的代码,尽管名称如此,但生成的是组织结构图而不是家谱。
出于测试的目的,我有 HTML 和 CSS 这样的:
.tree ul {
padding-top: 20px;
position: relative;
}
.tree li {
display: inline-block;
white-space: nowrap;
vertical-align: top;
margin: 0 -2px 0 -2px;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
}
/*We will use ::before and ::after to draw the connectors*/
.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;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after,
.tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
padding-top: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before,
.tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.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;
}
/*Time to add downward connectors from parents*/
.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;
}
<div class="tree">
<ul>
<li>
<a href="#">Self</a>
<ul>
<li>
<a href="#">Father</a>
<ul>
<li><a href="#">Grandfather</a>
</li>
<li>
<a href="#">Grandmother</a>
</li>
</ul>
</li>
<li>
<a href="#">Mother</a>
<ul>
<li><a href="#">Grandfather</a>
</li>
<li>
<a href="#">Grandmother</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS 几乎直接来自 CSS3 家谱。
这会生成如下所示的图表
但我需要倒过来,“自己”在最下面,最早的一代在最上面
我可以通过反转列表结构来反转方框,但我找不到修改 CSS 让连接器看起来正确的方法。
有没有办法修改::before和::after伪元素来得到我想要的?还是我的做法完全错误?
(也可以把“自己”放在左边,最早的一代放在右边,这样会更好。)
您可以重新排列 HTML
,并进行一些 css
调整(即交换顶部和底部填充、交换顶部和底部位置并更改边框半径以匹配新排列)有效:
.tree ul {
padding: 0 0 20px;
position: relative;
}
.tree li {
display: inline-block;
white-space: nowrap;
vertical-align: bottom;
margin: 0 -2px 0 -2px;
text-align: center;
list-style-type: none;
position: relative;
padding: 0 5px 20px 5px;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before,
.tree li::after {
content: '';
position: absolute;
bottom: 0;
right: 50%;
border-bottom: 1px solid #ccc;
width: 50%;
height: 20px;
}
.tree li::after {
right: auto;
left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after,
.tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
padding: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before,
.tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before {
border-right: 1px solid #ccc;
border-radius: 0 0 5px 0;
-webkit-border-radius: 0 0 5px 0;
-moz-border-radius: 0 0 5px 0;
}
.tree li:first-child::after {
border-radius: 0 0 0 5px;
-webkit-border-radius: 0 0 0 5px;
-moz-border-radius: 0 0 0 5px;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before {
content: '';
position: absolute;
bottom: 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;
}
<div class="tree">
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#">Grandfather</a></li>
<li><a href="#">Grandmother</a></li>
</ul>
<a href="#">Father</a>
</li>
<li>
<ul>
<li><a href="#">Grandfather</a></li>
<li><a href="#">Grandmother</a></li>
</ul>
<a href="#">Mother</a>
</li>
</ul>
<a href="#">Self</a>
</li>
</ul>
</div>
此外,这里有一个 jsfiddle,其中包含来自代码播放器网站的(反向)原始树。
我正在使用无序列表构建祖先图表,但我无法让它在屏幕上正确显示。我开始使用来自 CSS3 Family Tree 的代码,尽管名称如此,但生成的是组织结构图而不是家谱。
出于测试的目的,我有 HTML 和 CSS 这样的:
.tree ul {
padding-top: 20px;
position: relative;
}
.tree li {
display: inline-block;
white-space: nowrap;
vertical-align: top;
margin: 0 -2px 0 -2px;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
}
/*We will use ::before and ::after to draw the connectors*/
.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;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after,
.tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
padding-top: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before,
.tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.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;
}
/*Time to add downward connectors from parents*/
.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;
}
<div class="tree">
<ul>
<li>
<a href="#">Self</a>
<ul>
<li>
<a href="#">Father</a>
<ul>
<li><a href="#">Grandfather</a>
</li>
<li>
<a href="#">Grandmother</a>
</li>
</ul>
</li>
<li>
<a href="#">Mother</a>
<ul>
<li><a href="#">Grandfather</a>
</li>
<li>
<a href="#">Grandmother</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS 几乎直接来自 CSS3 家谱。
这会生成如下所示的图表
但我需要倒过来,“自己”在最下面,最早的一代在最上面
我可以通过反转列表结构来反转方框,但我找不到修改 CSS 让连接器看起来正确的方法。
有没有办法修改::before和::after伪元素来得到我想要的?还是我的做法完全错误?
(也可以把“自己”放在左边,最早的一代放在右边,这样会更好。)
您可以重新排列 HTML
,并进行一些 css
调整(即交换顶部和底部填充、交换顶部和底部位置并更改边框半径以匹配新排列)有效:
.tree ul {
padding: 0 0 20px;
position: relative;
}
.tree li {
display: inline-block;
white-space: nowrap;
vertical-align: bottom;
margin: 0 -2px 0 -2px;
text-align: center;
list-style-type: none;
position: relative;
padding: 0 5px 20px 5px;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before,
.tree li::after {
content: '';
position: absolute;
bottom: 0;
right: 50%;
border-bottom: 1px solid #ccc;
width: 50%;
height: 20px;
}
.tree li::after {
right: auto;
left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after,
.tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
padding: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before,
.tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before {
border-right: 1px solid #ccc;
border-radius: 0 0 5px 0;
-webkit-border-radius: 0 0 5px 0;
-moz-border-radius: 0 0 5px 0;
}
.tree li:first-child::after {
border-radius: 0 0 0 5px;
-webkit-border-radius: 0 0 0 5px;
-moz-border-radius: 0 0 0 5px;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before {
content: '';
position: absolute;
bottom: 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;
}
<div class="tree">
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#">Grandfather</a></li>
<li><a href="#">Grandmother</a></li>
</ul>
<a href="#">Father</a>
</li>
<li>
<ul>
<li><a href="#">Grandfather</a></li>
<li><a href="#">Grandmother</a></li>
</ul>
<a href="#">Mother</a>
</li>
</ul>
<a href="#">Self</a>
</li>
</ul>
</div>
此外,这里有一个 jsfiddle,其中包含来自代码播放器网站的(反向)原始树。