CSS Style for Organizing Content

I'm struggling to create CSS for a hierarchical display, resembling a tree structure of files and folders. Currently, I am using nested unordered lists in my HTML:

<ul>
  <li>animals<ul>
    <li>dogs</li>
    <li>cats</li>
  </ul></li>
</ul>

Although they are displayed nicely with appropriate CSS styling, the missing element is connecting lines between parent and child items. To achieve this, I have added the following CSS code:

ul ul li:before {
  content: "";
  padding: 15px;
  border: 1px solid #000;
  border-width: 0 0 1px 1px;
  border-radius: 0 0 0 10px;
  position: absolute;
  left: -22px;
  top: -15px;
}

The issue arises when these lines overlap with the icons representing animals, dogs, and cats. I attempted adjusting z-index values but to no avail. Is there an alternative method using CSS to solve this problem? Perhaps, is there a way to correctly manage z-indexes in this scenario?

Answer №1

Take a look at this demonstration. You can easily swap out the salmon colored box with images:

HTML Code

<div>
    <h3>Main Directory</h3>
    <ul>        
      <li>Folder A
        <ul>
            <li>Folder B
                <ul>
                    <li>file1.docx</li>
                    <li>file2.pdf</li>
                    <li>file3.png</li>
                </ul>
            </li>
            <li>file.jpg</li>
          </ul>
      </li>
      <li>file.doc</li>
      <li>file.txt</li>
    </ul>
</div>

CSS Styling

body {
    margin: 30px;
    line-height: 2.5;
    font-family: Arial, sans-serif;

}

div {
    position: absolute;
}

ul {
    text-indent: .6em;
    border-left: .35em solid black;
}

ul ul {
    margin-top: -1.45em;
    margin-left: 0.8em;

}

li {
    position: relative;
    bottom: -1.5em;
}

li:before {
    content: "";
    display: inline-block;
    width: 2.5em;
    height: 0;
    position: relative;
    left: -.9em;
    border-top: .3em solid black;
}

ul ul:before, h3:before, li:after {
    content: '';
    display: block;
    width: 1.2em;
    height: 1.2em;
    position: absolute;
    background: teal;
    border: .35em solid white;
    top: 1.2em;
    left: .7em;
}

h3 {
    position: absolute;
    top: -1.15em;
    left: 1.8em;
}

h3:before {
    left: -2.7em;
    top: .8em;
}

View Demo

Answer №2

CodePen Example

If you're searching for a tutorial that covers the exact concept, I recommend checking out this older online guide.

The tutorial utilized images for custom bullet points, but in your scenario, you can opt for using a pipe character (|) with a larger font size and preferred color.

Visual Representation:

UPDATE:

I've included an additional CodePen to replicate the stylish curved lines in your design.

CodePen Example 2


UPDATE 2:

Here's a revised version of the original CodePen that introduces an escaped space for better visibility without compromising the elegance of your connecting lines.

CodePen Example 3 and 3b

UPDATE 3: This specific type of blank space mentioned is suggested for utilizing the `content` property as demonstrated in the code snippet below:

Entity  Name Symbol/Description
&#8194; &ensp;   en space

To clarify, the special blank space indicated is positioned right in between the other two blank spaces under "Symbol." By employing it, there's no need for an alternative character or transparency to mimic empty space. Removing the transparency attribute ensures compatibility with IE8. As a precaution, the empty line beneath consists of just a single special blank space character. Copy it from here for usage, since 'Entity' and 'Name' won't function:


UPDATE 4: Another approach following Update 3 is exploring Stack Overflow responses provided for Incorporating HTML entities through CSS content.

Answer №3

Here’s a simple example to illustrate:

The HTML Code

<ul>
  <li>animals
   <ul>
    <li>dogs</li>
    <li>cats</li>
  </ul></li>
</ul>​

CSS Styling

li {
  border-left: 1px solid #000;
  margin-left: 10px;
  margin-top: 5px;
  padding-left: 10px;
}​

View the jsFiddle result here

Answer №4

When it comes to TreeView or hierarchical data, the usual approach is to gather them dynamically. In my case, I generate the data using PHP on the server-side from a database or filesystem. I required a more structured solution that would allow me to manipulate the data using JavaScript on the client-side.

For me, utilizing a table with colspan for variable indent was the way to go. To handle similar cells consistently (by applying the same class attribute), I used to include the same number of TD elements in every TR, marking the unused ones as display:none. This enabled me to maintain uniformity and facilitate formatting with methods like nth-child(..).

The missing elements include item-IDs per TR to establish links to the database for management purposes, as well as the incorporation of JavaScript functionalities.

In this example, different colors are assigned to aid in identifying placement errors during testing.

Here's a screenshot from FireFox Mint 104 for reference

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="webdev (at) kgsw (dot) de">
<meta name="description" content="2022-09-01/kg: Example hierarchical view (tree view)">
<meta name="keywords" content="html, html5, css, tree view, table">
<style>

body {
  font-family: sans-serif;
}

table { 
  border-collapse:collapse; 
  cursor:default; }

td,th { 
  font-size:12px;
  border:1px solid lightblue; /* transparent; */
  line-height: normal;
  padding: 0;
}

td { display:none; }
td.symbol, td.line, td.indent { display:table-cell; }

td.line { color:blue; }
td.symbol { color:green; }
td.indent { color:grey;font-size:8px; }

td:nth-child(12) { display:table-cell; width:70%; }  /* items */
td:nth-child(13) { display:table-cell; min-width:80px; } /* remarks */

td:hover { background-color:#dec; }
td.line:hover { background-color:transparent; }
td.symbol:hover { color:red; }

</style></head><body>

<table>

<tr>
  <td class="indent"> #</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="11"> <small>ROOT</small></td><td> :remarks</td>

</tr><tr>
  <td class="indent"> 1</td><td class="line">&#9500; </td><td class="symbol">&#9634;</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="9"> Item1</td><td> </td>

</tr><tr>
  <td class="indent"> 2</td><td class="line">&#9474; </td><td class="line">&#9500; </td><td class="symbol">&#9634; </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="8"> Item1.1</td><td> </td>

</tr><tr>
  <td class="indent"> 3</td><td class="line">&#9474; </td><td class="line">&#9492; </td><td class="symbol">&#9634; </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="8"> Item1.2</td><td> </td>

</tr><tr>
  <td class="indent"> 4</td><td class="line">&#9500; </td><td class="symbol">&#9634;</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="9"> Item2</td><td> </td>

</tr><tr>
  <td class="indent"> 5</td><td class="line">&#9500; </td><td class="symbol">&#9634;</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="9"> Item3</td><td> </td>

</tr><tr>
  <td class="indent"> 6</td><td class="line">&#9492; </td><td class="symbol">&#9634;</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td colspan="9"> Item4</td><td> </td>

</tr><tr>
</tr>
</table>

<hr>

<cite><p>By changing the general class (display) to 'skip' and defining only the utilized TDs as 'table-cell', improvements were made.</p>
</cite>

</body></html>

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

HTML5, canvas covering every element

Below is the code that I am working with: <html> <head> <title>canvas</title> </head> <body> <canvas width="1745" height="569" style="width: 1730px; height: 1680px; position: absolute; z-index: 1"></canvas> ...

What is the best way to update my logo and incorporate a colored border at the bottom of my fixed header while the user is scrolling down?

After spending countless hours researching online, I've been struggling to implement header effects on scroll without using JavaScript. My goal is to achieve a simple effect where the header reduces in height, the logo changes, and a colored border is ...

Issue arises when applying both overflow-x:scroll and justify-content:center

Encountering a problem with using overflow-x: scroll and justify-content: center on a flex parent container. Here is my code snippet: The issue: the first flex child item is not visible as it is cropped on the left side. Please refer to the screenshot and ...

Resolving issues with CSS placement and resizing

I have been considering developing a UI toolkit that offers an intuitive and powerful way of setting the position and size of elements/widgets. Here are some examples of how it could be used (although they are not currently implemented): ui("Panel").size( ...

Using Jquery to store input values from within <td> elements in an array

I'm trying to capture user input from a dynamically changing table with 2 columns. How can I retrieve the data from each column separately? The size of the table is adjusted by a slider that controls the number of rows. Below is the structure of my ta ...

Tips for presenting random images from an assortment of pictures on a webpage

I'm looking to enhance my website by adding a unique feature - a dynamic banner that showcases various images from a specific picture pool. However, I'm unsure of how to find the right resources or documentation for this. Can you provide any guid ...

Equal-height columns in a responsive grid system

I'm currently facing an issue with a layout that seems simple I am working on a design featuring 4 headline boxes. In the "desktop" view, all 4 boxes need to be of equal height. The same applies when viewed across 2 boxes in the "tablet" mode. Howeve ...

Advanced data synchronization with Angular 7's bidirectional data binding feature

One input box allows users to enter a number, and I would like to adjust the width of the background color based on that number. The box itself will have a fixed width, but I am looking to dynamically change the width of the background color according to ...

The design of iOS7 Safari becomes distorted when the orientation changes and an input field is in focus

It appears that there is a major issue in iOS7 on iPhone and iPad devices, and possibly on other versions as well. When you focus on an input field, causing the keyboard to open, the layout experiences a severe and irreversible disruption upon changing or ...

Tips for modifying the color of the last anchor in a list item

li elements are utilized to create breadcrumb in a shopping cart. The breadcrumb displays all anchor elements using the style defined for an 'a' element from site.css. How can I make the last anchor's color black? I attempted the style belo ...

Looking for a way to upload only part of a large file using HTML and PHP

Is it possible to create a PHP script that can upload only the first 1 MB of a very large file? Can this be achieved with a standard form upload in PHP by closing off the connection after 1 MB is uploaded? I have researched various uploaders (HTML5/Java ...

Incorporating a spectrum of hues into an HTML document

As a beginner in HTML, I recently stumbled upon a website (YT video) showcasing a stunning array of colors on buttons. You can check it out here: https://www.youtube.com/watch?v=OPaLnMw2i_0 I'm curious if these colorful designs can be applied to text ...

The Bootstrap navbar collapses and vanishes instantly

Whenever I tap on the navbar icon on my mobile or tablet, the navbar opens briefly before disappearing. Even though the navbar closes visually, it still indicates that it is open. When I tap the navbar icon again to close it (without seeing it actually clo ...

Problem with looping through arrays in JQuery using .each() function

Looking for some help with jQuery and JavaScript, please bear with me if my question seems naive. I am attempting to load images from different folders into corresponding divs to create a gallery. <div id="menu"> <div class="gallery" id="div0"&g ...

Utilize AngularJS to create a custom tag with a directive included

I've been working on creating a custom tag for a reusable component in a web page, but I seem to have hit a roadblock. It's not functioning as expected, and I feel like I might be overlooking something. If anyone could provide some guidance or p ...

A method for expanding the menu upwards to make room for newly added items

Looking at the images below, I have a menu that needs new items added to it while maintaining the position of the lower-left corner. Essentially, each time an entry is added, the menu should expand upwards from "the upper-left corner" while keepi ...

Having trouble with my unique 404 page templates not functioning properly in Django

I'm having trouble implementing my custom 404 and 403 error pages for my Django project. I've previously used the same code in other projects, but it's not functioning as expected this time. Here is the function I am using: def error_404(re ...

Using CSS to automatically convert a table into one column layout with labels from the thead section

Is it possible to automatically create the labels for data when converting a multi-columns table into one column using CSS and taking the information from the thead section? This resource discusses how to convert a multi-column table into a single column ...

Ways to trigger the display of an image upon hovering over a button without using inheritance

<div class="a"> <img class="img_c" id="img_id" src="~~"> <div class="b"> <div class="c"> <ul class="d" id="e"> <li ~~~ > </ul> </div> </div> </div> Is there a way to display the ...

Is it possible to adjust the CSS code linked to the HTML tag based on the specific webpage being viewed?

I am facing an issue with the homepage of my website, which uses Scrollmagic.js for smooth scrolling. In order for the sticky footer CSS to work properly on all other pages, the HTML tag needs to have a height of 100%. However, if I add this height value t ...