Achieving uniform height for the flyout menu and main menu

Currently, I am in the process of designing a flyout menu and my goal is to ensure that the height of the sub-menu matches that of the main menu.

Below is the html code for the menu that I am currently developing:

<ul id="nav">
    <li class="button"><a>Sub menu title 1</a>
        <ul>
            <li><a>Link1</a></li>
            <li><a>Submenu2</a></li>
        </ul>
    </li>
    <li class="button"><a>Sub menu title 2</a>
        <ul>
            <li><a>Link4</a></li>
            <li><a>Submenu5</a></li>
        </ul>
    </li>
    <li class="button"><a>Sub menu title 3</a>
        <ul>
            <li><a>Link</a></li>
            <li><a>Submenu</a></li>
        </ul>
    </li>
</ul>

Additionally, here is the accompanying css:

#nav {
top: 50%;
left: 0;
width: 200px;
border: 1px solid #000;
}
#nav ul {
width: 200px;
background: #f9f9f9;
position: absolute;
left: -9999em;
}
#nav a {
display: block;
padding: 5px 10px;
}
#nav ul li {
position: relative;
}
#nav li:hover ul {
left: 200px;
top: 0;
}
#nav li:hover > a {
cursor: pointer;
}
.button a {
display: block;
padding: 5px 10px;
}

If you want to view the live demo, you can access it through this link.

My question is, what would be the best approach to ensuring that the flyout menu's height matches that of the main menu?

Answer №1

If you want to set the dimensions of an element using CSS, you can utilize the height property:

#menu {
    top: 50%;
    left: 0;
    width: 200px;
    border: 1px solid #000;
    height: 100px;
}
#menu ul {
    width: 200px;
    background: #f9f9f9;
    position: absolute;
    left: -9999em;
    height: 100px;
}

Answer №2

You've included a JQuery tag in your question, so here is a JQuery solution for you:

console.log($('#nav').height())
var nav_height = $('#nav').height();
$('#nav ul').each(function(){
$(this).css('height', nav_height+'px');
})
#nav {
top: 50%;
left: 0;
width: 200px;
border: 1px solid #000;
}
#nav ul {
width: 200px;
background: #f9f9f9;
position: absolute;
left: -9999em;
}
#nav a {
display: block;
padding: 5px 10px;
}
#nav ul li {
position: relative;
}
#nav li:hover ul {
left: 200px;
top: 0;
}
#nav li:hover > a {
cursor: pointer;
}
.button a {
display: block;
padding: 5px 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="nav">
    <li class="button"><a>Sub menu title 1</a>
        <ul>
            <li><a>Link1</a></li>
            <li><a>Submenu2</a></li>
        </ul>
    </li>
    <li class="button"><a>Sub menu title 2</a>
        <ul>
            <li><a>Link4</a></li>
            <li><a>Submenu5</a></li>
        </ul>
    </li>
    <li class="button"><a>Sub menu title 3</a>
        <ul>
            <li><a>Link</a></li>
            <li><a>Submenu</a></li>
        </ul>
    </li>
</ul>

Answer №3

To implement this in your CSS, simply include the following code:

.button {
    display: flex;
}

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

Passing checkbox values to PHP using Ajax is resulting in a null response

In my attempt to transfer the data from 7 jQuery checkboxes to PHP via Ajax, I have been struggling to populate an array with the values and serialize it before sending it through Ajax. The end goal is to use these checkbox values as conditions in a MySQL ...

The final element that fractures all its sibling elements positioned absolutely

I am experiencing an issue with a container div that contains 5 absolutely positioned images. When I specify the last image as absolutely positioned, all other images lose their positioning and stack at the top. The container itself is relatively position ...

Is it possible for a while loop to display just one row?

<?php include 'db.php'; $sql_locations = "SELECT * FROM location"; $result = mysqli_query($conn,$sql_locations); $count = mysqli_num_rows($result); $markers = array(); while($row = mysqli_fetch_array ...

Issues with CSS rotation not functioning as intended

Seeking assistance with CSS animation: How can I make the elements :before rotate? Link to code on jsFiddle CSS .footerLink{ padding:20px; background:#000; color:#fff; } .footerLink:before{ content:'ABC'; margin-right:15px; ...

I am attempting to initiate an ajax request to my Django server-side backend

I've been working on some code to make an AJAX call to my Django backend, but I keep getting this error message: Reverse for 'delete_skill' with arguments '('',)' not found. 1 pattern(s) tried: ['jobs/delete_skill/(? ...

Are the features of emulation mode in IE11 identical to implementing the meta EmulateIE7 tag?

When using the IE11 browser, I encountered an interesting message on the console: “HTML1122: Internet Explorer is running in Enterprise Mode emulating IE8.” Upon researching this error code on this link, I found out that it is a mode configured by IT ...

"Trouble with the functionality of Animate.css fliptext when triggered by button click

I want to implement a flip animation on text when a button is clicked. I am using the 'flipInY' animation from animate.css library, but it's not working as expected. The desired animation can be seen in the example provided in this link: He ...

Using jQuery to manipulate the radio button input's alternate content in HTML attributes

How can I use Jquery Attr.Radio Button click to write to the div with id #RadioDesc? <input type="radio" desc="sample description" class="AddText"> <script type="text/javascript"> $( document ).ready( function() { $("radio").click ...

What could be causing the CSS styling for a Bootstrap 4 button to fail when it's applied?

Starting my first Bootstrap project and running into issues with CSS for my button. Trying to change it to green but it's not working, even though it worked in the past. This is the button I'm trying to style (the blue one): https://i.sstatic.n ...

The Google Visualization chart fails to display properly once CSS is applied

Struggling with a graph display issue here. It's quite perplexing as it works fine on older laptops and Safari, but not on Chrome or older versions of Firefox. Works like a charm on my old laptop and Safari, but fails on Chrome and Firefox (haven&apo ...

Add a pop of color to the container by applying a background color over the background

I am currently using the bootstrap4 framework to build this page, and I would like to incorporate a blue background color to the div that has the "globe-img" class. I attempted to achieve this by wrapping the content in an inner div (an overlay), but it re ...

Complete 2000 forms using a looping mechanism

I am looking to automate the submission of 2000 forms, with a delay of 6 seconds between each submission. This means that all 2000 forms will be submitted after 12000 seconds. Unfortunately, I am unable to use Ajax or cURL for this task. The only function ...

How can I dynamically populate a multiple select box with data from a PHP query and store it in a multidimensional array using jQuery?

I apologize for any language barriers as English is not my first language. My query is as follows, I am looking to implement three multiple select boxes on a single page. How can I retrieve query data before it is executed in PHP? Each loop result will be ...

Dynamic Dropdown Selections with AJAX

After exploring various resources, I found a solution for creating a multiple drop-down menu on this site: Roshan's Blog The implementation is mostly successful, except for an issue with the third drop-down box. (Dropdown1: Clients, Dropdown2: Loca ...

The expandable row remains open even after filtering the table with JavaScript

An expandable row in the table displays details of rows. I have implemented a JavaScript search and filter function to find specific row content. However, when using the search filter, it successfully shows results but does not expand the details as intend ...

Incorporate a horizontal scrollbar feature within an HTML section

Currently, I am in the process of learning vue js and would like to create a layout that includes two sections. The first section should occupy 3 grids on the screen, be fixed, and not scrollable, with a division into 4 vertical parts. The second section s ...

Delete the hidden attribute from an HTML element using a JavaScript function

Hey there! I have a question for you. Can you assist me in removing an attribute (Hidden) using an Input type button? Here is the script: Thank you for your help! <input type="button" onclick="myfunction()" value="Test"> <hr> <button id= ...

The primary view seamlessly integrates with the page following the invocation of the partial view

Whenever the button is clicked, a different partial view is returned based on the selected value from the drop-down list. Controller: [HttpPost] public ActionResult Foo(SomeViewModel VM) { var model = VM; if (Request.IsAjaxRequest()) { ...

Ways to display URL parameters on an HTML page without using PHP

I'm currently working on a website using HTML (without PHP) and I'm facing an issue with displaying URL parameters on an appointment confirmation page. The appointment details are being successfully passed through the URL parameters, but I'm ...

Choosing identical elements within comparable sub-divisions

How can I use JQuery to select an array of elements that are in separate sub divs? For example: <div class="panelWrap"> <div id="dvStage1" class="milestoneWrap" runat="server"> <div class="grid_1 ...