I have a possible solution, unfortunately, it is not always general, but better than nothing that I think.
Here is the solution:
jsfiddle
Here you can see the solution in full screen.
$(document).ready(function() { $('.c1').hide(); $("a").click(function() { $(this).parent().children("ul").toggle(); //make ul visible //foreach visible ul $($('ul').filter(':visible')).each(function() { var ul = $(this); var ulL = (ul.outerWidth() / 2); var lenght = 0; //calculate the left rule accordingly to the length of the li if(ul.children("li").length > 2){ var middle = Math.ceil(ul.children("li").length / 2); for (var i = 1; i < middle; i++) { lenght = lenght + ul.children("li:nth-child(" + i + ")").outerWidth(); } if ((ul.children("li").length)%2 == 0){ lenght = lenght + (ul.children("li:nth-child(" + middle + ")").outerWidth()); }else{ lenght = lenght + (ul.children("li:nth-child(" + middle + ")").outerWidth() / 2); } ul.find("li").css("left", (ulL - lenght)); } else if(ul.children("li").length === 2){ // base case, only two li var ulL = (ul.outerWidth() / 4); var half = ul.children("li:first-child").outerWidth() / 2; ul.find("li").css("left", (ulL - half)); } else if(ul.children("li").length === 1){ // base case, only 1 li ul.find("li").css("left", 0); } }); }); });
Unfortunately, such a calculation does not always make a diagram well formed, so I added a class:
.no-left > li{ left: 0 !important; }
In this example, I used this in
<a href="#" class="yellow">Director <br />Oprn</a> <ul class="c1 no-left"> .... </ul>
silviagreen
source share