Resize the main container to fit the floated elements

I've been working on constructing a family tree, and the last part of the functionality is proving to be quite challenging for me.

My family tree consists of list elements that are all floated to the left.

Currently, when the tree expands beyond the screen size, it wraps itself making it difficult to read. What I would like is for the parent div #c1 to enable horizontal scrolling to navigate through the entire tree.

To address this issue temporarily, I added a div inside #c1 called #c2 with a width of 100000px. This prevents the wrapping issue by allowing users to scroll across the tree horizontally.

I am looking for a way to dynamically adjust the width of #c2 to fit the tree upon loading using JavaScript or find a CSS solution for this problem.

I have experimented with various solutions such as white-space: nowrap, but none seem to work effectively in my case.


* {margin: 0; padding: 0;}

.scrollbtn {
  float: right;
  cursor: pointer;
  margin: 5px;
  padding: 5px;
  border: 1px solid #ccc;
  color: #ccc;

  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
}
.scrollbtn:hover {
  background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}

#c1 {
  width: 96%;
  overflow: auto;
  padding: 2%;

}

#c2 {
    width: 100000px;
}

.tree ul {
  padding-top: 20px; position: relative;

  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  transition: all 0.5s;
}

/* Additional CSS code goes here */

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="tree.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
      <div id="c1">
        <div id="c2">
          <div class="tree" id="tree"><ul>/* Tree data */</div></div></div>
  </body>
</html>

Answer №1

If you're finding floats are causing some issues for you, consider this pure CSS solution. Sometimes, a parent element with only floated children can lead to problems such as collapsed content boxes and zero height like in the case of your #c2 div.

Here are 3 steps you can take in your CSS:

  1. Start by removing:

    #c2 {
        width: 100000px;
    }
    

    (Consider taking the #c2 div out of your HTML altogether)

  2. Next, add:

    .tree {
        white-space: nowrap;
    }
    
  3. Finally, modify the rules in your .tree li selector with these changes:

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

By using display: inline-block, vertical-align: top, and white-space: nowrap, you can arrange the elements of your tree horizontally while keeping them vertically aligned at the top without overflowing onto new lines. Make sure to maintain overflow: auto on your div #c1 or append overflow-x: scroll to the div #tree to enable horizontal scrolling behavior.

For a demonstration, refer to the working snippet provided below:

[Working snippet example would be displayed here]

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

Obtain the child's identification number and include it in the parent element as an ARIA

I need assistance with a JavaScript (or jQuery) function to dynamically add an 'aria-labelledby' attribute to parent <figure> elements based on the ID of the <figcaption>. I have multiple images and captions set up in <figure>&l ...

Learn how to implement a captivating animation with JavaScript by utilizing the powerful Raphael Library. Unleash the animation by triggering it with either

My desire is to indicate this movement by triggering a mouse click or drag to the desired location. let myDrawing = Raphael(10,10,400,400); let myCircle = myDrawing.circle(200,200,15); myCircle.attr({fill:'blue', stroke:'red'}); let my ...

'Dynamic' styling in Next.js using conditional CSS

I am exploring ways to style elements based on their parameters. I have been searching for terms like "conditional css react" and "implementing conditional styles in react with css". Here is a snippet of my code: e.g. <div class="ChatComponent_ ...

Why should I set the width of the header to be larger than the body in order to fully cover it?

I've set my header width to 106% to span the entire page length, while the max-width for my body is kept at 95%. body { background-color: #333; max-width: 95%; font-family: ivyjournal, sans-serif; margin: 0; padding: 0; overflow-x: hidd ...

Set up npm and package at the main directory of a web application

Currently, I am in the process of developing a web application using node.js. Within my project structure, I have segregated the front-end code into a 'client' folder and all back-end logic into a 'server' folder. My question revolves a ...

Do I need to include a callback in my AWS Lambda handler function?

What is the function of the callback in the lambda handler? It appears to be utilizing the sns variable and I am looking to make some modifications to the variables. exports.handler = function(event, context, callback) { console.log("AWS lambda and ...

AngularJS $http.get('') is throwing an error because some parameters are missing

I am currently working on an application that utilizes OAuth for authentication, but I am facing some issues with passing the URL parameters correctly. Despite trying various methods, my API keeps returning an error stating that the username and password a ...

Angular 2: Navigating through submenu items

I have a question about how to route submenu elements in Angular 2. The structure of my project is as follows: -app ---login ---registration ---mainApp (this is the main part of the app, with a static menu and links) -----subMenu1 (link to some con ...

The React.js component search test encounters errors in locating components

I have been working on a React app that utilizes Redux and React-router. I am currently writing tests using React TestUtils, and I encountered an issue with the following test results: The first expect statement is successful: expect(nav).to.have.length(1) ...

Creating and accessing a temporary binary file using Node Js

Challenge I have been working on integrating the Google Text To Speech (TTS) service to save a generated binary audio file to Google Cloud Storage (GCS). Considering that saving a local binary file in Firebase's Cloud Functions environment may not b ...

Is it possible for me to generate values using PHP that can be easily read by JavaScript?

I'm currently building a website and I am facing some challenges when trying to incorporate JavaScript for real-time calculations. Here are my issues: Is there a more efficient way to avoid manually typing out the code for each level up to 90, lik ...

What are some ways to keep the text within the boundaries of the input box?

How can I prevent the letters from extending beyond the bar when typing a lot of characters? Your assistance in resolving this issue is greatly appreciated. <div id="tasks-container"> <div id="tasks-header"> ...

Categorize elements in an array based on a specific keyword

Struggling with my AngularJS (1) application, I can't seem to figure out how to split an array of items into separate arrays grouped by item. In simpler terms, I have an array with different items and I want to group them by their uuid like this: [ ...

Utilizing jQuery's each() function to create a seamless loop of background images in a

Struggling with looping the background image in a slick slider? Check out the code snippet below. var json = [ { "img_url": "https://via.placeholder.com/500x500?text=1" }, { "img_url": "https://via.placeholder.com/500x500?text=2" }, { "img_url": "ht ...

Troubleshooting issue with rendering <li></> using array.map: Map's return statement is producing undefined output

Trying to implement pagination in a React project and facing issues with rendering a set of li elements within an ul. The renderPageNumbers function is returning undefined in the console, even though I can see the array being passed and logging out each el ...

An unexpected type error occurred: Unable to read the undefined property 'map' when utilizing Highcharts

I am currently working on developing highcharts using data from Firebase. I came across a helpful example here: However, when I try to integrate it into my application, I encounter the following error: firebase.js:43 Uncaught TypeError: Cannot read pr ...

Issue: The component series.line does not exist. Please ensure it is loaded before using it in Vue Echarts

I added the line component, but I'm still encountering issues. I set up my project using vue cli 3 and referred to this guide, but I can't locate the vue.config.js file in my project. Therefore, I manually created a vue.config.js and placed it in ...

What causes my slider to speed up with an increase in items and slow down with fewer items in bxslider?

Find more information here jQuery('.homepage_slider').bxSlider( { minSlides: 1, maxSlides: 4, slideWidth: 200, slideMargin: 30, ...

Encounter a 401 error code while attempting to fetch data from CouchDB using an AJAX request

I attempted to make an AJAX call in a JavaScript file to fetch data from CouchDB. Unfortunately, I encountered a 401 error message: Failed to load resource: the server responded with a status of 401 (Unauthorized) This is an extract of my JavaScript cod ...

How can I modify the font size of h1 and h2 elements in CSS?

I am currently working with a WordPress twenty eleven theme and I am trying to adjust the size of my headings. Specifically, when I enclose my headings in h1 and h2 tags like so: <h1>My h1 heading </h1> <h2> My h2 heading </h2> The ...