我正在使用无序列表构建祖先图表,但是我很难让它在屏幕上正确显示。我开始使用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家族树中提取出来。
这将生成一个类似于
html - 使用CSS3伪元素设置图表格式-LMLPHP
但我需要它是颠倒的,底部是“自我”,最早的一代在顶部。
我可以通过反转列表结构来反转这些框,但是我找不到修改CSS的方法,无法使连接器看起来正确。
html - 使用CSS3伪元素设置图表格式-LMLPHP
是否有方法修改::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包含代码播放器网站的(反向)原始树。

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

10-12 12:24
查看更多