Looking to display a submenu next to its parent element

I have a list that is not in order:

<ul>
 <li>Parent-1
  <ul>
    <li>Child-1</li>
    <li>Child-2</li>
    <li>Child-3</li>
  </ul>
 </li>
 <li>Parent-2
  <ul>
    <li>Child-4</li>
    <li>Child-5</li>
  </ul>
 </li>
 <li>Parent-3
  <ul>
    <li>Child-6</li>
  </ul>
 </li>
</ul>

My goal is to show these items in a single line like this:

Parent-1 | Child-1 | Child-2 | Child-3 | Parent-2 | Child-4 | Child-5 | Parent-3 | Child-6

How can I achieve this using CSS or possibly with the assistance of jQuery? It seems simple, but I am having trouble figuring it out.

Answer №1

Although this may not be an exact match for what you need, it should give you a good starting point. Try implementing the following CSS to your list items and unordered list:

ul, li {
    list-style-type: none;
    display: inline-block;
}

To see the changes in action, feel free to view the live demo here: http://jsfiddle.net/Cvf33/

Answer №2

To achieve this layout, you can utilize the display: inline-block; property and value. This CSS property instructs the element to act like a paragraph by aligning from left to right on a single line until the available width is exhausted, at which point it wraps onto a new line.

This effect can be accomplished by setting the UL elements to inline-block, causing them to be aligned next to each other. By applying the same inline-block to the LI elements inside the ULs, they are then aligned in a horizontal manner. To observe the different outcomes, try removing the display: inline-block; declaration in the specified selectors below.

Inline-block is compatible with IE8 and later versions. For more information on browser compatibility, refer to this Can I Use It link.

Please note that the additional CSS styles (height and background) are purely for visual representation and do not affect the functionality of the code

Explore an interactive demonstration on JSFiddle: Demo JSFiddle

ul {
    display: inline-block;
    height: 40px;
}

ul li { 
    display: inline-block;
    height: 40px;
    background: red;
}

ul li ul.sub-menu li {
    background: blue;
}

Answer №3

Check out this styling:

<style>
ul, li {display: inline-block;}
ul, li {margin:0; padding: 0; text-align: left;}
li + li::before {
    content: " | ";
}
ul::before {
    content: " | ";
}
.container::before {
    content: " ";
}
ul li {font-weight: bold;}
ul li ul li {font-weight: normal;}
</style>

<ul class="container">
 <li>Parent-1
  <ul>
    <li>Child-1</li>
    <li>Child-2</li>
    <li>Child-3</li>
  </ul>
 </li>
 <li>Parent-2
  <ul>
    <li>Child-4</li>
    <li>Child-5</li>
  </ul>
 </li>
 <li>Parent-3
  <ul>
    <li>Child-6</li>
  </ul>
 </li>
</ul>

Take note that I assigned the outer-most ul a class name of "container" to eliminate the initial "|"

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

Objective subject for an element within a :not operation

I have a function that specifically excludes a style on links by targeting their IDs. Each of my links has an ID, so I use that to exclude the style. a:not([id='hopscotch_logo'] { color: red; } Now, I also want to find links that are children o ...

Dealing with a missing response in an Ajax Server-Side Call: The .done(function(data){..} function remains inactive

Let's say I make an Ajax server-side call using jQuery like this: $.ajax({ url: "/myapp/fetchUser?username=" + username, type : "get", dataType : "json", data : '' }).done(function(data) { co ...

JavaFX WebEngine pauses until ajax is finished

Currently, I am working on a JavaFX data mining application that heavily relies on WebView and WebEngine. The entire mining process involves two main steps. Firstly, the user navigates to a specific website using the UI in WebView to configure the search f ...

Using Angular2, you can dynamically assign values to data-* attributes

In my project, I am looking to create a component that can display different icons based on input. The format required by the icon framework is as follows: <span class="icon icon-generic" data-icon="B"></span> The data-icon="B" attribute sp ...

Set the div element to be horizontally aligned

Is there a way to make this display horizontally instead of vertically, from left to right rather than top to bottom? I attempted using display:flex but only the extra-text class is affected and not the outer div. https://i.stack.imgur.com/2DNoC.png .m ...

Tips for showing menu only when a user scrolls on your WordPress site

I've been working on creating an effect where the menu stays hidden until the user starts scrolling. However, I can't seem to figure out why my code is not producing the desired effect. Here is the jQuery code snippet I am using: <script src= ...

Steps to create a loading bar that starts loading when an ajax task begins

What is the best way to show a loading bar as an AJAX task begins on a page? Additionally, how can we make sure that hitting the back button will return to what was being viewed before that specific AJAX task? ...

Retrieve the modal ID when the anchor tag is clicked in order to open the modal using PHP

I am facing an issue with opening a modal and passing the id value using JavaScript. The id value is shown in a hidden input field. <a href="#modal2" data-toggle="modal" data-id="<?php echo $CRow['id'];?>" id="<?php echo $CRow[& ...

Is there a way to customize CKEditor to prevent it from continuously adding <p></p> tags within the textarea automatically?

As I was going through the CKEditor tutorial, I implemented the following: $( '#editor' ).ckeditor(function(){}); //it's working great!! However, after submitting the form, I noticed that by default, the textarea displays <p></p ...

Seeking guidance on utilizing JavaScript for implementing an onclick event

I am exploring the realm of Event tracking with Google Analytics. To make this happen, I know that I must include an onclick attribute to select links (those specifically requested for tracking) in the following manner: <a href="http://www.example.com" ...

Does the rendered ID of an ASPX control always appear the same in the source HTML code?

Let's say I have an aspx textbox with id="txtkms". In the HTML view source, it appears as ContentPlaceHolder1_Gridview1_txtkms_1. I'm curious if this control will always be rendered as ContentPlaceHolder1_Gridview1_txtkms_1 every time I run my as ...

Developing a functionality to search for specific values within JSON properties

Hello, I have a question about implementing a partial search on a specific property within JSON data. Basically, what I'm trying to achieve is that if I type in "Jac" into the input field, it will search the JSON data for similar strings like Jacob, J ...

The Bootstrap tooltip effectively fades away after displaying text, but it is not positioned correctly above the icon

Having some issues with my tooltip functionality. It seems to display the text on the left side and fades away upon mouseover, but it doesn't show up in a proper tooltip box over the icon as expected. I suspect that there might be a conflict between j ...

How can one append a string of text to the right of each bar? Let's find out!

.chart div { font: 10px sans-serif; background-color: steelblue; text-align: right; padding: 3px; margin: 1px; color: white; } </style> <div class="chart"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5 ...

Sending form data to a CFC asynchronously in Coldfusion

To begin with, I want to mention that the product I am creating is intended for individuals who do not have automatic access to HTML5. Some of these people are still using IE8. Here's an example of a form: <form action="ee.cfc?method=xlsupload" en ...

Convert the button element to an image

Can someone please explain how to dynamically change a button element into an image using javascript when it is clicked? For instance, changing a "Submit" button into an image of a check mark. ...

Is it possible to trim a video using HTML code?

I am trying to find a way to crop a video using HTML 5. <video id="glass" width="640" height="360" autoplay> <source src="invisible-glass-fill.mp4" type="video/mp4"> </video> Currently, the video has a resolution of 640x360. However ...

Prevent the underlining of the :before content of a link from being affected by a rule applied to the link

I have a hyperlink that becomes underlined when hovered over. I want to add a > symbol before the link, but I don't want it to be underlined when the link is hovered. Here's the code I'm using: .box.blueb a { color: #0098aa; } .box.blueb ...

php code to paginate mysql results

Imagine I have 50 rows in my database. How can I retrieve MySQL results in pages, displaying 5 results on each page and showcasing the pages as follows: [1], 2, 3, 4...10? For example, if it's on page 5, show 3, 4, [5], 6, 7...10 without refreshing al ...

Optimizing the particle rendering speed for HTML5 <canvas> elements

Currently conducting an experiment to enhance the maximum particle count before frame-rates begin to decrease in HTML5 Canvas. Utilizing requestAnimationFrame and employing drawImage from a canvas as it appears to be the most efficient method for image re ...