To tackle the issue at hand, I resorted to using jQuery since a pure CSS solution wasn't immediately evident. Keep in mind that this is just one potential approach.
My strategy involved iterating through each 1st level <li>
element and determining the tallest <ul>
within it. Subsequently, I set the height of all <ul>
elements within that <li>
to match the computed tallest value.
The JavaScript script utilized for this purpose can be found below (you can also refer to this resource: Use jQuery/CSS to find the tallest of all elements):
$("#menu > ul > li").each(function(){
var maxHeight = 0;
var myUl=$("ul", $(this));
myUl.each(function(){
maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
});
myUl.height(maxHeight);
});
In terms of CSS adjustments, I employed visibility: visible & hidden
to calculate the height of the <ul>
elements, replacing your initial display: none & block
.
This encapsulates all the code in action. Hopefully, you find it beneficial. :)
$("#menu > ul > li").each(function(){
var maxHeight = 0;
var myUl=$("ul", $(this));
myUl.each(function(){
maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
});
myUl.height(maxHeight);
});
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
* {
margin: 0px;
padding: 0px;
}
#main {
max-width: 1000px;
margin: 0 auto;
}
#menu:after {
content: "";
clear: both;
display: block;
}
#menu {
background-color: #000;
}
#menu ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0
}
#menu ul a {
display: block;
color: #231F20;
font-size: 12px;
}
#menu ul li {
float: left;
margin: 0;
padding: 0
}
#menu ul li.current {
background: #ddd
}
#menu ul ul {
position: absolute;
top: 100%;
left: 0;
background: #ffffff;
border: 1px solid #4598cc;
padding: 20px 0px;
z-index: 5;
display:block;
visibility:hidden;
}
#menu ul ul li {
float: none;
width: 200px;
padding: 5px 10px;
}
#menu ul ul a {
color: #4598cc;
display: block;
padding: 5px 0;
font-style: 14px;
font-family: FFMarkStdBook;
}
#menu ul ul ul {
top: -1px;
left: 100%;
height: auto;
}
#menu ul li:hover>ul {
visibility:visible;
}
#menu>ul>li {
float: left;
margin-right: 47px;
position: relative;
}
#menu>ul>li:last-child {
margin-right: 0px;
}
#menu>ul>li>a {
color: #fff;
text-transform: uppercase;
font-size: 14px;
padding: 10px;
text-decoration: none;
}
#menu>ul>li>ul b {
color: #4598cc;
font-size: 14px;
text-transform: uppercase;
font-weight: normal;
}
// Script src link to jQuery library
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
// Main navigation menu html structure
<div id="main">
<nav id="menu">
<ul>
// Menu items go here...
</ul>
</nav>
</div>