Styling a CSS dropdown menu: tips and tricks

I came across this example and tried to implement it, but I'm facing some difficulties. Any assistance would be greatly appreciated.

UPDATE: I managed to position my submenu better with the help of yourkishore and figured out how to deal with nested menus. However, my submenu is still displaying right at the bottom of my text?

** Updated jsFiddle **

HTML:

<ul id="menubar">
    <div id="title">title</div>
    <li>File
        <ul>
        <li>Photoshop CC</li>
        <li>Illustrator CC Adobe</li>
        <li>My Web Design
            <ul>
            <li>HTML</li>
            <li>CSS</li>
            </ul>
        </li>
        </ul>
    </li>
    <li>Config</li>
</ul>
<div id=other></div>

CSS

html, body {height:100%;width:100%;margin:0;}
#menubar {
    background: #eeeeee;
    height:25%;
    list-style-type: none;
    width:100%;
    display: inline-table;
    margin:0;
    padding:0;
    z-index:99;
}
#menubar #title {
    display:inline-block;
    padding:0 5%;
    font-size:140%;
    font-weight:bold;
    float:left;
    height:100%;
    display: inline-flex;
    align-items: center;
}
#menubar li {
    height:100%;
    position:relative;
    float:left;
    padding:0 1%;
    list-style-type: none;
    white-space:nowrap;
    display: inline-flex;
    align-items: center;
}
#menubar li:hover {
    background-color:#7faddb;
}
#menubar li > ul {
    background-color:white;
    z-index:99;
    display: none;
    position:absolute;
    list-style-type: none;
    margin:0;
    padding:0;
}
#menubar li:hover > ul {
    display: block;
}
#other {
    background-color:green;
    position:absolute;
    top:25%;
    bottom:0;
    left:0;
    right:0;
}

Answer №1

The reason why the menu items are showing inside is because they are contained within. To have them appear outside when you hover over, you must use the position property.

For #menubar li, you should include position: relative

And then for #menubar li ul, you should include position: absolute.

Menus rely heavily on this particular property. Without it, nested lists in menus would not function properly. Take the time to familiarize yourself with CSS positioning techniques. Good luck!

Answer №2

Everything is running smoothly on your fiddle, simply incorporate this HTML code:

<div id="title">title</div>
<nav>
<ul>
    <li>File
        <ul>
        <li>Photoshop CC</li>
        <li>Illustrator CC Adobe</li>
        <li>My Web Design
            <ul>
            <li>HTML</li>
            <li>CSS</li>
            </ul>
        </li>
        </ul>
    </li>
    <li>Config</li>
</ul>
</nav>
<div id=other></div>

Furthermore, apply the following CSS to enhance your page:

 body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, textarea, blockquote {
    margin: 0; padding: 0; border: 0;
}

body {

    font-family: Helvetica, sans-serif; font-size: 18px; line-height: 24px;
}

nav {

    text-align: center;
}

nav ul ul {
    display: none;
}

    nav ul li:hover > ul {
        display: block;
    }


nav ul {
    padding: 0 20px;

    list-style: none;
    position: relative;
    display: inline-table;
}
    nav ul:after {
        content: ""; clear: both; display: block;
    }

    nav ul li {
        float: left;
    padding:12px;
    }
        nav ul li:hover {
            background: #505050;

        }
            nav ul li:hover a {
                color: #171717;
            }

        nav ul li a {
            display: block; padding: 25px 40px;
            color: #994411; text-decoration: none;
        }


    nav ul ul {
        background: #888888; border-radius: 0px; padding: 0;
        position: absolute; top: 100%;
    }
        nav ul ul li {
            float: none; 
            border-top: 1px solid #111;
            border-bottom: 1px solid #575f6a; position: relative;
        }
            nav ul ul li a {
                padding: 1px 40px;
                color: #666;
            }   
                nav ul ul li a:hover {
                    background: #333;
                }

    nav ul ul ul {
        position: absolute; left: 100%; top:0;
    }
        #other {
    background-color:green;
    position:absolute;
    top:25%;
    bottom:0;
    left:0;
    right:0;
    z-index:-1;
}

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

Exploring the ways to retrieve the returned Dynamic scope in AngularJS

There's a specific need I have - I require a single AngularJS function that can dynamically return the scope variable based on an input parameter. When it comes to controller functions, I've come across examples of how to achieve this dynamic sco ...

Tips for showing the data from a JSON object with numerous arrays in Angular HTML

I managed to create a JSON object by parsing CSV file fields using the papa Parse library. Now, my goal is to showcase this data on an HTML page. However, I'm struggling with how to extract the arrays from the JSON object and transfer them into a vari ...

JSP Error: the specified resource cannot be found

I am a newcomer to this website, and as a programmer, I consider myself to be at a beginner to intermediate level. I am new to Java and have to work with JSP for a University course. I am encountering an error and would like help in identifying what is wro ...

Emphasizing specific terms by changing them (but only within p tags)

After spending about 8 hours searching for a solution yesterday on various platforms, I almost gave up. But then I realized I should turn to this community for some much-needed help. So here is my first question - a big thank you to everyone who has helped ...

Include image hover text without using HTML

I am looking to add a hover effect to some images using CSS and JS since I cannot directly edit the HTML file. The goal is to have centered text pop out when hovering over certain images. I know the div class of the image but unfortunately, I cannot add te ...

When you print, transfer HTML tags to a separate page

I need help with writing a report that pulls data from a dynamic database. I added code to force a page break after every second div tag, but since the content is constantly changing, some div tags end up spanning across pages when printed. Is there a way ...

What steps should I take to send an html form for server-side validation once I have completed successful validation on the client side using jQuery

Currently, I'm immersed in a web application project where the registration page design has been successfully completed. jQuery has been utilized for input validation and everything seems to be functioning as intended. However, upon clicking the Regis ...

The <video> HTML tag is experiencing issues in iOS 12 and higher when used in an Ionic application

I have integrated a video into the application using an html tag for playback. The video functions correctly on Android devices and iOS versions prior to 12. However, it fails to play on iOS 12. <video class="anim-img" height="360px" width="360px" ...

Tips for navigating between slides on a CSS carousel?

I am currently in the process of creating a CSS-based carousel .carousel { -webkit-scroll-snap-type: x mandatory; -ms-scroll-snap-type: x mandatory; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; overflow-x: scro ...

Transforming card dimensions with Bootstrap

I am currently working on my web portfolio and I am looking to create a card format that includes an image thumbnail with text below it. However, I am facing an issue where I cannot control the size of the initial image, resulting in misaligned cards. htt ...

Change the color of the icon from white to blue in png format using html/css

Changing a White Icon.png to a Blue Icon.png Using HTML/CSS The original icon.png is white in color. How can we change it to a blue color version using only HTML and CSS? HTML: <img src="icon.png" class="icon1"> CSS: .icon1 { background: url(ic ...

Step-by-step guide on generating multiple division elements according to the values specified by the user

Currently, I am focused on a project using asp.net. The primary objective is to create an image div whenever a button is clicked. This involves fetching the image from a database and displaying it on the screen, either in another div or table. I would ap ...

The function "classList.add" does not successfully add a class to both a div and form elements

I've encountered an issue where I am unable to add a class to div/form elements using the classList.add method. Interestingly, this method works perfectly fine for other elements like input or button. However, when it comes to the div and form element ...

Steps to create a floating navbar using tailwindcss

Here is the code snippet for the site header component: export function SiteHeader() { return ( <header className="fixed top-0 z-50 w-full border-b border-border/40 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60&qu ...

Discovering the element's class within an each loop

I am currently working on a dynamic menu system that changes the style of the active page item in the menu. My approach involves utilizing separate body classes for each page and then iterating through the li elements in the menu to find a match with the b ...

Navigating to the Top of the Page

I am facing an issue with my webpage that is similar to the code provided below: var btn = document.getElementById('test') test.addEventListener("click", function(){ window.scroll(0,0) }) .wrap { overflow-y: scroll; position: absolute; ...

Tips for displaying just the image name without the entire image file path

When I try to upload an image, the path displayed is "C:\fakepath\promotion1.JPG". This fake path is causing the image not to upload to my project media folder. How can I solve this issue? Instead of the complete path, I only want to capture the ...

Incorporating Twitter Bootstrap into your Zend Framework 2 Navigation Menu

I have successfully created a menu from my database and the code is functioning perfectly. Now, I'm looking to enhance the menu's style by incorporating Twitter Bootstrap, but I'm encountering some difficulties in doing so. Is there anyone k ...

Automated Star/Stop feature with a convenient Save button (the Start/Stop format will display as mm/dd/yyyy hh:mm:ss)

I'm having an issue with the date format in my code. Currently, it displays as Mon Mar 13 2017 19:02:50 GMT-0400 (Eastern Daylight Time), but I need it to be in the format mm/dd/yyyy 00:00:00 am/pm. My challenge is to capture the current date and tim ...

What is the process for defining the default landing page route in Angular routing?

My application currently has only one route, and I want it to start with the URL http://localhost:4200/specialtyQ. However, my application is not loading properly. The code snippet below is what I am using to try to achieve this. How can I correct the URL ...