Structure of CSS Hierarchy

I stumbled upon a simple organization chart online and decided to customize it by adding an avatar bubble. However, I am currently facing two issues that I am unable to resolve on my own. Any assistance or guidance would be greatly appreciated.

Below is the code snippet:

* {
  margin: 0;
  padding: 0;
}

.tree {
  text-align: center;
  padding-left: 10px;
}

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

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

/* More CSS code... */

The first issue I am facing is that the lines break when they do not fit on the page, resulting in a visually odd layout. For example, the "Manager 2" should be on the same line as "Manager Jane Smith," but it breaks down.

https://i.sstatic.net/p9lAA.png

Ideally, I would like the chart to extend the right pane as needed. If there are 20-30 children in one line, the overflow should be able to scroll to the right. However, I keep encountering issues with the positioning, which affects the alignment.

The second issue I am struggling with is preventing text from wrapping after the <br/> tag. This can be seen in the "Next Title, Next Name" section, where the text wraps incorrectly. I suspect the <br/> tag is causing this. In contrast, the "Department Area Name" text is not wrapping after the <br/> tag.

Thank you in advance for any help or suggestions you can provide.

Answer №1

By avoiding the use of floats and opting for changing the li to display:inline-block;, you can prevent the li elements from wrapping by including white-space:nowrap in the ul style.

To allow text wrapping within the li elements, you should add white-space:normal and apply vertical-align:top to align tree nodes properly when there are multiple children:

* {
  margin: 0;
  padding: 0;
}

.tree {
  text-align: center;
  padding-left: 10px;
}

.tree ul {
  display: block;
  white-space: nowrap;
  padding-top: 15px;
  position: relative;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.tree li {
  vertical-align:top;
  display: inline-block;
  white-space: normal;
  text-align: center;
  list-style-type: none;
  position: relative;
  padding: 15px 0px 0px 60px;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

/* Other CSS styles */

<div class="tree">
  <ul class="first">
    <li class="first techops">
      <span class="box">
        <div class="avatar"></div>
        Department Area Name<br>
        <strong>Director</strong><br><br>John Smith
        </span>
      <ul>
        <li><span class="box"><div class="avatar"></div>Manager<br/>Jane Smith</span>
          <ul>
            <li><span class="box"><div class="avatar"></div>Team Leader<br/>John Doe</span>
              <ul>
                <li><span class="box"><div class="avatar"></div>Next Title<br/>Next Name</span></li>
              </ul>
            </li>
            
            <!-- More nested list items here -->
            
          </ul>
        </li>

        <!-- More list items here -->

      </ul>
    </li>
  </ul>
</div>

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

What is the best way to line up changing column values with changing row values in an HTML table?

In its current state, the data appears as follows without alignment: https://i.sstatic.net/o5OLu.jpg The data columns are housed within a single variable due to the presence of multiple excel tabs with varying columns. Within the tr tag, rows are checked ...

CSS alone has the power to make multiple elements react to a hover action on a div

Is there a way to dynamically change the size of multiple div elements on hover? In this example, we can see how one div element's size changes when hovering over another div element: https://jsfiddle.net/9510a6kj/ .left, .right{ float:left; ...

How I disabled scrollbars in my HTML/CSS while implementing the Progid Microsoft gradient

IMPORTANT: Please read the latest update at the end of the question! Check out this snippet from my HTML/CSS code: html { height: 100%; } body { height: 100%; margin: 0px; filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#6699 ...

Vertically align the text

Is there a way to center this text vertically within the divs? I've attempted adjusting the position using relative/absolute, and modifying the top and left properties of the paragraph, but none of these methods have been successful. div.roxo{ ...

Is the first child component of Material UI Stack not properly aligned?

Is there a CSS issue that needs fixing? I am working on creating a vertical list of checkboxes with labels using the <Stack /> component from Material UI. I have tried implementing this in the sandbox provided (check out demo.tsx): https://codesandb ...

What steps can be taken to modify the style of a single button belonging to a broader group of elements?

Is there a way to hide the save button within a Vue Modal without affecting other buttons with the same class? .btn.btn-primary { display: none; } Simply targeting .btn-primary in the CSS affects all elements with that class, and adding .modal woul ...

Don't forget to preserve the hover state of divs even after refreshing

I recently added a navigation bar that expands in width upon hovering over it. For an illustration, check out this example: http://jsfiddle.net/Gsj27/822/ However, I encountered an issue when clicking on a button within the hover effect area. After load ...

JS causes the navigator to malfunction

UPDATE: Greetings! I have developed a navigation bar that loads various pages into a specific div. Here is the source code for the navigation bar: <div id="navBar"> <ul> <li><a href="#" onClick="document.getElementById('bott ...

How can I customize a CSS media class?

In my project, I am using a CSS library that includes the following class: @media only screen and (max-width: 480px) { .nav-tabs>li { margin-bottom:3px; } .nav-tabs>li, .nav-tabs>li>a { display:block !important; ...

Guide to creating a parallax scrolling effect with a background image

My frustration levels have hit an all-time high after spending days attempting to troubleshoot why parallax scrolling isn't functioning for a single image on a website I'm developing. To give you some context, here's the code I've been ...

Prevent background image animation in CSS

I've encountered an issue with a simple button I created that uses two background images for normal and hover states. Upon testing in various browsers, I noticed that the images animate on hover, causing them to slide up and down. I tried eliminating ...

Tips for locating the :before element's position with Javascript

I am currently working on an animation where I need to change the color of an element in the center once a small circle touches it. Despite trying to use the position() method, it does not work as intended because I am using a :before for animating the div ...

Is there a way to identify whether the image file is present in my specific situation?

I have a collection of images laid out as shown below image1.png image2.png image3.png image4.png … … image20.png secondImg1.png secondImg2.png secondImg3.png secondImg4.png secondImg5.png ……. …….. secondImg18.png My goal is to dynamically ...

What is the best way to achieve a transparent background for an element?

Having trouble making elements transparent in the background for mobile view with React. I've attempted adding background-color and background with a value of transparent to various classes, but it's not working as intended. Any suggestions on h ...

What is the best way to determine the size of CSS elements relative to other elements?

Is there a way to calculate the size of an element by using calc() or any other method based on the size of another DOM element? ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

Designing the chosen choice with CSS and HTML: Making it look like a placeholder and keeping it hidden in the dropdown menu

UPDATE: I'm puzzled by the 2 downvotes without any solutions to my problem. Issue: The challenge I'm facing is styling the color of the selected option that acts as a placeholder. I attempted different solutions like this one, but unfortunate ...

Display or hide a div element when hovering over it, while also being able to select the text within

I am trying to display and conceal a tooltip when hovering over an anchor. However, I want the tooltip to remain visible as long as my cursor is on it. Here is the fiddle link $('#showReasonTip').mouseover(function(){ $(this).parent().find(&apo ...

Two CSS borders of varying heights

Just wondering, can we achieve something similar to the style shown in the attachment using only CSS? <h3 style="border-bottom:1px solid #515151"><span style="border-bottom:3px solid #0066b3">HEADER</span></h3> ...

Removing a CSS Class Using Tampermonkey: A Step-by-Step Guide

I'm completely new to CSS and javascript, so please bear with me. My goal is to remove the class disable-stream from each of the div elements located under the div with the class "stream-notifications". Below is an image for reference: Even though I ...