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

The content within the div appears to be in a perpetual state

I'm working on creating a chat application with a single interface. I've encountered an issue where the content in the middle div "messages" keeps bouncing, including the scroll bar. How can I resolve this problem? Additionally, I'm unable ...

What is the method for retrieving a PHP JSON variable value and displaying it in HTML?

Using the graph API, I managed to retrieve my Facebook friend list and received a JSON array as a response. The value of that array is stored in a variable like this: $json_output=($result['summary']['total_count']); echo "$json ...

Setting MenuItem to the correct width in asp.net simplified

I have a 1000px wide container, within it there is a menu control with 5 items (links). Each item has a width of 200px in the CSS file to make use of the entire length of the line. .level1 { border-width: 0px; margin: 0px; padding: 0px; background ...

How can I send a current variable through PHP?

I have a pressing deadline and I need some guidance before moving forward. Can someone please advise if what I'm attempting is feasible or if there's a quicker solution? I encountered some problems with an accordion menu, so as a temporary fix, ...

Preventing Height Stretch in CSS Flex-Wrap: A Guide

Is there a way to eliminate the large gaps between box3, box1 and box4, box6? How can we ensure each box adjusts its height dynamically? .wrap { display: flex; align-items: baseline; align-content:flex-start; justify-content: space-between; fl ...

Animating the Click Event to Change Grid Layout in React

Can a grid layout change be animated on click in React? For instance, consider the following component: import { Box, Button, styled, useMediaQuery } from "@mui/material"; import Row1 from "./Row1"; import React from "react"; ...

jQuery not being applied to dynamically added dropdown element

I am currently utilizing bootstrap and jquery within my react project. I have a button that, when clicked, should transform into a dropdown field. The dropdown functions properly when placed statically, but the functionality is lost once it is dynamically ...

Discover the power of AngularJS's ng-route feature for creating a dynamic and seamless one-page HTML template experience

I'm attempting to create a dynamic header using ng-repeat. Here's the initial code: <ul class="right"> <li><a href="#carousel">Home</a></li> <li><a href="#about-us">About Us</a></li> &l ...

What causes the discrepancy in smoothness between the JavaScript animation when offline versus its choppiness when online, particularly on AWS

Recently I delved into game development using HTML5/CSS/JS and embarked on a small project. Check out the game here at this AWS storage link: If you open the game and press SPACE, you'll notice that the ball starts moving with occasional brief pauses ...

Soft keyboard on mobile fails to update when arrows are used in Ajax-populated dropdown menus

I am working on a web form that includes two select fields: Country and City: <select id="country" onchange="getCity(this);"> <option value="">-- Please select your country --</option> <option value="1">Austria& ...

Customizing the cursor for disabled custom controls in Bootstrap 4

I'm having trouble changing the cursor for disabled custom-control elements like checkboxes and radiobuttons. The following style isn't working as expected: .custom-control-input:disabled ~ .custom-control-label::before { cursor: not-allowe ...

I am looking to incorporate two YouTube videos into an HTML webpage, each in a different language. I would like to provide the option for users to select the language, even though the content

I am looking to target a global audience with my website, so the content is in English. However, I want to offer users the option to watch videos in their preferred language. I have recorded content in 4 different languages that might enhance engagement on ...

Using jQuery to continuously make an ajax call until it completes and then return the callback result

Currently, I have two functions that handle ajax requests. The first function sends a request for a batch and then continues to check on the status of the batch using its ID until it is marked as "done." Throughout this process, it stores all the results i ...

[Vue alert]: Issue in created function: "TypeError: Unable to assign value to undefined property"

I'm currently in the process of developing a VueJS application where I have implemented a child component called Child.vue that receives data from its parent. Child.vue export default{ props:['info'], data: function(){ ...

Implementing jQuery for dynamic theme changes on a single webpage

Driving me absolutely insane! I'm at my wits' end trying to follow this post that just won't cooperate: Here's the situation: I have a base theme, let's say it's the Smoothness theme from jQuery UI gallery. Then there's ...

Convert several XML nodes to display in an HTML format

I am currently facing an issue where I am trying to display all the nodes of a specific tag in XML. Although it does filter the data correctly, only one node is showing up. Is there anyone who can assist me with this problem? Below is the XML content: < ...

What is the best way to retrieve the URL of a website using scrapy?

Currently, I am faced with the challenge of scraping the Amazon website using Scrapy. While I have successfully extracted items like product titles and prices, I am struggling to figure out how to extract the URL of a product (as shown in the image at the ...

Passing data from Bootstrap 4 to a modal on a different server

I am currently using NodeJs and Express for my project. I have a requirement to pass data from a MongoDB database to a remote modal in Bootstrap 4. I understand that the "remote" option was removed in version 4 of Bootstrap, but I need to display different ...

Error Alert: JQuery Pop Up Issue

Struggling with getting my JQuery Pop-Up Box to work properly. Any guidance for a newbie like me on how to fix it would be greatly appreciated. Here's the code I've been working on: <!-- POP UP HTML --> <div class="infocontainer"> ...

GAE does not have access to background images

Having trouble loading background pictures in my GAE front-end app. "Failed to load resource: the server responded with a status of 404 (Not Found)". My app is a simple website, no GAE backend. Using Eclipse with Google AppEngine Plugin. Background pict ...