What is the best way to ensure that my drop-down menu items match the size of the parent nav bar?

I'm having trouble creating a dropdown menu using only CSS. Specifically, I am struggling to ensure that the dropdown menu is the same size (width and height) as its parent element.

Here is a link to a working example: HERE

This is the snippet of HTML containing the <nav> section:

<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu With Menus</a>
            <ul>
                <li><a href="#">opt 1</a></li>
                <li><a href="#">opt 2</a></li>
                <li><a href="#">opt 3</a></li>
            </ul>
        </li>
        <li><a href="#">Whatnot</a></li>                
    </ul>
</nav>

Here is the corresponding CSS:

nav ul {
    position: relative;
    display: inline-table;
    list-style: none;
}

nav ul li {
    float:left;
    list-style-type: none;
}

nav ul li a {
    background-color: #dae8ec;
    color: rgb(233,78,31);
    display: block; 
    font-weight: bold; 
    margin: 0 auto;         
    padding: 9px 18px 9px; 
    text-decoration: none;
    border: 1px solid black;
    border-radius: 2px; 
}

nav ul ul {
    display: none;
    position: absolute; 
    top: 100%;
}

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

nav ul ul li {
    float: none;
    position: relative;
}

nav ul ul li a {
    padding: 7px 30px;
    color: rgb(233,78,31);
}

nav ul li a:hover {
    background: rgb(138, 92, 132);
    color:#dae8ec;
}

If you have any suggestions or tips on how to achieve equal sizing for the dropdown menu, it would be greatly appreciated. Thank you!

Answer №1

Check out my JS Fiddle Demo

<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu Withs</a>
            <ul>
                <li><a href="#">opt 1</a></li>
                <li><a href="#">opt 2</a></li>
                <li><a href="#">opt 3</a></li>
            </ul>
        </li>
        <li><a href="#">Whatnot</a></li>                
    </ul>
</nav>

Cheers to a successful project!!

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

Guide to effectively retrieving data from S3 and displaying a view at regular intervals of 4 seconds

In my current project, I am working on fetching a file from S3 and utilizing the data within that file to generate a map on a webpage. To achieve this task, I have set up an Express server along with the EJS templating engine for serving and rendering the ...

What are the best techniques for positioning text next to images in HTML and CSS?

I have attempted to position the text in close proximity to the images as shown in the picture below (to allow for spacing in the middle). As depicted in the image, the "AVENUE FOR GROWTH" and "NETWORKING OPPORTUNITIES" texts are near Man's hand and w ...

JavaScript and jQuery validation issues persisting

Here is the HTML code I am using: <script src="js/validate.js"></script> <label>FIRST NAME:</label> <td> <input type="text" class="firstname" id="firstname" onKeyUp="firstname()" /> </td> <td> <label id=" ...

Activate the WebDAV feature within the .htaccess file

Can WebDAV be activated through an .htaccess file? I'm experiencing issues with Plesk not recognizing my modified config files that have WebDAV enabled. Is it feasible to enable WebDAV without using an .htaccess file? ...

In JavaScript, a prompt is used to request the user to input a CSS property. If the input is incorrect,

Implement a while loop that continuously prompts the user to enter a color. If the color entered matches a CSS property such as blue, red, or #000000: The background will change accordingly, but if the user enters an incorrect color, a message will be dis ...

Troubleshooting issues with media queries related to device pixel ratio

My media queries in LESS are functioning correctly on the desktop, but encountering issues when tested on an iPad2 or Samsung Galaxy 4 phone. It appears that the device-pixel-ratio is causing disruptions. I attempted to reduce the min-device-pixel-ratio to ...

What is the best method for managing file storage and retrieval in web applications that only run on the client-side?

I'm currently working on an application where users can create their own data. Without a backend or database, all the information is stored within the user's session. My preferred solution involves: User interacts with the app, clicks "Save", ...

I am currently working on a one-page website that will feature both a Careers form and a Contact Us form. However, I am facing a challenge with the submission

I am currently working on integrating Careers FORM and Contact Us FORM into a single page website. However, I am facing an issue with the form Submit Buttons. The problem arises during validation of the input boxes. How can I differentiate between the two ...

Tips for modifying a text input in a list

Managing comment boxes and buttons for each box can be a bit tricky. Clicking on the edit button should enable only that specific textbox, while save or cancel actions should hide the text box and show "<p></p>". Below is my ng-template where ...

Which specific CSS attributes should be applied to create a scroll bar within this table?

Below is the table that I am working with: 'table' I want to ensure that its width remains the same even when the screen size is reduced, and only add a scroll bar to view it. I have already included overflow: scroll; in the container <div> ...

The use of HTML 5 Autofocus causes interference with CSS loading

Whenever I apply the autofocus="autofocus" attribute to an input element, it causes a brief glitch in Firefox. The content briefly appears without the CSS styling applied, resulting in elements not centered and headings rendered in default fonts. If I tak ...

Ensuring the accuracy of user input in JavaScript, even after fixing a mistake and submitting again, will not cause the code to exit the else clause

I am currently working on a coupon code field that needs to accept a specific set of coupon keys. Below is the code I have written for validation. If a user enters an invalid key at first, they receive an alert saying "Invalid Key." However, if they corr ...

Unsuccessful conversion of JSON data to HTML using json2html in Python

I have been experimenting with converting JSON data to HTML using json2html. The JSON data structure I am working with is as follows: json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username" ...

Internet Explorer version 8 and above tend to load HTML at a slower pace compared to other web browsers

I have encountered a problem while loading ten separate dropdown lists, each containing around 13,000 options from the same data set. Despite confirming that the issue lies within the HTML rather than the code used to populate them (by saving and directly ...

Having trouble interacting with an xpath-selected element in Selenium using Python?

I've been attempting to click on an element that I've selected using Xpath, but it appears that I am unable to locate the element. Specifically, I'm trying to click on the "Terms of Use" button on the page. The code snippet I've written ...

Splitting a square into four triangles using CSS styling

My current challenge involves creating a square comprised of 4 triangles of equal size, each with hover events. Here is the CSS I'm using to create the triangles: .right, left, .top, .bottom { position: relative; width: 26px; } .right:before ...

What is the best way to create a new row at a specific index in an ng-repeat loop?

My Goal: I am aiming to insert a new row of ul after every 2 elements in my ng-repeat loop. For example: <ul class="col-sm-2"> <li><p>Automobile & Motorcycle</p></li> ...

Adding elements within a loop using jquery

I am currently working on adding a toggle button using Javascript. I want to include three span tags inside it as well. To achieve this, I am creating a span variable and attempting to append it within a basic FOR loop that iterates 3 times. Below is the ...

Ensure that the floated element stretches to 100% height in order to completely fill the page

I'm working on achieving a height of 100% for the left sidebar so that it fills the page regardless of the size of the "main" div. Currently, it stops at the normal page height and doesn't expand further. Is there any way to make this work as i ...

When encountering a 404 redirect, CSS and JS files may fail to display

I created a unique error message using .htaccess, incorporating CSS and JS in separate folders. Although it functions properly when accessed directly, integrating these resources into the 404 Error message results in only the HTML text being displayed with ...