无序列表

时间:2016-04-24 02:11:13

标签: html css

我正在尝试使用ulli HTML元素构建家谱。代码可用here。它使用psedo元素“绘制”连接器。然而问题是,如果某个人的名字太长,那么造型会有点偏离,如下所示:

enter image description here

我试图使垂直连接器居中,使其位于2个父母之间的水平连接器的中心。任何帮助将不胜感激。 codepen here

这是制作垂直连接器的css片段:

.tree li:last-child::before{
    border-right: 1px solid #ccc;
    border-radius: 0 5px 0 0;

    -webkit-transform: translateX(1px);
    -moz-transform: translateX(1px);
    transform: translateX(1px);

    -webkit-border-radius: 0 5px 0 0;
    -moz-border-radius: 0 5px 0 0;
    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: -12px; left: 50%;
    border-left: 1px solid #ccc;
    width: 0; height: 32px;
    z-index: -1;
}

.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;
    background: white;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;

    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
    transition: all 0.5s;
}
.tree li a+a {
    margin-left: 20px;
    position: relative;
}
.tree li a+a::before {
    content: '';
    position: absolute;
    border-top: 1px solid #ccc;
    top: 50%; left: -21px; 
    width: 20px;
}

2 个答案:

答案 0 :(得分:0)

我唯一能想到的是某种“黑客”,它会在树上添加一个不可见的节点:

<a href="#">Parent</a>
<a href="#">Very long parent name Parent</a>
<a href="#" style="visibility:hidden;">Parent</a> <!-- ugly hack that works -->

它通过添加一个与左叶(Parent

相同宽度的不可见叶子使顶行居中

答案 1 :(得分:0)

我认为垂直条应该来自锚伪类 - 而不是来自无序列表。我觉得有一种很酷的“中心任何”技术适用于你的问题。这是我添加的一小段代码:

.tree li a+a::after {
  content: '';
  position: absolute;
  top: 100%;
  transform: translateX(-50%);
  left: 50%;
  border-left: 1px solid #ccc;
  width: 0;
  height: 21px;
  z-index: -1;      
}

left: 50%; translateX(-50%)规则处理居中位并且只需要相对定位的父级即可工作,这是您已有的。我截取了一个屏幕截图,看看它如何应用于包含长名称的锚点。

enter image description here