CSS problem, Binary tree scatter at the bottom

Hello friends,

I am working on a MLM Project & have a binary tree which is being generated from my database. The tree legs appear well at the top but scatter at the bottom. At the top, the left leg is showing on the left and the right leg on the right, which is ok. But at the bottom, the left leg is showing at the top of the right leg. I think the problem is with my css.

<style type="text/css">
* {margin: 0; padding: 0;}


.tree li a:hover, .tree li a:hover+ul li a
{
	margin-right:100px
}
.leftnode > ul > li > a
{
	background-color:whitesmoke;
}
.rightnode >  ul > li > a
{
	background-color:orange;
}
.tree ul {
	padding-top: 20px; position: relative;f
	
	transition: all 0.5s;
	-webkit-transition: all 0.5s;
	-moz-transition: all 0.5s;
}

li.child 
{
	width:50%
}

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

.child
{
    /* width:30%;
    min-width:500px; */
}

/*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;
	
	transition: all 0.5s;
	-webkit-transition: all 0.5s;
	-moz-transition: all 0.5s;
}

/*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;
}
</style>```

Please help. Thanks a million
1 Like

I think the problem is more likely the width of your viewport being too small to contain a binary tree that wide.

How deep and wide is your tree?

2 Likes

Please provide the generated html that goes with that CSS so we can test. (i.e. View source from browser and then copy and paste generated html).

It does look like you have to much data to fit in the allocated space as mentioned by @m_hutley above .

1 Like

the depth is infinity. but i don’t thing anyone can get to 200 levels.

the tree was generated from mysql database table using php. @m_hutley or is there a way i can make the width expand to take the width? even if it will be in an iframe or whatever where i can scroll left and right to see the expanded tree?

thanks greatly.

After your php is parsed a browser only gets html so I was hoping to get your generated html from view source so that I could play around with it. :slight_smile:

Without html I won’t be much help here I’m afraid.

Would you like the thread moved to php instead ?

1 Like

Thanks @PaulOB


<div class='tree'>

<ul>
<li><a href="#">greatest2020</a>
			<ul>
		<li class="child">
			<a href="#">Okechukwu</a>
			        <ul>
                            <li class="child"><a href="#">Pendy</a>
                            <ul>
                            <li class="child"><a href="#">Chichi</a>
                            <ul>
                            <li class="child"><a href="#">Olumichael2000</a>
                            <ul>
                            <li class="child"><a href="#">Endykay</a>
                            <ul>
                            <li class="child"><a href="#">ngsally</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Greatman</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Alex2020</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Onu4cash</a>
                            <ul>
                            <li class="child"><a href="#">Kikiomuwaha</a>
                            <ul>
                            <li class="child"><a href="#">chinyere2020</a>
                            <ul>
                            <li class="child"><a href="#">Aforeal 3</a>
                                    </li>
                            <li class="child"><a href="#">Benchy</a>
                            <ul>
                            <li class="child"><a href="#">Chizaramekpere</a>
                            <ul>
                            <li class="child"><a href="#">Frankgreen</a>
                                    </li>
                            <li class="child"><a href="#">Uchechi2020</a>
                            <ul>
                            <li class="child"><a href="#">Oluba4real</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Momo-A</a>
                                    </li>
                            <li class="child"><a href="#">Maymowisk</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Vickybilly</a>
                            <ul>
                            <li class="child"><a href="#">bluebird</a>
                            <ul>
                            <li class="child"><a href="#">Fancy</a>
                                    </li>
                            <li class="child"><a href="#">Blue</a>
                            <ul>
                            <li class="child"><a href="#">Purple</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">gladstone</a>
                            <ul>
                            <li class="child"><a href="#">Prettymoney</a>
                            <ul>
                            <li class="child"><a href="#">Prettymoney01</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Gladstone1</a>
                            <ul>
                            <li class="child"><a href="#">Gladstone2</a>
                            <ul>
                            <li class="child"><a href="#">Lynben</a>
                            <ul>
                            <li class="child"><a href="#">Ndisuccesss</a>
                            <ul>
                            <li class="child"><a href="#">Charles E</a>
                            <ul>
                            <li class="child"><a href="#">Obong1</a>
                            <ul>
                            <li class="child"><a href="#">Obong3</a>
                                    </li>
                            <li class="child"><a href="#">Ofonmbuk</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Obong2</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Ubonchristy</a>
                            <ul>
                            <li class="child"><a href="#">Udeme2020</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Amwatin</a>
                            <ul>
                            <li class="child"><a href="#">Esedoc</a>
                                    </li>
                            <li class="child"><a href="#">Jephorneph</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Urujava2020</a>
                            <ul>
                            <li class="child"><a href="#">LadyN</a>
                            <ul>
                            <li class="child"><a href="#">Kayden02</a>
                                    </li>
                            <li class="child"><a href="#">Zalvis</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Fortuneheaven</a>
                            <ul>
                            <li class="child"><a href="#">Fortuneheaven1</a>
                                    </li>
                            <li class="child"><a href="#">Fortuneheaven2</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Gladstone3</a>
                            <ul>
                            <li class="child"><a href="#">Tones021</a>
                            <ul>
                            <li class="child"><a href="#">Roseben</a>
                            <ul>
                            <li class="child"><a href="#">Akpan2020</a>
                                    </li>
                            <li class="child"><a href="#">Roseben1</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">vickyudom</a>
                            <ul>
                            <li class="child"><a href="#">Lynben01</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Verojava</a>
                            <ul>
                            <li class="child"><a href="#">Bestgiftdon1</a>
                            <ul>
                            <li class="child"><a href="#">Bestgiftdon2</a>
                            <ul>
                            <li class="child"><a href="#">Charles Ako</a>
                            <ul>
                            <li class="child"><a href="#">John P</a>
                            <ul>
                            <li class="child"><a href="#">Casefried2020</a>
                                    </li>
                            <li class="child"><a href="#">Alvintone</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Emygirl</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Bestgiftdon3</a>
                            <ul>
                            <li class="child"><a href="#">Vinesalem2013</a>
                            <ul>
                            <li class="child"><a href="#">Ritaeffiong2016</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Sweetjoe</a>
                            <ul>
                            <li class="child"><a href="#">Maria Afangideh</a>
                            <ul>
                            <li class="child"><a href="#">Utidon</a>
                                    </li>
                            <li class="child"><a href="#">Utidon1</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">arkway</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Lawrence2020</a>
                            <ul>
                            <li class="child"><a href="#">Onyibe</a>
                            <ul>
                            <li class="child"><a href="#">John2020</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Caroline c.</a>
                            <ul>
                            <li class="child"><a href="#">Priscamoore</a>
                            <ul>
                            <li class="child"><a href="#">Lilchrist</a>
                            <ul>
                            <li class="child"><a href="#">Frankmoore</a>
                            <ul>
                            <li class="child"><a href="#">De Eagle</a>
                                    </li>
                            <li class="child"><a href="#">endysol</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Brownie</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Harryberry</a>
                            <ul>
                            <li class="child"><a href="#">Bosslady</a>
                                    </li>
                            <li class="child"><a href="#">Ngogirl</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
    		</li>
			<li class="child">
			<a href="#">ifybest2020</a>
			        <ul>
                            <li class="child"><a href="#">vcc2020</a>
                            <ul>
                            <li class="child"><a href="#">bewise</a>
                            <ul>
                            <li class="child"><a href="#">generations</a>
                            <ul>
                            <li class="child"><a href="#">nevvie</a>
                                    </li>
                            <li class="child"><a href="#">Ahiabalink24</a>
                            <ul>
                            <li class="child"><a href="#">Testimoneny2020</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Ritagold</a>
                            <ul>
                            <li class="child"><a href="#">Excelqueen</a>
                            <ul>
                            <li class="child"><a href="#">Comfort4real</a>
                                    </li>
                            <li class="child"><a href="#">Badike</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">chikamso</a>
                            <ul>
                            <li class="child"><a href="#">Tesma478</a>
                            <ul>
                            <li class="child"><a href="#">Felisco</a>
                                    </li>
                            <li class="child"><a href="#">Adakuokeke</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Seedroyal</a>
                            <ul>
                            <li class="child"><a href="#">greatest202001</a>
                            <ul>
                            <li class="child"><a href="#">Okoro4cash</a>
                            <ul>
                            <li class="child"><a href="#">Rawfusion</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Greatest202002</a>
                            <ul>
                            <li class="child"><a href="#">Greatest202003</a>
                            <ul>
                            <li class="child"><a href="#">Vickybelly2</a>
                            <ul>
                            <li class="child"><a href="#">Handofgod</a>
                            <ul>
                            <li class="child"><a href="#">Handofgod1</a>
                                    </li>
                            <li class="child"><a href="#">Handofgod2</a>
                            <ul>
                            <li class="child"><a href="#">Handofgod3</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">onyeamuma</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Warriors</a>
                            <ul>
                            <li class="child"><a href="#">Aka4millioniar</a>
                            <ul>
                            <li class="child"><a href="#">Aforealthree</a>
                            <ul>
                            <li class="child"><a href="#">fessy</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">friday2020</a>
                            <ul>
                            <li class="child"><a href="#">beauty2020</a>
                            <ul>
                            <li class="child"><a href="#">Baseman</a>
                                    </li>
                            <li class="child"><a href="#">Promise</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">josephine2020</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Seedroyal2</a>
                            <ul>
                            <li class="child"><a href="#">Hazelnut</a>
                            <ul>
                            <li class="child"><a href="#">Sylviagold</a>
                                    </li>
                            <li class="child"><a href="#">Steve</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Ujubest4cash</a>
                            <ul>
                            <li class="child"><a href="#">Jennyjava</a>
                            <ul>
                            <li class="child"><a href="#">Obi4cash</a>
                            <ul>
                            <li class="child"><a href="#">Nkybaby</a>
                            <ul>
                            <li class="child"><a href="#">Ehi2020</a>
                                    </li>
                            <li class="child"><a href="#">Glory2020</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Nancy2020</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Nuelamario</a>
                            <ul>
                            <li class="child"><a href="#">Caroleke</a>
                            <ul>
                            <li class="child"><a href="#">favour2020</a>
                                    </li>
                            <li class="child"><a href="#">Tobe2020</a>
                                    </li>
              
        </ul>  
                    </li>
                            <li class="child"><a href="#">Oge2020</a>
                                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
                    </li>
              
        </ul>  
    		</li>
	</ul>
</li>
</ul>
   </div>

Cheers that’s what I wanted I’ll have a play with that when I get back to my computer :slight_smile:

Just the length of that code block makes me think ‘too much data, too little space’, followed quickly by ‘draw it on an SVG and use a zoomable viewport’.

It’s going to be completely unreadable on a phone.

2 Likes

Ok, looking at your code and forcing a width of 6000px and removing your 50% widths in a couple of places I can get the tree to display.

The problem is that the width required will be determined by the number of entries at its widest point. If there is no width applied then the tree wraps in the wrong place due to the way its constructed.

I guess you may have to dynamically calculate the width of that tree and apply it to the parent. I will see if there is a css way to allow the menu to grow without wrapping but I’m guessing not due to the way that floats are utilised in its construction

Yes a nightmare :slight_smile:

2 Likes

That was quicker than expected.:slight_smile:

If we use a flex layout instead on the tree then that will nullify all the floats and also allow the element to stretch without requiring a fixed width (flex does not wrap unless you tell it to).

Here’s a new pen with that code added.

Of course as already mentioned there may be considerable work involved in allowing smaller screens to see this easily.

1 Like

Guys, thank you very much. Am so happy. You really fix the problem.

One more thing please. Is there a way I can centralise the tree insteal of it aligning left to the screen. What I mean is that once the page loads, the top member (greatest2020) is at the center of my screen, then i can now scroll left or right to view whichever side am interested in?

thanks greatly.

You would need js for that once the element is larger than the viewport and adjust the scroll bar to the center position. You could drag the element to the left in css but then you couldn’t scroll to see the left side.

This is really a question for @m_hutley but something like this might work.

(function () {
  var bodyEl = document.querySelector("body").offsetWidth;
  var el = document.querySelector(".tree > ul").scrollWidth;
  document.documentElement.scrollLeft = document.body.scrollLeft =
    (el - bodyEl) / 2;
})();

Codepen updated:

2 Likes

If it ain’t broke, you’re not tryin’.

Wait, that’s not it…

Ah close enough.

1 Like

Thanks @PaulOB, am so grateful
thumb up

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.