0

I'm trying to create a simple menu with floated nested lists. The idea is to show all nested lists at once (like a mega menu) but I keep getting unwanted white space due to the floated elements clearing the right floated nested list.

JSFiddle is here: https://jsfiddle.net/agenturallison/mrf5e820/21/

The issue is the space above the "Level2 THIS" li element which should not be there.

How can I force the floated LI element to float up without clearing any content on the right?

HTML code:

ul {
  list-style:none;
  margin: 0;
  padding: 0;
}

li {
  margin: 0;
  padding: 0; 
}

a {
  color: #fff;
}

#container {
  width: 400px;
}

#container li {
  width: 200px;
  float: left; 
  background: blue;
}

#container .level3 {
background: green;
height: 100px;
overflow: visible;
}

#container .level3 li {
  background: black;
  opacity: 0.5;
}
<ul id="container">

  <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
    </ul>
  </li>

 <li><a href="#">Level 2</a></li>
  
 <li><a href="#">Level 2</a></li>
  
 <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
    </ul>
  </li>
  
 <li><a href="#">Level 2 THIS</a></li>
  
 <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
   </ul>
  </li>
  
</ul>

An example of how I'd like it visually to look like is like this

And this is the space I want to get rid off: JSFiddle screenshot

E Allison
  • 51
  • 6
  • You are probably better looking off at using [css columns](https://jsfiddle.net/mrf5e820/23/) if you want it to fill up without leaving gaps, otherwise you may need a masonry plugin. As it stands, I would expect that white gap as your level 3s do not have the same height to fill the gap. [One other option would be flex](https://jsfiddle.net/mrf5e820/24/) but this will equalise level 2s on the same row – Pete Jul 26 '18 at 11:03
  • Can you add an example of how you want the menu to look like? – Shai Jul 26 '18 at 11:10
  • Hi Shai, I've edited my original post with a link to an image which shows how I want it to look and a screenshot from JSFiddle to show the space I want to get rid off. I'd like for the LI to float right up underneath the LI above it. – E Allison Jul 26 '18 at 11:25
  • Hi Pete - thank you for the code, much appreciated! – E Allison Jul 26 '18 at 11:27

2 Answers2

0

The answer I needed was supplied by Pete as a comment (hence I can't mark it as correct answer).

Solution was to abandon floats, and use column-count instead. I also added "inline-block" to the child element to avoid a column break within an element as per How to prevent column break within an element? .

ul {
  list-style:none;
  margin: 0;
  padding: 0;
  column-count:2;
  column-gap:0;
}

li {
  margin: 0;
  padding: 0; 
}

a {
  color: #fff;
}

#container {
  width: 400px;
}

#container li {
  width: 200px;
  background: blue;
  display: inline-block;
}

#container .level3 {
background: green;
height: 100px;
overflow: visible;
width: 200px; 
}

#container .level3 li {
  background: black;
  opacity: 0.5;
}
<ul id="container">

  <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
    </ul>
  </li>

 <li><a href="#">Level 2</a></li>
  
 <li><a href="#">Level 2</a></li>
  
 <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
    </ul>
  </li>
  
 <li><a href="#">Level 2 THIS</a></li>
  
 <li><a href="#">Level 2</a>
    <ul class="level3">
      <li><a href="#">Level 3</a></li>
   </ul>
  </li>
  
</ul>

Thank you!

E Allison
  • 51
  • 6
-1

why don't you try this, it is clean and it can be as nested as you want, credits goes to: https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_multilevel_css&stacked=h

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.dropdown-submenu {
    position: relative;
}

.dropdown-submenu .dropdown-menu {
    top: 0;
    left: 100%;
    margin-top: -1px;
}
</style>
</head>
<body>

<div class="container">
  <h2>Multi-Level Dropdowns</h2>
  <p>In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).</p>
  <p>Note that we have added jQuery to open the multi-level dropdown on click (see script section below).</p>                                        
  <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li><a tabindex="-1" href="#">HTML</a></li>
      <li><a tabindex="-1" href="#">CSS</a></li>
      <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li class="dropdown-submenu">
            <a class="test" href="#">Another dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">3rd level dropdown</a></li>
              <li><a href="#">3rd level dropdown</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

<script>
$(document).ready(function(){
  $('.dropdown-submenu a.test').on("click", function(e){
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
</script>

</body>
</html>
Ray A
  • 1,283
  • 2
  • 10
  • 22
  • Sorry for the confusion, the container height was just to illustrate the problem, in real life the list will have an unknown number of list items. Hence defining a height is not feasible. – E Allison Jul 26 '18 at 14:55