在HTML / CSS中将多路树树根居中

时间:2019-04-08 08:48:02

标签: html css

我是HTML / CSS的初学者,我想将根放在节点 2 4 之间。我考虑了节点 2 3 4 之间的固定距离,然后将根相对于整个图形居中。

如果可能的话,我想将整个图形向左旋转90°。

感谢您的帮助!

* {
  margin: 0;
  padding: 0;
}

.tree ul {
  padding-top: 20px;
  position: relative;
}

.tree li {
  float: left;
  text-align: center;
  list-style-type: none;
  position: relative;
  padding: 20px 5px 0 5px;
}

.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;
}

.tree li:first-child::after {
  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;
  border-radius: 25px 25px 25px 25px;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
}
<div class="tree">
  <ul>
    <li>
      <a href="#">1</a>
      <ul>
        <li>
          <a href="#">2</a>
          <ul>
            <li>
              <a href="#">2.1</a>
            </li>
            <li>
              <a href="#">2.2</a>
            </li>
          </ul>
        </li>
        <li>
          <a href="#">3</a>
          <ul>
            <li>
              <a href="#">3.1</a>
            </li>
            <li>
              <a href="#">3.2</a>
            </li>
          </ul>
        </li>
        <li>
          <a href="#">4</a>
          <ul>
            <li>
              <a href="#">4.1</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:1)

将整个图形向左旋转90°,建议使用CSS变换。 由于您的代码块没有设置宽度和高度,因此我将其设置为display: inline-block以限制其宽度并能够按照我们希望的方式旋转它。

要使顶部元素居中和/或为每个节点设置一个固定的宽度,请在其他元素上使用最小宽度(我将其指定为.special类),或者给他节点子元素,然后然后将其不透明度或可见性设置为0。如果您使用最小宽度解决方案,则需要将其<li>子级设置为float: none,以使其居中。

* {
  margin: 0;
  padding: 0;
}

.tree ul {
  padding-top: 20px;
  position: relative;
}

/* Using display: inline-block to limit its width */

.tree > ul {
  display: inline-block;
  transform: rotate(90deg);
}

/* Clearing floats (so <ul> has a defined height) */

.tree ul:after {
  content: "";
  display: table;
  clear: both;
}

.tree li {
  float: left;
  text-align: center;
  list-style-type: none;
  position: relative;
  padding: 20px 5px 0 5px;
}

.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;
}

.tree li:first-child::after {
  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;
  border-radius: 25px 25px 25px 25px;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
}

.tree .special {
  min-width: 94.594px;
}

.tree .special > ul > li {
  float: none;
}
<div class="tree">
  <ul>
    <li>
      <a href="#">1</a>
      <ul>
        <li>
          <a href="#">2</a>
          <ul>
            <li>
              <a href="#">2.1</a>
            </li>
            <li>
              <a href="#">2.2</a>
            </li>
          </ul>
        </li>
        <li>
          <a href="#">3</a>
          <ul>
            <li>
              <a href="#">3.1</a>
            </li>
            <li>
              <a href="#">3.2</a>
            </li>
          </ul>
        </li>
        <li class="special">
          <a href="#">4</a>
          <ul>
            <li>
              <a href="#">4.1</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>