Discover the true width of the unordered list structure Forest

One of my projects involves creating family trees using CSS and unordered lists. It works great, except when the tree is wider than the screen, causing it to wrap awkwardly. To solve this issue, I implemented the following CSS:

#treewrapper {
 margin: 10px; padding: 20px; width:auto; white-space:nowrap; overflow-x:scroll;
}
#tree {width:5000px;}

Unfortunately, this results in a huge #tree area even when the tree is narrower than the #treewrapper. My initial idea was to use JavaScript to adjust the width of ul.tree based on its content width after rendering. However, it seems that the ul element width is calculated as the width of #tree minus the padding.

Setting #tree's width to auto still causes wrapping issues when the screen is narrow. Is there a way, either through CSS or JavaScript, to dynamically adjust #tree's width to fit appropriately?

You can see a live example with #tree set to Auto at

Answer №1

To determine the width, you can utilize JavaScript and the .offsetWidth property. See below for an example:

document.getElementById('tree').offsetWidth;

Alternatively, you could achieve the same result by applying CSS properties like:

display: block;
float: left;

Using block elements will automatically factor in the padding as well.

Answer №2

Adjust the width settings for #treewrapper, .tree, and .tree ul to width:100%;. This change should resolve the issue you are experiencing.

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

Is it possible to make the body background image adjust its size to the browser window

Looking to change the body background using a jQuery script. The goal is to scale the background image to fit the browser window size. I've come across a script called , but it seems to affect the class img and not the background-img. Any suggestions ...

What steps should I take to create a script that can manipulate elements of a webpage when a specific DIV is visible on the screen?

I am working on customizing the carousel on my website roseannebarr.tumblr.com. My plan is to add a dedicated "About" button on the left side of the carousel. When this button is clicked, I want a "left" arrow to appear in its place. I am new to Javascri ...

Exploring the integration of mixins from .scss files within node_modules into our .tsx or .jsx files for enhanced styling

I am currently facing a challenge with importing a .scss file from one of the installed node_modules into my .tsx file. The scss file contains a mixin named @mixin make-embedded-control($className: embedded-control){.....} which has all the necessary css ...

Tips for preserving hyperlinks while elegantly containing them within a designated space

Today, while experimenting with new angular features, I encountered an issue that is shown in the link below: https://i.sstatic.net/p4pNc.png The "tags" on the page are expanding beyond the boundaries of their parent container (the white div). They wrap, ...

What is causing the Bootstrap 5 navbar to not collapse?

Struggling to compile Bootstrap 5 with sass, I have a feeling that I might be missing some important scss files. When I added the <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7 ...

What is the best way to achieve two columns of the same height, with the right column nested inside the left column?

Is there a way to create two equal height columns with the right column inside the left column using pure CSS? I have explored different methods, but due to the lack of a parent selector in CSS, I haven't been successful so far. My goal is to have ex ...

Issues with div and hyperlinks/forms

I am currently working on creating a left sidebar in my HTML code, but I am still new to this. In the div for the sidebar, I have three buttons. However, Weebly keeps notifying me of an issue with the <a> tag, and the second button's link appear ...

"Error message stating 'Unresolved variable' occurs in the code while trying to assign className={styles.nameOfClass

My current task involves adjusting the style of a button in ReactJS using the "className" property. However, I encountered an error message in WebStorm stating "Unresolved variable nameOfClass," and the desired style changes did not reflect when running we ...

Formatting Anchor Tags Inside a Table Data Element

My existing CSS section is as follows: .leftMemberCol { width:125px; vertical-align:top; padding: 13px; border-width:0px; border-collapse:separate; border-spacing: 10px 10px; text-align:left; background-color:#f2f3ea; } I have a td section (a left sideb ...

Interactive Content Swapping: How to Use Javascript for Dynamic Link Updates

http://jsfiddle.net/bUjx7/42/ <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'> </script> <script type='text/javascript'> $(document).ready(function () { $('.fieldreplace ...

Placing a pseudoelement amidst the track and thumb of a range input type

I'm trying to position a pseudo element with a specific z index that is lower than the thumb of an input type range but higher than its track. This is what I have so far: HTML: <div class="range"> <input type="range" name="" class="progre ...

Unable to change background image to an external image link

<div class="flipbook-viewport"> <div class="container"> <div class="flipbook"> <div style="background-image:url("https://example.com/images/lareg/1.JPG")">< ...

Use CSS to insert a solid rectangle into the contents of a table cell

Struggling with the HTML code that defines a table? Here is an example: <table> <th> <td style="width:200px"> col1 </td> </th> <tr> <td id="a1"> text1 </td> </t ...

What is the best way to retrieve values from li elements using Cheerio/jQuery?

<div class="tabs__content tabs__content--bg js-tab-panel"> <div class="tabs__panel tabs__panel--active"> <div class="product__sizes-wrapper"> <ul class="product__sizes-select js-size-select-l ...

The CSS dropdown menu ends up being positioned underneath the slideshow

Currently, I am working on creating a CSS dropdown menu using CSS3. However, I have encountered an issue where the dropdown menus are going under the slideshow on the page when hovered, making the dropdown items invisible. I am unsure whether I should fix ...

The CSS Image Gallery refuses to be centered

.imageContainer{ width: 100%; margin: 0 auto; position: relative; text-align: center; } .imageContainer img{ width: 250px; height: 250px; float: left; } .imageContainer img:hover{ opacity: 0.60; } I am having trouble gett ...

How can I utilize jQuery to iterate through every anchor tag on an HTML page?

I am looking to reference all anchor tags on the page that have a parent h2 tag. To achieve this, I need to iterate through each anchor tag that has a parent h2 and add an attribute using jQuery. <body> <h1>not me</h1> <a href ...

Update Button Visibility Based on State Change in ReactJS

Currently, I'm utilizing a Material UI button within my React application. Despite changing the state, the button remains visible on the screen. Here's the relevant code snippet: function MainPage() { const state = useSelector(state => sta ...

On smaller device screens, the full-height sidebar and content area experience some disruption

How can I resolve the height issue with sidebar and content area on smaller devices, maintaining 100% height for both? I'm having trouble fixing it with the current code. I am aiming to create a fixed-width sidebar that expands to full width on small ...

Avoid displaying the identical div through consecutive random fade-ins and fade-outs

I'm trying to make a JavaScript function that selects a random div from a list of divs, displays it for a few seconds, fades it out, and repeats this process in a loop. The issue I'm facing is that sometimes the same div is selected consecutively ...