What are my choices for showcasing hierarchical information?

I'm currently working on organizing hierarchical data using HTML, JS, and jQuery. My chosen method involves utilizing a left-floated div container system in place of a grid system for easier recursive code generation. One challenge I've encountered is the need to ensure that the tree structure's width fits perfectly within the client window. To accomplish this, I dynamically adjust the width of each div created in JavaScript by calculating percentages based on sibling count.

However, this approach results in wasted space as even childless elements receive the same width as their siblings. This becomes particularly problematic at deeper levels where space efficiency is crucial.

An alternative approach I've explored is letting the browser auto-size everything, which optimizes space usage but sacrifices dynamic sizing control. The inability to zoom properly and guarantee an initial 100% width poses challenges. Additionally, when the tree exceeds the page width, the overflowed divs shift vertically instead of scrolling horizontally as desired.

Therefore, my question is: what options exist to ensure optimal space utilization, precise width fitting, and dynamic sizing with zoom capability for my tree structure?

For reference, here is a screenshot of the hierarchical data:

And a simplified example of the generated code:

<div id="map" class="tag-container">
    <div class="tag">Level 0 data</div>
    <div class="tag-container">
        <div class="tag">Level 1 data</div>
        <div class="tag-container">
            <div class="tag">Level 2 data</div>
            <div class="tag-container">
                <div class="tag">Level 3 data</div>
            </div>
            <div class="tag-container">
                <div class="tag">Level 3 data</div>
                <div class="tag-container">
                    <div class="tag">Level 4 data</div>
                </div>
            </div>
        </div>
        <div class="tag-container">
            <div class="tag">Level 2 data</div>
            <div class="tag-container">
                <div class="tag">Level 3 data</div>
            </div>
            <div class="tag-container">
                <div class="tag">Level 3 data</div>
                <div class="tag-container">
                    <div class="tag">Level 4 data</div>
                </div>
            </div>
        </div>
    </div>
</div>

Along with the relevant CSS:

.tag-container {
    padding: 0px;
    float: left;
    box-sizing: border-box;
    display: inline;
    width: 33.333%
}

.zoom {
    cursor: pointer
}

body {
    background-color: azure;
    overflow: scroll
}

Answer №1

For those unafraid of utilizing css3, here is a unique suggestion:

Create a div grouping tag-containers that are on the same level. The revamped markup would appear as follows:

<div id="map" class="tag-container">
        <div class="tag">Level 0 data</div>
        <div class="tag-container">
            <div class="tag">Level 1 data</div>
            <div class="tag-container-group">
                <div class="tag-container">
                    <div class="tag">Level 2 data</div>
                    <div class="tag-container-group">
                        <div class="tag-container">
                            <div class="tag">Level 3 data</div>
                        </div>
                        <div class="tag-container">
                            <div class="tag">Level 3 data</div>
                            <div class="tag-container-group">
                                <div class="tag-container">
                                    <div class="tag">Level 4 data</div>
                                </div>
                            </div>
                    </div>
                </div>
                </div>
            <div class="tag-container">
                <div class="tag">Level 2 data</div>
                <div class="tag-container-group">
                    <div class="tag-container">
                        <div class="tag">Level 3 data</div>
                    </div>
                    <div class="tag-container">
                        <div class="tag">Level 3 data</div>
                        <div class="tag-container-group">
                            <div class="tag-container">
                                <div class="tag">Level 4 data</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</div>

Next, implement the following CSS

.tag {
    text-align: center;
    border: 1px solid #aaa;
}
.tag-container-group {
    /* Firefox */
    display:-moz-box;
    -moz-box-orient:horizontal;

    /* Safari, Opera, and Chrome */
    display:-webkit-box;
    -webkit-box-orient:horizontal;

    /* W3C */
    display:box;
    box-orient:horizontal;
}
.tag-container-group > .tag-container {
    width: 1px;
    -moz-box-flex:1.0; /* Firefox */
    -webkit-box-flex:1.0; /* Safari and Chrome */
    -ms-flex:1.0; /* Internet Explorer 10 */
    box-flex:1.0;
}

This has only been tested in chrome and may not work well in older versions of IE, but you could explore using an alternative style sheet with display: inline-table for better compatibility.

If you prefer nodes sized relative to content, consider removing width: 1px in the

.tag-container-group > .tag-container

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activating scrolling through Javascript

A friend of mine created a web page to help me learn some JavaScript. He designed a feature where the content in each div becomes visible as soon as the user scrolls past it. However, I noticed that the content appears too late for my liking. I would like ...

Remove the boundaries from the grid and only retain the box

I am not skilled in css, but I simply want to eliminate the interior edges of the box and retain only the outer edges. .grid-container { display: grid; grid-template-columns: auto auto auto; background-color: #2196F3; padding: 10px; } .grid-ite ...

Adding to the beginning of a list in JQuery mobile

Having trouble figuring out how to prepend my list in jQuery mobile while keeping the divider on top of the most recent item added. I attempted to prepend the newly added item, but it ended up shifting the divider to the bottom instead. function loadScan ...

Hide when reaching the end with d-none

One of the challenges I'm facing is aligning a button to the right side of a column while still utilizing d-none to control its display. Previously, I used d-flex justify-content-md-around justify-content-lg-end for alignment before adding d-none, but ...

Making the most of Material Design icons in MaterializeCSS: A step-by-step guide

I am looking to customize my mdi icons by adding my own set of icons into custom classes. The existing mdi classes do not match my requirements. Is there a way for me to add these icons and use them just like the default ones? <i class="mdi-image-face ...

Failed to add a new entry to the Sharepoint 2013 List

Having trouble saving an item to a SharePoint list using Knockout.js and REST. In my Ajax POST request, I've used ko.toJSON but the entry doesn't show up in the SharePoint list. It's possible that I'm passing the wrong parameter value. ...

Passing form values from JavaScript to a JSP page - a comprehensive guide

I am working on a sample HTML page that includes inline JavaScript for calculating geocodes and retrieving latitude and longitude values. My question is, how can I submit all the form values along with the latitude and longitude returned from the showlocat ...

Loading information in a directive by utilizing promises in a service with AngularJS

Can anyone lend a hand? I've been struggling to solve this issue. I created a directive (see below) to display a pre-written ul-list on a page using html data fetched asynchronously from a database server. Both the Directive and The Service are funct ...

Execute PHP function by clicking on an HTML anchor tag

I'm curious - can a PHP function be triggered simply by clicking a link, or is it better to stick with the form submit method? If linking works, where should the reference point to? Check out this code snippet: <?php session_start(); func ...

Unable to get the Express.js Router functioning correctly on my server, even in a basic scenario

I am encountering an issue with using express.Router(). It was not functioning correctly in my application for serving JSON from MongoDB, so I attempted to simplify the scenario. However, I am receiving a 404 Not Found error in the request. What steps shou ...

What is the best way to assign specific variables in a PHP loop using form input?

Let's say I have a basic HTML form displayed below: <div class="form-group"> <label class="control-label" for="checkboxes" name="industry">How would you classify your business?</label> ...

Creating a proper nth-child CSS for multi-level HTML elements involves understanding the structure of the elements

My current project involves a screen where widgets can be dynamically inserted into the DOM. I am looking to adjust the z-index based on the number of times the class decrement-z-index appears. This is the CSS code I have written in an attempt to achieve ...

When the button is clicked, request the total count of elements in the array

Is there a way to log the index of an array element when clicked? I have a large array with over 100 elements: var cubesmixed = []; var cubes; for(var i = 0; i < 143; i++) { cubes = paper.rect(Math.floor(Math.random()*2000), Math.floor(Math.random ...

What steps can I take to stop text from disappearing when the screen size decreases and the span is used?

I'm currently using Bootstrap 5 to create a form. On a computer screen, my file input element looks like this: https://i.sstatic.net/fX6Kx.png However, on mobile devices, it appears like this: https://i.sstatic.net/ilUZ9.png Below is the code sni ...

Search bar placeholder text appears off-center in Firefox browser

I have implemented a universal search bar on our website and it is working perfectly in Chrome and Safari. However, I am facing an issue with Firefox where the placeholder text is aligned to the bottom of the bar instead of the middle. Usually, placeholder ...

Adding spacing breakpoints in Material-UI 5 sx properties

Currently in the process of updating my components to utilize the latest MUI version. I have been converting old classes into the sx props and I find this new styling method to be more readable in my opinion. However, one thing that concerns me is handl ...

Using JavaScript and HTML, create a click event that triggers a drop-down text

Can anyone help me with creating a dropdown feature using JavaScript, HTML, and CSS? I want to be able to click on the name of a project and have information about that project show up. Any suggestions on how I can achieve this? Thanks in advance! ...

Is there a way for me to discover the identity of the victor?

There are 4 divs that move at different, random speeds each time. I am trying to determine which one is the fastest or reaches the goal first. Additionally, there is a betting box where you can choose a horse to bet on. I need to compare the winner with my ...

Automating the process of running npm start on page load: A guide

Recently, I've been delving into learning npm in order to incorporate it into a website. I'm curious about how exactly it is used within a website - do you typically need to execute the command "npm start"? How does this integration work for a li ...

Looking to transform a list into JSON format using JavaScript?

I have a collection that looks like this: <ol class="BasketballPlayers"> <li id="1">Player: LeBron, TotalPoints: 28753, MVP: true</li> <li id="2">Player: Steph, TotalPoints: 17670, MVP: true< ...