gpt4 book ai didi

html - 使用 CSS3 伪元素格式化图表

转载 作者:行者123 更新时间:2023-11-28 01:06:30 26 4
gpt4 key购买 nike

我正在使用无序列表构建祖先图表,但我无法让它在屏幕上正确显示。我开始使用来自 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 家族树中提取。

这会产生一个看起来像这样的图表

Ancestor chart

但我需要把它倒过来,“Self”在底部,最早的一代在顶部。

我可以通过反转列表结构来反转方框,但是我找不到修改 CSS 来让连接器看起来正确的方法。

Inverted tree

有没有办法修改::before 和::after 伪元素来得到我想要的?还是我的处理方式完全错误?

(也可以把“自己”放在左边,最早的一代放在右边,这样会更好。)

最佳答案

您可以重新安排 HTML,并进行一些 css 调整(即交换顶部和底部填充、交换顶部和底部位置并更改 border-radius 以匹配新安排)有效:

.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包含来自代码播放器网站的(反向)原始树。

关于html - 使用 CSS3 伪元素格式化图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com