Creating a Navigation Bar with a Dropdown Menu in HTML

Apologies if this question has been asked before, but I've searched everywhere and can't find an answer. All my searches just bring up dropdown menus with

<nav></nav>

Consider this table (serving as a navigation bar):

<table class="nav">
<tr>
<td><a href="index.html">Home</a></td>
<td><a href="/Videos">All</a></td>
<td><a href="spec.html">Specification</a></td>
<td><a href="chem.html">Chemistry</a></td>
<td><a href="bio.html">Biology</a></td>
<td><a href="phy.html">Physics</a></td>
<td><a href="feedback.html">Feedback</a></td>
<td><a href="donate.html">Donate</a></td>
</tr>
</table>

CSS:

table.nav {
    border: 2px solid white;
    background-color: black;
}

How could I implement a hover effect that displays a dropdown menu with links on this table? I'm opting for this approach due to design preferences and ease of use.

Answer №1

If you are dealing with dynamically created content from a CMS or PHP code, it is highly recommended to use a list for better organization. A simple CSS-only solution involves creating nested lists that display on hover - a technique I have successfully implemented in basic Wordpress menus before.

Here is an example of the styling you could apply:

body {
    margin: 0px;
    padding: 0px;
}

nav ul {
    margin: 0px;
    padding: 0px;
    float: left;
    list-style-type: none;
}

nav ul li {
    padding: 20px;
    float: left;
}

nav ul li ul {
    display: none;
}

nav ul li ul li {
    width: 100%;
    padding: 0px;
    display: block;
}

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

The part where nav ul li:hover instructs the browser to switch the display property of nav ul li ul from none to block, causing the menu items to appear.

You can use the following HTML markup, and further customize it for a visually appealing design:

<nav>
    <!-- Start the listing of main items -->
    <ul>
        <li>Menu item 1</li>
        <li>Menu item 2
            <!-- Sub items include -->
            <ul>
                <li>Menu item 2.1</li>
                <li>Menu item 2.2</li>
            </ul>
        </li>
        <li>Menu item 3</li>
    </ul>
</nav>

I have personally tested this code and it should work effectively. If you encounter any issues, consider incorporating some Javascript as suggested by cr0ss.

Answer №2

Avoid using javascript and tables for this task. Consider the following example instead:

.nav {
    border: 2px solid white;
    background-color: black;
}

.nav td { position: relative; }

.nav table,
.nav ul { 
    display: none;
    position: absolute;
    list-style: none;
    margin: 0;
    padding: 0;
    width: 300px;
    left: 0;
}

.nav td:hover table { display: table; /* or display: block; */ }
.nav td:hover ul { display: block; }
<table class="nav">
    <tr>
        <td>
            <a href="index.html">Home</a>
        </td>
        <td>
            <a href="/Videos">All</a>
            <ul>
                <li><a href="#">Item in list</a></li>
                <li><a href="#">Item in list</a></li>
            </ul>
        </td>
        <td><a href="spec.html">Specification</a></td>
        <td>
            <a href="chem.html">Chemistry</a>
            <table>
                <tr>
                    <td><a href="#">Item in table</a></td>
                </tr>
                <tr>
                    <td><a href="#">Item in table</a></td>
                </tr>
            </table>
        </td>
        <td><a href="bio.html">Biology</a></td>
        <td><a href="phy.html">Physics</a></td>
        <td><a href="feedback.html">Feedback</a></td>
        <td><a href="donate.html">Donate</a></td>
</tr>
</table>

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

Animating path "d" with ReactJS and SVG upon clicking in FireFox

Visit this CodePen for more: https://codepen.io/sadpandas/pen/xxbpKvz const [currentScreen, setCurrentScreen] = React.useState(0); return ( <React.Fragment> <div> <svg className="theSvg" width="156" height="6 ...

Changing the description doesn't affect the fixed height of the box - here's how to do it with CSS

Here are the references I used: GetBootstrap Jumbotron CoreUI Base Jumbotron This is how my script looks like: <template> <div class="wrapper"> <div class="animated fadeIn"> <b-card> <b-row v-for="row in ...

Using jQuery to select and animate several elements simultaneously without repeating the animation

Issue Summary (Fiddle): I'm trying to make all elements fade out simultaneously when clicked, but the current code causes the target to trigger three times resulting in three alert() calls. I want them to fade together without using a wrapper element ...

Tips for incorporating a background color into a specific section

Code: .headline { font: 150 50px/0.8 'Pacifico', Helvetica, sans-serif; color: #2b2b2b; text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.1), 7px 7px 0px rgba(0, 0, 0, 0.05); text-align: center; } <link href='http://fonts.googleapis.com ...

When incorporating `Ionic/core` into my project, it unexpectedly alters my scss styling

Exploring StencilJs: In my project using StencilJs, I aim to create a wheel time picker with 3 columns for hours in 12-hour format and minutes which can vary based on the provided array such as [0,15,30,45] or [00,30]. Additionally, there will be options ...

The HTML elements may have identical heights, but visually, one appears larger than the other

The size of the "login to enter chat" button seems to be larger than the messageBox div, despite adjusting padding and margins. What could be causing this issue? Any suggestions on how to fix this design glitch? #chatbox { width: 500px; height: ...

How to place a div within another div without using absolute positioning

I've been searching for a solution to this issue, but nothing seems to be working. I want the inner div to be positioned at the bottom of the outer div, with a 5px margin separating them. However, using absolute positioning disrupts the width and cent ...

Disable location prompt feature when using AngularJS with Maps API

For my web application, I developed a feature that includes a map with custom markers using Angular loops: <map data-ng-model="mymap" zoom="4" center="[38.50, -95.00]" style="heigth:375px"> <div ng-repeat="item in list"> <marker pos ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Manage the border around the image by incorporating a timer countdown - starting from a complete circle, transitioning to a partial arc, and finally disappearing completely

My expertise lies in html, css, and angularjs for front-end development. I have an image that is initially surrounded by a thick border forming a full circle. As a countdown of one minute begins, I want the border to gradually disappear as time progresses. ...

Is it possible to integrate ngx Pagination with Bootstrap?

Is it possible to integrate Bootstrap's pagination with ngx Pagination? I'm interested in using the functionality of ngx Pagination but styling it with the CSS from Bootstrap. ...

Middle-Click JavaScript Action

On the website I'm currently working on, there is a button that uses a sprite sheet animation and therefore needs to be set as a background image. I want the button to have a slight delay when clicked, so the animation can play before redirecting to a ...

Translucent" outline for an "opaque" object

Consider the following HTML snippet: <div id="my_div"> </div> Now, let's take a look at the CSS code: #my_div { width: 100px; height: 100px; background-color: #0f0; op ...

Advancing in the Mozilla fashion

Having an issue with my progress bar design... When viewed in Chrome, the value of the progress bar appears in red due to CSS styling I applied... But when opened in Mozilla, it shows up as a dull grey color. progress[value] { width: 250px; height ...

Unusual discovery within JQuery and IFrames

Let's set the stage. function scrollLog(line) { // Let's assume it's for Firefox // alert("weird"); frames['log'].find(line); }; There's a function that I trigger when the document is ready. The current code doesn&a ...

Choose an option with JavaScript once the request is successful

Choose the day, month, and year using JSON data success: function(jsondata){ var data=JSON.parse(jsondata); var list_html="<div id='editrelation'><label id='dateLabel' style='display:none&apo ...

What is the best way to include CSS code in the output display?

I'm looking to customize the appearance of my table rows and cells. Specifically, I want the cell file name, size, etc., to be colored blue while each row should have a different color on hover. I've attempted to achieve this using CSS and variou ...

Using jQuery to load content into a jQuery UI dialog box

I am looking to implement a pop-up that displays content from another asp page. To achieve this, I am using jquery.load to load the page into a div and jquery-ui.dialog. This is my code: <div id="dialog"></div> Inside the document ready fun ...

Creating a visually appealing background image using WordPress and PHP

Currently struggling with adjusting the width of an image that's set as a background in Wordpress. The text is perfectly centered, but the background image remains full size and I want it to match the container size... You can view the image at the t ...

Display scroll bars over the position:absolute header

My container has content that exceeds its size in both directions. To see the issue, try scrolling horizontally and vertically on the table available here: The vertical scrollbar is visible as desired, except that it gets hidden behind the table header un ...