Is it possible to create a CSS dropdown menu that is aligned vertically in a

I've successfully created a vertical navigation menu using CSS that includes two sub-menus. However, I am struggling to align them in a single column for proper functionality. Is there a way to achieve this?

HTML


    <ul>
        <li><a href="#">works</a>
            <ul>
                <li><a href="#">Category 1</a></li>
                <li><a href="#">Category 2</a></li>
                <li><a href="#">Category 3</a></li>
                <li><a href="#">Category 4</a></li>
                <li><a href="#">Category 5</a></li>
            </ul>
        </li>
        <li><a href="#">photos</a>
            <ul>
                <li><a href="#">something</a></li>
                <li><a href="#">something</a></li>
            </ul>
        </li>
        <li><a href="#">friends</a></li>
        <li><a href="#">contact</a></li>
    </ul>

CSS


#menu {
    font-size: 14px;
    font-family: "Courier New", Courier, monospace;
}
#menu ul {
    margin: 0px;
    list-style-type: none;
}
#menu ul li {
    position: relative;
}
#menu ul li a {
    line-height: normal;
    color: #000;
    text-decoration: none;
}
#menu ul li ul {
    display: none;
    position: absolute;
    top: 0px;
    left: 180px;
    float: left;
    z-index: 99999;
    width: 180px;
}
#menu ul li ul li {
    min-width: 180px;
}
#menu ul li ul ul {
    float: left;
    top: 0px;
}
#menu ul li:hover > ul { 
    display:block;
}

Answer №1

Your HTML structure needs some cleaning up. A cleaner structure could look like this:

<div id="menu">
  <ul>
    <li>
      <a href="#">works</a>
      <li>
        <a href="#">works subcategory</a>
        <ul>
        <li><a href="#">something</a></li>
        <li><a href="#">something</a></li>
        <li><a href="#">something</a></li>
        </ul>
      </li>
      <li><a href="#">Category 1</a></li>
      <li><a href="#">Category 2</a></li>
      <li><a href="#">Category 3</a></li>
      <li><a href="#">Category 4</a></li>
      <li><a href="#">Category 5</a></li>
    </li>
    <li><a href="#">something</a></li>
    <li><a href="#">something</a></li>
    <li>
      <a href="#">photos</a>
      <ul>
        <li><a href="#">something</a></li>
        <li><a href="#">something</a></li>
      </ul>
    </li>
    <li><a href="#">friends</a></li>
    <li><a href="#">contact</a></li>
  </ul>
</div>

There were mistakes in closing tags and I recommend using CSS resets for dropdown menus to avoid issues with user-agent predefined styles (try Normalize.css).

In CSS: You don't need to float the 2nd-level ul blocks and setting list items position property to relative and using top and left properties for children ul is not ideal.

I made some style changes to your menu and it looks much better now. Check it out here: http://codepen.io/anon/pen/sdomr

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

Utilizing Bootstrap 4: Opting for <select> over tabs (<li>)

Is there a way to customize the appearance of Bootstrap tabs by replacing them with a dropdown select menu instead of the standard tab layout created with <ul> and <li> elements? I am working with a relatively small area (such as col-3) where ...

Design a floating text box similar to Gmail's style by incorporating Google's Material Design Lite

I have been utilizing the Material design lite library in an attempt to replicate the Floating text box feature seen in Gmail by Google. Despite not finding any official documentation on this particular component, I decided to experiment with the following ...

Nothing is being returned when using JQuery AJAX POST

Having trouble identifying the issue, as all that seems to happen is the URL changes to "http://localhost/?search=test" when entering "test", for example. index.php <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

How can I eliminate the white bar elements from my dropdown menu? Also, seeking guidance on replacing an empty <div> tag in a boolean query

Can anyone help me understand the strange white border around my dropdown menu and guide me on how to remove it? I am currently using DropdownButton from react bootstrap. I have attempted to adjust the CSS with no success. Here is the code I tried: .Navig ...

"Customize your map pins on Google Maps by updating the marker location through a

I am trying to update the position of a map marker based on a dropdown selection. Upon changing the dropdown option, I retrieve the latitude and longitude values and want to set these coordinates for my current marker. Here is the code snippet: $("#locati ...

A unique technique for creating a stunning visual effect with images using

Can anyone help me with this issue: Check out this animated GIF The images in my project are overlapping when scrolling! How can I achieve a similar effect for my images? Is there a tutorial or plugin available for this? Here is the current code sn ...

Iterate through each line of code and modify the integer in order to target elements by their class

Originally shared on the MrExcel forum www.mrexcel.com/board/threads/change-integer-in-code-line-for-htmldoc-getelementsbyclassname.1146814/ The initial line of code I used was Set DogRows1 = HTMLDoc.getElementsByClassName("rpb-greyhound rpb-greyhou ...

Angular not firing slide.bs.carousel or slid.bs.carousel event for Bootstrap carousel

I've searched high and low with no success. I'm attempting to detect when the carousel transitions to a new slide, whether it's automatically or by user click. Despite my numerous attempts, I have been unable to make this event trigger. I ha ...

Unpredictable String of Caret Symbols Within an HTML Document

I'm currently working on a web application using Django, bootstrap, HTML, CSS, and JS. I am populating a table with API results through template building in Django. However, there seems to be a mysterious line of carets appearing between an H1 tag for ...

Categorize Jekyll tags into two distinct groups

I am currently using Jekyll to construct a GitHub Pages website. The grid and column layout are powered by Bootstrap. When it comes to listing all the posts linked to a specific tag, I begin by sorting the tags and then listing all the posts associated wit ...

Add a stylish border to your image within a container of fixed width

I am facing a challenge with a fixed width DIV element. Inside this element, there is a second DIV containing only an IMG element. My goal is to add a border to the image without exceeding the boundaries of either of the enclosing DIV tags. Solution #1: ...

Encountering a Vuetify CSS issue upon building for production

We recently acquired a Vue web app from a third party and have been working on enhancing it. One of the modifications we made was to integrate Vuetify in order to utilize its elements. While everything functioned smoothly during development, we encountered ...

Programming code that will assign an object as the value for an option in a select box

On my website, I have created a select box with different values displayed. <select onChange="return filler(this.value);"> <option value="{'name':'rajiv',age:'40'}">a</option> <option value="{'name& ...

How to insert a value using jQuery Minicolors in an HTML document?

While I found similar topics on Stackoverflow, none exactly match my issue. I am currently incorporating jquery Minicolors into my project: https://github.com/claviska/jquery-miniColors/ Everything is functioning properly, but I simply need to accomplish ...

Are there any specific requirements or preferences for using .0 in em?

I'm struggling to find a concrete solution. In a nutshell, would it be more appropriate to utilize font-size: 2.0em; or font-size: 2em;? ...

My custom Wordpress template experiences a hiccup with the jQuery slideshow, as it unexpectedly

Looking for help with a slideshow issue. Initially, it loads and transitions smoothly into one picture, but then stops working. I decided to use Chrome to troubleshoot the problem, and it informed me that it doesn't recognize the slideSwitch function ...

Explore a unique navigation bar using CSS hover effects

I am trying to create a navigation bar that displays a big block when hovered over, but unfortunately, it's not working as expected. Below is the HTML code I have written: <div id="outerContainer"> <div id="header"> ...

Position the text at the upper center of the div and the button at the bottom

I've got a div container structured like so: <div class="text-offer_dos"> <div class="centrar"> <h3>Lorem ipsum dolor!</h3> <h5></h5> <p class="text"><b>Lorem ipsum dolor ...

Is there a way to incorporate the ::after or ::before pseudo-elements into an image tag?

While working on my project, I attempted to incorporate the ::after tag (I had used ::before as well) for an image within an img tag. However, I am facing difficulties as it doesn't seem to be working. Any advice or guidance would be greatly appreciat ...

Placing a new element with a specific class name into another element identified by its unique ID

I'm trying to dynamically add a new li element with a specific classname from an array to a static element with an id. However, I encountered an error stating "appenChild is not a function." It's important to note that we are working inside a Jav ...