`<li>` - a list element aligned on the same line as the text before it

I am currently working on customizing an HTML logfile that allows specific items (like exceptions) to be collapsed. To achieve this, I have utilized an unordered list with several li elements. However, I am facing an issue where the first li element is not displayed inline with the previous text (in this case, the timestamp).

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

For reference, I have created a JSFiddle demonstrating this problem: https://jsfiddle.net/rs358vw8/

<div class="debug">
    <span class="timestamp">[09:33:04.137] </span>
    <span class="message">
        <ul class="tree">
            <li><a href="#" class="exception">Exception message</a>
                <ul>
                    <li><a href="#" class="exception">Innereception</a></li>
                    <li><a href="#" class="exception">Stacktrace</a></li>
                    <li><a href="#" class="exception">Bla</a></li>
                </ul>
            </li>
        </ul>
    </span>
    <br>        
</div>

Is there a way to ensure that the first li element is displayed inline with the timestamp/text?

Answer №1

To style your ul element, use the following CSS to make it display as an inline-block with vertical alignment at the top of the line:

ul.tree {
    display: inline-block;
    margin: 0 0 0 10px;
    padding: 0;
    vertical-align: top;
}

Check out this demo on JSFiddle: https://jsfiddle.net/sd25muvn/1/

Answer №2

Check out the updated version of your JSFiddle here: https://jsfiddle.net/qzEjk9Pm/

I made a few modifications to the .timestamp styling:

.timestamp {
    font-size: 14px;
    color: #888 !important;
    float: right;
    padding: 0 10px 0 5px;
}

Answer №3

you can utilize display:flex property

var tree = document.querySelectorAll('ul.tree a:not(:last-child)');
for(var i = 0; i < tree.length; i++){
tree[i].addEventListener('click', function(e) {
var parent = e.target.parentElement;
var classList = parent.classList;
if(classList.contains("open")) {
classList.remove('open');
var opensubs = parent.querySelectorAll(':scope .open');
for(var i = 0; i < opensubs.length; i++){
opensubs[i].classList.remove('open');
}
} else {
classList.add('open');
}
});
}
body {
    background: #222;
    font-family: Consolas;
    font-size: 11px;
}
.debug{
  display:flex;
}
.timestamp {
    color: #999 !important;
}

.message {
    color: #6abaf5 !important;
}

.debug {
opacity: 0.7 !important;
}

.exception {
    color:red !important;
font-weight: bold !important;
}

.exception .stack {
color: #ff0000 !important;
}


/* Collapsible Tree */
ul.tree {
display: flex;
margin: 0;
padding: 0;
}

ul.tree li {
    list-style-type: none;
    position: relative;
}

ul.tree li ul {
    display: none;
}

ul.tree li.open > ul {
    display: block;
}

ul.tree li a {
    text-decoration: none;
}

ul.tree li a:before {
    height: 1em;
    padding:0 .1em;
    font-size: .8em;
    display: block;
    position: absolute;
    left: -1.3em;
    top: .2em;
}

ul.tree li > a:not(:last-child):before {
    content: '+';
}

ul.tree li.open > a:not(:last-child):before {
    content: '-';
}
<div class="debug">
<span class="timestamp">[09:33:04.137] </span>
<span class="message">
<ul class="tree">
<li><a href="#" class="exception">Exception message</a>
<ul>
<li><a href="#" class="exception">Innereception</a></li>
<li><a href="#" class="exception">Stacktrace</a></li>
<li><a href="#" class="exception">Bla</a></li>
</ul>
</li>
</ul>
</span>
<br>
</div>

Trust this method proves to be useful

Answer №4

Include the following display:inline rule and adjust padding, margins, etc. as needed for your elements:

ul.tree, ul.tree > li {
  display: inline;
  margin: 0;
  padding: 5px;
}

Demonstration below

var tree = document.querySelectorAll('ul.tree a:not(:last-child)');
for (var i = 0; i < tree.length; i++) {
  tree[i].addEventListener('click', function(e) {
    var parent = e.target.parentElement;
    var classList = parent.classList;
    if (classList.contains("open")) {
      classList.remove('open');
      var opensubs = parent.querySelectorAll(':scope .open');
      for (var i = 0; i < opensubs.length; i++) {
        opensubs[i].classList.remove('open');
      }
    } else {
      classList.add('open');
    }
  });
}
body {
  background: #222;
  font-family: Consolas;
  font-size: 11px;
}
.timestamp {
  color: #999 !important;
}
.message {
  color: #6abaf5 !important;
}
.debug {
  opacity: 0.7 !important;
}
.exception {
  color: #ff0000 !important;
  font-weight: bold !important;
}
.exception .stack {
  color: #ff0000 !important;
}
/* Collapsible Tree */

ul.tree, ul.tree > li {
  display: inline;
  margin: 0;
  padding: 5px;
}
ul.tree li {
  list-style-type: none;
  position: relative;
}
ul.tree li ul {
  display: none;
}
ul.tree li.open > ul {
  display: block;
}
ul.tree li a {
  text-decoration: none;
}
ul.tree li a:before {
  height: 1em;
  padding: 0 .1em;
  font-size: .8em;
  display: block;
  position: absolute;
  left: -1.3em;
  top: .2em;
}
ul.tree li > a:not(:last-child):before {
  content: '+';
}
ul.tree li.open > a:not(:last-child):before {
  content: '-';
}
<div class="debug">
  <span class="timestamp">[09:33:04.137] </span>
  <span class="message">
        <ul class="tree">
            <li><a href="#" class="exception">Exception message</a>
                <ul>
                    <li><a href="#" class="exception">Innereception</a></li>
                    <li><a href="#" class="exception">Stacktrace</a></li>
                    <li><a href="#" class="exception">Bla</a></li>
                </ul>
            </li>
        </ul>
    </span>
  <br>
</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

Retrieve the initial image link from a blogger's post in cases where it is not being stored on the Blogger platform for use in related

I am currently using a hosting service to store the images that I include on my blogger platform. However, I have encountered an issue where blogger does not automatically fetch the image url to use as the thumbnail when the image is hosted externally. C ...

tips for incorporating margin without sacrificing responsiveness

Having an issue with three cards in a row, they are too close together without any margin. It doesn't look good, so I tried adding margin, but it messes up the responsiveness that Bootstrap provides. So, I decided to add padding only. Any suggestions ...

A guide on traversing a HTML table and cycling through its contents with JavaScript

I'm currently in the process of constructing a grid with 20 rows and 20 columns of squares, but I am encountering difficulties with looping through table values to effectively create the grid. For more detailed information on the html code, please se ...

The AJAX requests in my project are failing to trigger upon touch events when using the jQuery Plugin

Ensuring that both <script src="hammer.js"></script> and <script src="../jquery.hammer.js-master/jquery.hammer.js"></script> have been correctly included in my header. I have a mouse click-triggered AJAX event for dynamic and depen ...

Flexbox isn't lining up items horizontally as expected

I have been researching flexbox and it seems like it should display in a horizontal row by default. However, no matter what I try, I can't seem to get it to work as expected. I've included the code below. When I remove "display: flex;" from the C ...

Express Node + Styling with CSS

I am in need of a single EJS file (configuration includes Express and Request). app.get('/space', function(req, res){ res.render('space.ejs'); }); The file successfully renders, but only two out of three CSS stylesheet links work ...

Using radio buttons to toggle the visibility of a div element within a WordPress website

I am currently working on creating a WordPress page using the custom page tool in the admin interface. My goal is to have 3 radio buttons, with 2 visible and 1 hidden. The hidden button should be automatically checked to display the correct div (although ...

Tips for creating a responsive background image that adjusts after resizing the window to a certain width

Is there a way to create a responsive background-image that adjusts when the window is resized to a specific width, similar to the main image on ? ...

Removing from MySQL database results in PHP form not being shown

I'm having trouble figuring out why my form isn't showing up. Below is the code for PostDelete.php: <?php session_start(); $username=$_SESSION['uname']; // Process delete operation after confirmation if(isset($_POST["pid"]) &a ...

Unable to view HTML without an internet connection

I have encountered an issue where my files work fine when uploaded to an online server, but do not work when accessed locally offline. An error message about a cross-origin issue appears. How can I solve this problem? Error message: Security Error: Conte ...

Utilize the CSS ~ selector to target the first child element

I have a scenario where I want to apply CSS to the first child only if a second or other child exists in the parent. The '~' selector can achieve this, but it applies the style to siblings of the first child, and I need it the other way around. ...

What is the best way to format specific text as bold within an input text field?

I am attempting to make certain text bold within an input text field. However, I'm uncertain about how to achieve this because HTML code is not recognized inside a text field. Therefore, using <b> will not be effective. Is there a way to bold sp ...

Shift the column upwards for devices with smaller screens

I need help with a layout issue I am facing. Currently, my layout looks like this: [blurb] (8) [form] (4) However, I want the [blurb] to take up col-md-8 and the [form] to take up col-md-4 so that they are full width on smaller devices. On smaller devic ...

Is it possible to create a table using a loop in an SQLite query?

Welcome In the company where I work, Excel is currently being used to manage a large database of items for cost estimation purposes. To simplify this process, I am developing an Electron App that will replace Excel with a more user-friendly interface and ...

Guidelines for accurately and appropriately creating divisions in JavaScript using mathematical principles

The program occasionally generates mathematically incorrect divisions, such as 2/0, and does not accept rounded answers like 0.33 or 0.33333 for "1/3". What modifications should I make to the code? Thank you for your assistance and time. document.getEl ...

Out of the DIVs floating left and right, which one is positioned first?

Is the order of <div> elements in HTML important? If it is, what is the reason behind it? .float-left{float:left;} .float-right{float:right;} <div> <div class="float-left">Left</div> <div class="float-right">Right</ ...

Instructions on integrating iframe into Slides on material-auto-rotating-carousel

Looking to swap out the carousel slide image with a loom video. Any ideas on how to replace the media img with an iframe in material-auto-rotating-carousel? const AutoRotatingCarouselModal = ({ handleOpen, setHandleOpen, isMobile }) => { return ( ...

I'm noticing that my Tailwind styles don't take effect until I redefine them. What could be causing

My Tailwind classes are giving me trouble. I faced an issue when I created a new component in the directory and placed my Button component there. Whenever I restart the project in terminal, the styles do not apply to my button unless I manually define th ...

Tips for saving input data from an HTML page to a text file

How can I save the inputs from a simple form as a text file using HTML code? Here is an example of the HTML code: <div id="wrapper"> <div class="main-content"> <div class="header"> </div> ...

The background remains hidden from view as a result of its positioning

There seems to be an issue with the background color of my list not displaying correctly. The problem arose after floating elements left and right. Any suggestions on how to resolve this? If you'd like to take a look, here's the link to the fidd ...