What is the best way to design a dropdown menu with multiple columns?

I'm attempting to design a dropdown menu using CSS with 21 sub-items. I want to display them in 3 columns of 7, but I can't seem to figure it out.

I've set up a simple example on jsFiddle, how can I adjust the CSS so that sub items 6-10 appear to the right of sub items 1-5 instead of below?

The layout should resemble the image I created in MS Paint.

EDIT :

jsFiddle

<nav>
  <ul>
    <li>
      <a href="#">Top Level Group</a>
      <div>
      <ul>
        <li><a href="#">Sub One</a></li>
        <li><a href="#">Sub Two</a></li>
        <li><a href="#">Sub Three</a></li>
        <li><a href="#">Sub Four</a></li>
        <li><a href="#">Sub Five</a></li>
      </ul>
      <ul>
        <li><a href="#">Sub Six</a></li>
        <li><a href="#">Sub Seven</a></li>
        <li><a href="#">Sub Eight</a></li>
        <li><a href="#">Sub Nine</a></li>
        <li><a href="#">Sub Ten</a></li>
      </ul>
      </div>
    </li>
  </ul>
</nav>

Answer №1

To stack elements, you can apply the CSS property float: left.

For a live example, check out this fiddle: http://jsfiddle.net/07spd07b/2/

#top-level-group{
    width: 200px;
}
#top-level-group ul{
    float: left;
}

In the provided sample, I moved the original position from -9999px to the left so that it is visible when opening the jsfiddle. Adjust the width based on your content needs, and feel free to add more columns by increasing the width.

UPDATE:

Check out the updated sample with three columns and a clearfix included:

http://jsfiddle.net/07spd07b/10/

I've introduced a new class called clearfix:

.clearfix:before,
.clearfix:after {
    content: '\0020';
    display: block;
    overflow: hidden;
    visibility: hidden;
    width: 0;
    height: 0; }
.clearfix:after {
    clear: both; }
.clearfix {
     zoom: 1; }

This class helps clear floating elements and sets the width: 250px; to accommodate the three columns. Hopefully, this explanation proves useful!

Answer №2

To achieve this effect easily, you can utilize the display:inline-block; property:

nav ul li ul{
display:inline-block;
margin: 0;
padding: 0;
}

Once that is set, you simply need to specify a width for the div container:

nav ul li div {
position: absolute;
left: -9999px;
width:300px;
}

Check out this JSFiddle Demo for reference.
To incorporate more columns, just create additional <ul> elements and adjust the width accordingly.

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

Align navigation items in the center of the navigation container

I've been grappling with aligning the content of a nav-item inside a nav container in my project. I'm unable to apply a margin on the ul>li items as it doesn't adjust properly on different resolutions. I've tried using percentages, b ...

By combining TCPDF with basic HTML, you can easily generate a detailed table of contents

Is there a way to generate a table of contents when printing HTML to PDF using TCPDF? Here is an example code snippet: $html = '<h1>Hello</h1> How are you? <h2>Answer</h2> I am well, thanks'; // ... etc. Long HTML documen ...

What is the best way to prevent the mat-options from overlapping with a fixed mat-toolbar header when scrolling?

Looking for help with Angular as a beginner. I want to create a layout with a fixed toolbar/header at the top of all components, where everything else will be rendered underneath it. However, when using mat-autocomplete and scrolling to the bottom without ...

Supported video formats in various web browsers

Are there detailed records of video codec compatibility across various desktop and mobile web browsers? ...

The issue with the Hellip symbol not functioning correctly persists within the URL location

I'm having trouble with the address provided in my ePub. It's telling me that ... (ellipsis) is not valid: http://www.connectingforhealth.nhs.uk/…/infogov/…/infogovleaflet.pdf Is there a way to properly include this URL? ...

Selecting CSS paging in JQuery Jtable

I am currently working with Jquery JTable and encountering an issue with pagination. The numbers displayed on the selects for "Go to Page" and "Row Count" are not appearing correctly. Any suggestions on how I can resolve this? Thank you, Nk ...

Slide toggle in jQuery reveals itself briefly before vanishing

Check out the codepen here: https://codepen.io/anon/pen/gRJEwx Essentially, with jQuery toggle 'slide', the div slides in and right back out. It seems to only happen every second or third time the button is pressed. The first try never triggers ...

Bootstrap doesn't display in the dropdown menu

Struggling to get a dropdown menu to display on my page, my HTML code is: <div class="dropdown d-inline"> <div class="widget-header mr-3"> <button href="#" class="icon icon-sm rounded-circle border" data-toggle="dropdown"><i cl ...

The Conundrum of CSS versus Razor Views

Currently, I am developing a basic website using ASP.NET/MVC with Razor (.cshtml). As I am relatively new to CSS, I am interested in customizing the appearance and style of my site. After inspecting the source of the homepage, I find myself struggling to t ...

Conceal the URL and any parameters upon successful completion of AJAX request

In my ajax js code, the solonreport.jsp file returns a URL link that connects to a local report server and generates an Excel report. However, when using this link with the window.open(json.url) function, the link and parameters are visible to the user. ...

Transitioning the Background Image is a common design technique

After spending hours trying to figure out how to make my background "jumbotron" change images smoothly with a transition, I am still stuck. I have tried both internal scripts and JavaScript, but nothing seems to work. Is there any way to achieve this witho ...

Does WebSVG struggle with producing sharp, defined lines in its inline format?

Upon page load, my SVG background starts as separated and then smoothly transitions into forming the background. However, I noticed that a large triangle with poor quality and choppiness appears at the beginning. What could be causing this issue? For an e ...

Div sliding down from the top of the page

I have successfully created a pop up that appears when the user reaches the bottom of the page. Now, I am looking to implement a similar concept but have the pop up appear from the TOP of the page, at a specific location on the page instead of just top or ...

Move your cursor over the image to see a different image appear

I am facing an issue - I'm attempting to make image hover affect another hover <img id="1" src="#" /> <img id="2" src="#" /> #1 {width:20%; opacity:0.5 } #2 {width:80%; position:absolute; display:none; } I have experi ...

struggling to access the value of a hidden field by using the parent class name

My coding experience so far looks like this- <tr class="chosen"> <td id="uniqueID">ABCDE5678</td> <input type="hidden" value="000005678" id="taxCode"> <td id="fullName">Z, Y</td> </tr> In this scenario, I need to ...

Having trouble compiling your Vue project and running into the "No mixed spaces and tabs" error?

Below are the details with an error: Error Details: Failed to compile. ./src/components/Header.vue Module Error (from ./node_modules/eslint-loader/index.js): H:\project\VueProjects\stock-trader\src\components\Header.vue 27: ...

Missing 'id' property in ngFor loop for object type

I'm currently learning Angular and I've been following a code tutorial. However, despite matching the instructor's code, it doesn't seem to be working for me. When I try to compile the following code, I receive an error message stating ...

Guide to positioning a div at the bottom of a Drawer while maintaining the same width as the Drawer in React Material UI

Creating a Drawer on the right-hand side using <Drawer/>, I am trying to make a <div> stick to the bottom of the <Drawer />. The width of this <div> should match the width of the <Drawer />. Desired Outcome: https://i.sstati ...

Arranging sibling buttons to wrap seamlessly like text

Is there a way to turn every syllable of a specific phrase into clickable buttons while maintaining regular text wrapping behavior? For reference, in the first image each syllable is plain text, in the second they're wrapped in <span>, and in t ...

The page only appears properly after clearing the cache

My floating list menu is behaving oddly in Firefox. It only displays correctly after clearing the cache, but as soon as I refresh the page, the menu breaks again. The links inside the list elements appear wider than the text, even though I haven't se ...