two columns for dynamic drop down menu - html

I want to make a two columns drop down menu like this.
Main Menu 1 | Main Menu 2 | Main Menu 3
Sub Menu 1 | Sub Menu 5
Sub Menu 2 | Sub Menu 6
Sub Menu 3 | Sub Menu 7
Sub Menu 4
Here is my code.
<style>
ul{
list-style:none;
}
li{
float:left;
}
li li {
width:50%;
}
</style>
<ul>
<li>Main Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
<li>Sub Menu 5</li>
<li>Sub Menu 6</li>
<li>Sub Menu 7</li>
</ul>
</li>
<li>Main Menu 2</li>
<li><a href="#" target="_self" >Main Menu 1</a></li>
</ul>
But if I use float left, it becomes this.
Main Menu 1 | Main Menu 2 | Main Menu 3
Sub Menu 1 | Sub Menu 2
Sub Menu 3 | Sub Menu 4
Sub Menu 5 | Sub Menu 6
Sub Menu 7
How can I make it by CSS or Query? Any ideas? Thanks!!

If you need to stick to just HTML, you can either reorder your list items to fall in alternating order:
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 5</li>
<li>Sub Menu 2</li>
<li>Sub Menu 6</li>
<li>Sub Menu 3</li>
<li>Sub Menu 7</li>
<li>Sub Menu 4</li>
</ul>
Which isn't great, but works.
Or, you can separate the list into two lists (also not the best solution).
<ul class="col1">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
</ul>
<ul class="col2">
<li>Sub Menu 5</li>
<li>Sub Menu 6</li>
<li>Sub Menu 7</li>
</ul>
If your content is generated elsewhere, or you're unable to adjust your lists for some reason, A List Apart has a good article on this: http://alistapart.com/article/multicolumnlists

The answer: google it. "CSS multi-level navigation" brought me 29 800 000 results.
http://www.htmldog.com/articles/suckerfish/dropdowns/
http://alistapart.com/article/dropdowns
http://speckyboy.com/2013/01/30/more-responsive-navigation-solutions/
http://www.noupe.com/css/multilevel-drop-down-navigation-menus-examples-and-tutorials.html
http://woork.blogspot.be/2009/09/how-to-implement-perfect-multi-level.html
http://geek.ryanhellyer.net/products/suckerfish_css/
And tons of others.

Related

Mega menu with flex, divide into 4 columns automatically

I'm trying to build a mega menu with flexbox. The only issue I have left figuring out how I can "Redskap & tillbehör" into 3 columns so it fills the column before and after it. Right now it's just cutting out some of the items.
http://dataring.se/dev/wiggs3/
Here is how I want it to look
I could achieve 4 column menu layout with very basic css that solves your use case. Additional styling as per your design guidelines you can include to this
body {
margin: 0;
}
ul {
padding: 0;
list-style: none;
}
.menu-items {
columns: 4;
display: none;
}
.menu > li:hover .menu-items {
display: block;
}
<ul class="menu">
<li>
Main Menu
<ul class="menu-items">
<li>
Menu Item Header
<ul>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
</ul>
</li>
<li>
Menu Item Header
<ul>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
</ul>
</li>
<li>
Menu Item Header
<ul>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
</ul>
</li>
<li>
Menu Item Header
<ul>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
</ul>
</li>
<li>
Menu Item Header
<ul>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
<li>Sub menu item</li>
</ul>
</li>
</ul>
</li>
</ul>

Dropdown Menu Items Open Outside the Container

I am creating a mobile menu as dropdown list items as follows,
jQuery('#menu-main-menu > .is-dropdown-submenu-parent a').click(function() {
$(this).siblings('ul').slideToggle(500);
jQuery(this)
.closest(".is-dropdown-submenu-parent")
.siblings()
.find(".sub-menu")
.slideUp();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main-menu-mobile" class="menu-list navbar-collapse collapse clearfix">
<ul id="menu-main-menu" class="navigation clearfix" style="display:flex; flex-direction: column">
<li data-toggle="collapse" class="dropdown is-dropdown-submenu-parent collapsed">
menu 1
<ul class="sub-menu ">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
</ul>
</li>
<li data-toggle="collapse" class="dropdown is-dropdown-submenu-parent collapsed">
menu 2
<ul class="sub-menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
</ul>
</li>
</ul>
</div>
The problem is when I click into the main menu for opening, it opens outside the frame of the menu (it gets outside the container) explain more: when I click on the menu1 the items of it appear above the menu2. Where it suppose to shift menu2 down and list the items bellow menu1.
I did not get where is the error & I have searched & seen all menus made as I did !!
Try this small change
I have commented slideUp() line in the .js.
I think it works as you want.
jQuery('#menu-main-menu > .is-dropdown-submenu-parent a').click(function() {
$(this).siblings('ul').slideToggle(500);
jQuery(this)
.closest(".is-dropdown-submenu-parent")
.siblings()
.find(".sub-menu")
//.slideUp(); // commented this line
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main-menu-mobile" class="menu-list navbar-collapse collapse clearfix">
<ul id="menu-main-menu" class="navigation clearfix" style="display:flex; flex-direction: column">
<li data-toggle="collapse" class="dropdown is-dropdown-submenu-parent collapsed">
menu 1
<ul class="sub-menu ">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
</ul>
</li>
<li data-toggle="collapse" class="dropdown is-dropdown-submenu-parent collapsed">
menu 2
<ul class="sub-menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
</ul>
</li>
</ul>
</div>

How to select only the parent of an unordered list, using only CSS?

Without changing the HTML, which pains me, how do I only select the parent of an unordered list for CSS?
I have something like the following:
<div class="content">
<ul class="navigation">
<li>Item 1
<ul>
<li>Sub Item A</li>
<li>Sub Item B</li>
</ul>
</li>
<li>Item 2</li>
</ul>
</div>
I need to grab only Item 1 for altering it's CSS. I've attempted things like:
ul li a - won't work because it will grab Item 2
ul li a - won't work because it will grab Item 2
ul.navigation li - won't work because it will grab Item 2
ul:first-child - won't work because it will hit Item 1 and Sub Item A
Any thoughts?
You can do it like this:
.navigation > li:first-child > a {color: red}
<div class="content">
<ul class="navigation">
<li>Item 1
<ul>
<li>Sub Item A</li>
<li>Sub Item B</li>
</ul>
</li>
<li>Item 2</li>
</ul>
</div>
Or like this:
.navigation > li:first-of-type > a {color: red}
<div class="content">
<ul class="navigation">
<li>Item 1
<ul>
<li>Sub Item A</li>
<li>Sub Item B</li>
</ul>
</li>
<li>Item 2</li>
</ul>
</div>

floating sub menu contains on its parent instead of overlapping the parent

Below is my snippet. First, I have two different link which triggered a drop down sub menu when hovered. Now the problem is, If you hover on the "First menu" (this is a fixed position), its drop down sub menu does not overlap like the second drop down sub menu from the second menu, it contains itself to its parent which seem's ugly, any ideas, help, clues, suggestions, recommendations to fix this please?
a{text-decoration:none;color:#000000;}
ul,li{list-style:none;padding:0px;}
#menu{overflow-y:auto;overflow-x:hidden;max-height:50px;background:#ededed;color:#ffffff !important;padding:15px;position:fixed;width:300px}
.sub_menu{
position:absolute;
z-index:99999999999;
background:red;
padding:15px;
color:#000000;
display:none;
}
.d_menu a:hover + .sub_menu{display:block !important;}
#menu2{overflow-y:auto;overflow-x:hidden;max-height:50px;background:#ededed;color:#ffffff !important;padding:15px;margin-top:50px;}
<div id="menu">
<ul class="d_menu">
<li>
This is first menu, this has a fixed position
<ul class="sub_menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
<li>sub menu 5</li>
<li>sub menu 6</li>
<li>sub menu 7</li>
<li>sub menu 8</li>
<li>sub menu 9</li>
<li>sub menu 10</li>
</ul>
</li>
<li>menu 2</li>
<li>menu 3</li>
<li>menu 4</li>
<li>menu 5</li>
</ul>
</div>
<br>
<br>
<div id="menu2">
<ul class="d_menu">
<li>
This is second menu, this has a static position
<ul class="sub_menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
<li>sub menu 5</li>
<li>sub menu 6</li>
<li>sub menu 7</li>
<li>sub menu 8</li>
<li>sub menu 9</li>
<li>sub menu 10</li>
</ul>
</li>
</ul>
</div>
You just have to remove overflow-y and overflow-x and it will work :
a{text-decoration:none;color:#000000;}
ul,li{list-style:none;padding:0px;}
#menu{max-height:50px;background:#ededed;color:#ffffff !important;padding:15px;position:fixed;width:300px}
.sub_menu{
position:absolute;
z-index:99999999999;
background:red;
padding:15px;
color:#000000;
display:none;
}
.d_menu a:hover + .sub_menu{display:block !important;}
#menu2{overflow-y:auto;overflow-x:hidden;max-height:50px;background:#ededed;color:#ffffff !important;padding:15px;margin-top:50px;}
<div id="menu">
<ul class="d_menu">
<li>
This is first menu, this has a fixed position
<ul class="sub_menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
<li>sub menu 5</li>
<li>sub menu 6</li>
<li>sub menu 7</li>
<li>sub menu 8</li>
<li>sub menu 9</li>
<li>sub menu 10</li>
</ul>
</li>
</ul>
</div>
<br>
<br>
<div id="menu2">
<ul class="d_menu">
<li>
This is second menu, this has a static position
<ul class="sub_menu">
<li>sub menu 1</li>
<li>sub menu 2</li>
<li>sub menu 3</li>
<li>sub menu 4</li>
<li>sub menu 5</li>
<li>sub menu 6</li>
<li>sub menu 7</li>
<li>sub menu 8</li>
<li>sub menu 9</li>
<li>sub menu 10</li>
</ul>
</li>
</ul>
</div>

Three level CSS menu with first layer in horizontal layout

What I'm after is to have the top level ul lis laid out horizontally, and then subsequent levels laid out vertically, preferably indented slightly as levels go down, under their parent li.
Not after any hover functionality, just a nice layout, something like this:
Top Item 1 Top Item 2 Top Item 3
sub item 1 sub item 1 sub item 1
sub item 2 sub item 2 sub item 2
sub sub item 1 sub item 3 sub item 3
sub sub item 2 sub item 4 sub item 4
sub item 5 sub item 5 sub item 5
How about this:
Live Demo
CSS:
#footer {
overflow: auto;
background: #ddd
}
.outer {
list-style: none;
width: 33%;
float: left;
margin: 0;
padding: 0
}
.first {
font-weight: bold
}
.outer ul {
list-style: none;
margin: 0;
padding: 0 0 0 18px
}
HTML:
<div id="footer">
<ul class="outer">
<li class="first">Top Item 1</li>
<li>
<ul>
<li>sub item 1</li>
<li>sub item 2</li>
<li>
<ul>
<li>sub item 1</li>
<li>sub item 2</li>
</ul>
</li>
<li>sub item 5</li>
</ul>
</li>
</ul>
<ul class="outer">
<li class="first">Top Item 2</li>
<li>
<ul>
<li>sub item 1</li>
<li>sub item 2</li>
<li>sub item 3</li>
<li>sub item 4</li>
<li>sub item 5</li>
</ul>
</li>
</ul>
<ul class="outer">
<li class="first">Top Item 3</li>
<li>
<ul>
<li>sub item 1</li>
<li>sub item 2</li>
<li>sub item 3</li>
<li>sub item 4</li>
<li>sub item 5</li>
</ul>
</li>
</ul>
</div>
here is a copy-paste stuff to play with:
<html>
<head>
<style>
li.level1 { float: left; display: inline; margin-right: 20px }
ul.level2 { display: inline }
li.level2 { display: block; padding-left: 20px }
ul.level3 { display: inline }
li.level3 { display: block; padding-left: 20px }
</style>
</head>
<body>
<ul class="level1">
<li class="level1">apple</li>
<li class="level1">banana
<ul class="level2">
<li class="level2">banana 1
<ul class="level3">
<li class="level3">banana 1 A</li>
<li class="level3">banana 1 B</li>
</ul>
</li>
<li class="level2">banana 2</li>
</ul>
</li>
<li class="level1">cherry
<ul class="level2">
<li class="level2">cherry 1</li>
<li class="level2">cherry 2</li>
</ul>
</li>
<li class="level1">dddd</li>
</ul>
</body>
</html>
Or if you want something pure css then how about something like this:
http://jsfiddle.net/pLFqd/2/
Edit: Missed the bit about not wanting hover functionality, try simple example attached