html - Properly centering multiple variable-width children inside a variable width container -


i have copied simplified version of this code jsfiddle.

the code tree diagram written html , css. jsfiddle above looks good.

however, after playing around have discovered centering of nodes relative parents works if child same size or bigger parent.

i still new css, primary issue seems fact all <li> elements floated left.

consider this example, child smaller parent. because parent <li> floated left, child <li> gets floated left well.

how can fix this?

i think playing display set table , inline-table might help. setting display:inline-table ul elements , display:table anchor tags in combination margin: 0 auto seems work.

/*now css*/  * {margin: 0; padding: 0;}    .tree ul {  	padding-top: 20px; position: relative;  	display: inline-table;  	transition: 0.5s;  	-webkit-transition: 0.5s;  	-moz-transition: 0.5s;  }    .tree li {  	float: left; text-align: center;  	list-style-type: none;  	position: relative;  	padding: 20px 5px 0 5px;  	text-align:center;  	transition: 0.5s;  	-webkit-transition: 0.5s;  	-moz-transition: 0.5s;      display: list-item;  }    /*we use ::before , ::after draw 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 remove left-right connectors elements without   siblings*/  .tree li:only-child::after, .tree li:only-child::before {  	display: none;  }    /*remove space top of single children*/  .tree li:only-child{ padding-top: 0;}    /*remove left connector first child ,   right connector last child*/  .tree li:first-child::before, .tree li:last-child::after{  	border: 0 none;  }  /*adding vertical connector 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 add downward connectors 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: table;      margin: 0 auto;  	border-radius: 5px;  	-webkit-border-radius: 5px;  	-moz-border-radius: 5px;  	  	transition: 0.5s;  	-webkit-transition: 0.5s;  	-moz-transition: 0.5s;  }    /*time hover effects*/  /*we apply hover effect the lineage of element also*/  .tree li a:hover, .tree li a:hover+ul li {  	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="#">parent</a>              <ul>                  <li>                      <a href="#">child</a>                      <ul>                          <li>                              <a href="#">+</a>                          </li>                      </ul>                   </li>                  <li>                      <a href="#">child big</a>                      <ul>                          <li>                              <a href="#">+</a>                          </li>                      </ul>                  </li>                  <li>                      <a href="#">child bigger</a>                      <ul>                          <li>                              <a href="#">+</a>                              <ul>                                  <li>                                      <a href="#">lorem ipsum</a>                                  </li>                              </ul>                          </li>                      </ul>                  </li>                  <li>                      <a href="#">child big</a>                      <ul>                          <li>                              <a href="#">+</a>                              <ul>                                  <li>                                      <a href="#">lorem ipsum dolor sit amet</a>                                  </li>                              </ul>                          </li>                      </ul>                  </li>                    </ul>          </li>      </ul>  </div>

hope helps.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -