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

Changing the properties of the admin bar when it is hovered over in

After successfully styling a button on the admin bar, I noticed a few imperfections in the implementation. The button in question is situated on the admin bar and is labeled "maintenance". Upon clicking the button, jQuery triggers the addition of the clas ...

I am facing difficulties with the header in CSS as it is not resizing properly

I'm having trouble getting my mobile-first design to work properly because the header doesn't resize in mobile view. I'm satisfied with how it looks in full-screen, but I haven't been able to make any media queries work. You can downloa ...

What is preventing me from extracting the callback function from the app.get method in Node.js and Express?

In my server.js file, I'm attempting to do the following: // This code is not functioning as expected, and I am unsure why. var findBooks = function (err, books) { if (!err) { return response.send(books); } else { console.log( ...

Unexpected issue on AngularJS application

Encountering issues with incorporating a controller into my page - each time I add it to a DOM element, an error is thrown. Here's the structure of my HTML page: <html data-ng-app="sportsStore"> <head> <title>Sports Sto ...

Having trouble retrieving a customized header from the HTTP response

I've encountered an issue with my Node.js server where I set a custom header. I added the Access-Control-Expose-Headers to allow access from browsers, and it works fine in Chrome and Firefox. However, I'm getting an error in PhantomJS saying "Ref ...

Ajax not functioning after submission of form

I currently have this code snippet: $('#my_form').submit(function () { setTimeout(function () { console.log('1'); $.ajax({ type: "GET", url: "/CorrectUrl/CorrectUrl", ...

What is the recommended library for managing a task queue in this project?

Is there a library or package available for Node.js that can help me create a task queue with a fixed timeout between the start of each task and an overall callback that is triggered after all tasks have finished? https://i.stack.imgur.com/j1wCY.jpg ...

Sending variable boolean values to a VueJS component

How can I assign dynamic properties to a VueJS Component using VuetifyJS? Below is an example of VuetifyJS code that constructs a select field element: <div id="app"> <v-app id="inspire" style="padding: 10px; "> ...

D3: Ensuring Map is Scaled Correctly and Oriented Correctly

I am attempting to integrate a map into a website using D3 and topoJSON that resembles the following: However, when I create the map with D3/topoJSON, it shows up small and inverted. Even after exploring various solutions (like Center a map in d3 given a ...

Trouble with Directive - the watch function isn't functioning as expected

After spending countless hours and trying almost everything, I can't seem to get it to work... I have an Angular directive that calls another Angular directive. I need to handle events in the child directive. So my child directive looks like: ...

Azure SQL Server linked with a REST API built on Express.js/Node.js encountered an error stating: "TypeError: req.sql is not a function"

Having trouble connecting to Azure SQL Server with express4-tedious. I'm working on building an app in react-native with a Node/Express server (REST API), but encountered this error after setting up express4-tedios in Express: req.sql is not a functio ...

Choosing specific rows in a kogrid by clicking on a button within a column

My kogrid includes a single column with a view button for each row. I would like to show a popup containing the values of the selected row when the View button is clicked. How can I retrieve the values of the selected row in order to pass them into the p ...

Data retrieval takes precedence over data insertion

I'm facing an issue where I am trying to insert data into MySQL using Knex within a loop, but the data retrieval is happening before the insertion. Can anyone assist me with this problem? for (let i = 0; i < fileArray.length; i++) { fileLocation ...

Console command to change paragraph tag color---Modifying the color

I am attempting to change the color of all paragraph tags on a webpage to white by utilizing the console. My initial attempt was: document.p.style.color = 'white'; However, this method did not have the desired effect. Interestingly, I have had s ...

Pretending to determine the exact height of the body

I am facing a challenge with my JavaScript application that is loaded within an iframe through a Liferay portlet. The HTML container is currently empty and the JS is loaded only when the document is fully loaded. Upon loading the page in the iframe, Lifer ...

Is there a way for me to execute a function multiple times in a continuous manner?

I am attempting to create a blinking box by calling a function within itself. The function I have is as follows: $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeToggle("slow"); }); }); <script src="https://a ...

Header displayed on each page. (WordPress)

(my website: ) I am using Wordpress.org and I am trying to create a button that will redirect the user back to the front page, but I am having trouble figuring out how to do it. The button should be located in the top left corner of the site and should not ...

Ways to fetch additional Product Cards using JSON and a nextPage parameter

I'm facing difficulties in nesting a fetch inside another fetch. I'm not sure if this is the correct approach, but my goal is to utilize Vanilla JavaScript to fetch another JSON from the nextPage URL within the JSON list when the "load more" butt ...

Retrieving HTML content from Wikipedia's API using JavaScript

I am encountering an issue where every time I attempt to log data to my console, an error occurs. Could someone kindly point out what may be the problem with the code? My objective is to actually showcase the html content on a webpage. Below is the code ...

Passing the $scope variable between functions

Here is the function in my angular controller: $scope.selectinteresteduser = function(Id){ $scope.selecteduserid = Id; } $scope.sendMessageToEmployeer = function($scope.selecteduserid) { alert($scope.selecteduserid); } I am looking for a way to pa ...