使用HTML / CSS创建族谱

时间:2018-04-04 16:55:38

标签: html css

仅使用HTML / CSS处理家谱。我正在尝试将叔叔/阿姨添加到它以及他们的孩子们这将是我的表兄弟。我一直在玩它但却无法做到。最后,我想补充一下阿姨/叔叔和他们的孩子。此外,如果我可以添加祖父母,这将是一个很好的额外的接触。

p {
  padding-top: 20px;
  padding-left: 300px;
  border-style: solid;
  border-width: 2px;
}

a {
  margin-top: 50px;
}

* {
  margin: auto;
  padding: auto;
}

.tree ul {
  padding-top: 20px;
  position: relative;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  transition: all 0.5s;
}

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


/*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: 45px;
  z-index: -1;
}

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


/*Time for some hover effects*/


/*We will apply the hover effect the the lineage of the element also*/

.tree li a:hover,
.tree li a:hover~ul li a {
  background: #c8e4f8;
  color: #000;
  border: 1px solid #94a0b4;
}


/*Connector styles on hover*/

.tree li a:hover~ul li::after,
.tree li a:hover~ul li::before,
.tree li a:hover~ul::before,
.tree li a:hover~ul ul::before {
  border-color: #94a0b4;
}
<div class="tree">
  <ul>
    <li>
      <a href="#">Mom(Susan)</a>
      <a href="#">Dad(John)</a>
      <ul>
        <!-- <li>
						<a href="#">Thomas(Brother)</a>
						
					</li> -->
        <li>
          <a href="#">Thomas(Brother)</a>
          <a href="#">Me</a>
          <a href="#">Brandon(Brother)</a>
          <ul>
            <li><a href="#">Grand Child</a></li>
            <li>
              <a href="#">Grand Child</a>
              <ul>
                <li>
                  <a href="#">Great Grand Child</a>
                </li>
                <li>
                  <a href="#">Great Grand Child</a>
                </li>
                <li>
                  <a href="#">Great Grand Child</a>
                </li>
              </ul>
            </li>
            <li><a href="#">Grand Child</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

<a href="index.html"> Link back to Home Page!</a>

1 个答案:

答案 0 :(得分:0)

我会尝试另一种方法。这是一个例子:

*, *:before, *:after {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  min-width: 1200px;
  margin: 0;
  padding: 50px;
  color: #333;
  font: 16px Verdana, sans-serif;
  background: #dedede;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

#wrapper {
  position: relative;
}

.branch {
  position: relative;
  margin-right: 250px;
}
.branch:before {
  content: "";
  width: 50px;
  border-top: 2px solid #333;
  position: absolute;
  right: -100px;
  top: 50%;
  margin-top: 1px;
}

.entry {
  position: relative;
  min-height: 60px;
}
.entry:before {
  content: "";
  height: 100%;
  border-right: 2px solid #333;
  position: absolute;
  right: -50px;
}
.entry:after {
  content: "";
  width: 50px;
  border-top: 2px solid #333;
  position: absolute;
  right: -50px;
  top: 50%;
  margin-top: 1px;
}
.entry:first-child:before {
  width: 10px;
  height: 50%;
  top: 50%;
  margin-top: 2px;
  border-radius: 0 10px 0 0;
}
.entry:first-child:after {
  height: 10px;
  border-radius: 0 10px 0 0;
}
.entry:last-child:before {
  width: 10px;
  height: 50%;
  border-radius: 0 0 10px 0;
}
.entry:last-child:after {
  height: 10px;
  border-top: none;
  border-bottom: 2px solid #333;
  border-radius: 0 0 10px 0;
  margin-top: -9px;
}
.entry.sole:before {
  display: none;
}
.entry.sole:after {
  width: 50px;
  height: 0;
  margin-top: 1px;
  border-radius: 0;
}

.label {
  display: block;
  min-width: 150px;
  padding: 5px 10px;
  line-height: 20px;
  text-align: center;
  border: 2px solid #333;
  border-radius: 5px;
  position: absolute;
  right: 0;
  top: 50%;
  margin-top: -15px;
}
<div id="wrapper"><span class="label">Root</span>
  <div class="branch lv1">
    <div class="entry"><span class="label">Entry-1</span>
      <div class="branch lv2">
        <div class="entry"><span class="label">Entry-1-1</span>
          <div class="branch lv3">
            <div class="entry"><span class="label">Entry-1-1-1</span></div>
            <div class="entry"><span class="label">Entry-1-1-2</span></div>
            <div class="entry"><span class="label">Entry-1-1-3</span></div>
          </div>
        </div>
        <div class="entry"><span class="label">Entry-1-2</span>
          <div class="branch lv3">
            <div class="entry sole"><span class="label">Entry-1-2-1</span></div>
          </div>
        </div>
        <div class="entry"><span class="label">Entry-1-3</span>
          <div class="branch lv3">
            <div class="entry sole"><span class="label">Entry-1-3-1</span></div>
          </div>
        </div>
      </div>
    </div>
    <div class="entry"><span class="label">Entry-2</span></div>
    <div class="entry"><span class="label">Entry-3</span>
      <div class="branch lv2">
        <div class="entry"><span class="label">Entry-3-1</span></div>
        <div class="entry"><span class="label">Entry-3-2</span></div>
        <div class="entry"><span class="label">Entry-3-3</span>
          <div class="branch lv3">
            <div class="entry"><span class="label">Entry-3-3-1</span></div>
            <div class="entry"><span class="label">Entry-3-3-2</span>
              <div class="branch lv4">
                <div class="entry"><span class="label">Entry-3-3-2-1</span></div>
                <div class="entry"><span class="label">Entry-3-3-2-2</span></div>
              </div>
            </div>
            <div class="entry"><span class="label">Entry-3-3-3</span></div>
          </div>
        </div>
        <div class="entry"><span class="label">Entry-3-4</span></div>
      </div>
    </div>
    <div class="entry"><span class="label">Entry-4</span></div>
    <div class="entry"><span class="label">Entry-5</span></div>
    <div class="entry"><span class="label">Entry-6</span></div>
  </div>
</div>

<a href="https://stackoverflow.com/a/31161783/762640">Source 1</a>
<a href="https://codepen.io/anon/pen/OVQXGg">Source 2</a>

(fiddle link)