I am trying to position the sub menus right under the main menu in CSS Grid while hovering over the multiple main menu comps.
I could not figure out how to position the sub menu within the CSS grid.
I adapted the HTML and CSS code from here : https://css-tricks.com/solved-with-css-dropdown-menus/
Working:
When I hover over the main menu the sub menu appears
Not Working:
All of the sub menus from multiple main menu components appear on the same position and I can't figure out how to position them right under the main menu comp.
HTML
<ul>
<li class="home menu">Home
<ul class="home-dropdown">
<li>Vocalcoaching</li>
<li>Circlesinging</li>
</ul>
</li>
<li class="uber menu">Über mich</li>
<li class="vocal menu">Vocalcoaching
<ul class="vocal-drop">
<li>Gesangunterricht</li>
<li>Songwriting</li>
<li>Technische Geräte</li>
</ul>
</li>
<li class="circle menu">Circlesinging
<ul class="circle-drop">
<li>Was ist Was</li>
<li>Volume 1</li>
<li>Volume 2</li>
<li>Volume 3</li>
</ul>
</li>
CSS
ul {
display:grid;
grid-template-columns: repeat(7, minmax(10%, 1fr));
justify-items: center;
margin: 5% 10%;
text-align: left;
}
ul li {
padding: 2% 0;
}
li {
list-style-type: none;
display: block;
transition-duration: 1.5s;
}
li:hover {
cursor: pointer;
}
ul li ul {
visibility: hidden;
opacity: 0;
position: absolute;
transition: all 0.15s ease;
margin-top: 1rem;
left: 0;
display: none;
padding:0;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
background-color:beige;
}
ul li:hover > ul,
.home-dropdown li .home-dropdown:hover {
margin-right: 60%;
}
ul li ul li {
clear: both;
width: 100%;
margin-top:2%;
}
I can't align the sub menu position right under the specific menu menu using CSS GRID
You simply need to add position: relative; to your main menu li items.
Your submenus (ul li ul) are position: absolute;, so their parent li needs the position: relative; property set. Absolute positioned elements will position themselves to the nearest non-static element, so without setting the parent main menu item to relative, they are positioning themselves all to a container element.
ul {
display:grid;
grid-template-columns: repeat(7, minmax(10%, 1fr));
justify-items: center;
margin: 5% 10%;
text-align: left;
}
ul li {
padding: 2% 0;
}
li {
list-style-type: none;
display: block;
transition-duration: 1.5s;
position: relative;
}
li:hover {
cursor: pointer;
}
ul li ul {
visibility: hidden;
opacity: 0;
position: absolute;
transition: all 0.15s ease;
margin-top: 1rem;
left: 0;
display: none;
padding:0;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
background-color:beige;
}
ul li:hover > ul,
.home-dropdown li .home-dropdown:hover {
margin-right: 60%;
}
ul li ul li {
clear: both;
width: 100%;
margin-top:2%;
}
<ul>
<li class="home menu">Home
<ul class="home-dropdown">
<li>Vocalcoaching</li>
<li>Circlesinging</li>
</ul>
</li>
<li class="uber menu">Über mich</li>
<li class="vocal menu">Vocalcoaching
<ul class="vocal-drop">
<li>Gesangunterricht</li>
<li>Songwriting</li>
<li>Technische Geräte</li>
</ul>
</li>
<li class="circle menu">Circlesinging
<ul class="circle-drop">
<li>Was ist Was</li>
<li>Volume 1</li>
<li>Volume 2</li>
<li>Volume 3</li>
</ul>
</li>
</ul>
.outer {
display: grid;
grid-template-columns: repeat(7, minmax(10%, 1fr));
}
li {
list-style-type: none;
}
.dropdown {
display: none;
padding: 0;
}
.menu:hover>.dropdown,
.dropdown:hover {
display: block;
}
<ul class="outer">
<li class="home menu">Home
<ul class="dropdown home-dropdown">
<li>Vocalcoaching</li>
<li>Circlesinging</li>
</ul>
</li>
<li class="uber menu">Über mich</li>
<li class="vocal menu">Vocalcoaching
<ul class="dropdown vocal-drop">
<li>Gesangunterricht</li>
<li>Songwriting</li>
<li>Technische Geräte</li>
</ul>
</li>
<li class="circle menu">Circlesinging
<ul class="dropdown circle-drop">
<li>Was ist Was</li>
<li>Volume 1</li>
<li>Volume 2</li>
<li>Volume 3</li>
</ul>
</li>
Related
I am having trouble converting my default vertical drop-down menu to a horizontal menu.
This is the website: www.ritasaraci.com/homepage
I want my links to appear horizontally rather than vertically.
Thanks,
Change Css property of display to display:flex
Css Class edited
.highlighted .submenu {
/other css/
display:flex;
}
if you are using wordpress open customizer open Css editor and paste there for quick edits
use !important to make it stick
keep it consistent and use CSS to change states
.menu:hover .submenu {
css
}
HTML
<ul class="menu">
<li>
Item 1
<ul class="sub-menu">
<li>Sub-item 1-1</li>
<li>Sub-item 1-2</li>
<li>Sub-item 1-3</li>
<li>Sub-item 1-4</li>
<li>Sub-item 1-5</li>
<li>Sub-item 1-6</li>
</ul>
</li>
<li>
Item 2
</li>
<li>
Item 3
<ul class="sub-menu">
<li>Sub-item 3-1</li>
<li>Sub-item 3-2</li>
<li>Sub-item 3-3</li>
<li>Sub-item 3-4</li>
</ul>
</li>
<li>
Item 4
</li>
</ul>
CSS
ul.menu{
width: 940px;
height: 40px;
line-height: 40px;
position: relative;
text-align: center;
margin: auto;
padding: auto;
background-color:#dedede;
}
li{
float: left;
width: auto;
}
ul.menu li a {
display: block;
width: auto;
text-decoration: none;
}
ul.sub-menu {
opacity: 0;
visibility: hidden;
display: block;
position: absolute;
left: 0;
height: 40px;
line-height: 40px;
}
ol, ul {
list-style: none;
}
ul.menu a, ul.sub-menu a {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #0E0500;
padding: 0 10px;
}
ul.menu li:hover ul {
width: 940px;
background: rgb(177, 200, 218);
visibility: visible;
opacity: 1;
transition-property: opacity;
}
ul.menu li:hover {
background-color: rgb(177, 200, 218);
transition-duration: 0.3s;
}
I am unable to reach the list element that is positioned at level 2, right after the level 3 submenu because when I hover out of the level 3 submenu the whole menu collapses to level 1.
The item that I am unable to reach is titled "Unreachable" in the code.
N.B.: I am able to reach the item if I bypass the level 2 menu, i.e. approach it without triggering level 2. But most of the time, the end user will hover over level 2 before approaching that item and will not be able to reac it.
.side-nav-bar-menu {
position: absolute;
height: 100%;
overflow: hidden;
white-space: nowrap;
width: 19.692em;
padding-top: 0.385em;
}
.side-nav-bar-menu ul {
list-style: none;
padding: 0;
margin: 0;
background-color: red;
}
.side-nav-bar-menu li:hover {
background-color: blue
}
.side-nav-bar-menu ul li ul {
display: none;
}
.side-nav-bar-menu ul li:hover > ul {
display: block;
}
.side-nav-bar-menu ul li ul li {
margin-left: 15px;
}
.side-nav-bar-menu ul li ul li ul li {
margin-left: 30px;
}
<div class="side-nav-bar-menu">
<ul>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
<li>
<a>L2</a>
<ul>
<li><a>L3</a></li>
<li><a>L3</a></li>
</ul>
</li>
<li><a>Unreachable</a></li>
</ul>
</li>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
</ul>
</li>
</ul>
</div>
Updated with new fix
Try this:
.side-nav-bar-menu {
position: absolute;
height: 100%;
overflow: hidden;
white-space: nowrap;
width: 19.692em;
padding-top: 0.385em;
}
.side-nav-bar-menu ul {
list-style: none;
padding: 0;
margin: 0;
background-color: red;
}
.side-nav-bar-menu li:hover {
background-color: blue
}
.side-nav-bar-menu ul li ul {
display: none;
}
.side-nav-bar-menu ul li:hover ul{
display: block;
}
.side-nav-bar-menu ul li ul li {
margin-left: 15px;
}
.side-nav-bar-menu ul li ul li ul li {
margin-left: 30px;
}
<div class="side-nav-bar-menu">
<ul>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
<li>
<a>L2</a>
<ul>
<li><a>L3</a></li>
<li><a>L3</a></li>
</ul>
</li>
<li><a>Reachable</a></li>
</ul>
</li>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
</ul>
</li>
</ul>
</div>
I created a menu with multiple submenus. I've been searching for ways to make the submenus dropdown in a horizontal fashion from the original menu to the submenu, and then to the final submenu (which I can sometimes get by accident, but then I screw everything up and go back to my original horizontal menu with vertical submenus). I've tried changing them to in-line block, static, and block, but I can't force it to work. Is there an easier way? What am I missing?
/* Navigation Bar Menu */
nav {
color: #F00;
min-width: 100%;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display: inline-block;
float: none;
white-space: nowrap;
}
nav a {
display: block;
padding: 0 10px;
color: #F00;
font-size: 20px;
line-height: 30px;
text-decoration: none;
}
nav a:hover {
color: #FFF;
background-color: #CCC;
transition: all .3s ease;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
}
nav ul li:hover>ul {
display: inherit;
transition: all .3s ease;
}
nav ul ul li {
min-width: 170px;
display: list-item;
position: relative;
}
nav ul ul ul {
position: absolute;
top: 0;
left: 100%;
}
<nav>
<ul>
<li>Home</li>
<li>About
<ul>
<li>Our Team</li>
</ul>
</li>
<li>Services
<ul>
<li>Cardiovascular
<ul>
<li>Perfusion</li>
<li>PTCA & IABP</li>
<li>ECMO</li>
<li>TAVR</li>
</ul>
</li>
<li>Blood Management
<ul>
<li>Autotransfusion</li>
<li>Platelet Gel</li>
</ul>
</li>
</ul>
</li>
<li>Products
<ul>
<li>Disposables</li>
<li>Featured Products</li>
</ul>
</li>
<li>Contact
<ul>
<li>Employment Inquiries</li>
<li>Contact</li>
</ul>
</li>
</ul>
</nav>
Sorry if I'm missing something, but is this what you're looking for?
https://codepen.io/will0220/pen/VMMgMb
This
nav ul ul li {
display: list-item;
}
Needs the display property removed, display: list-item forces it into rows. Hope this helps!
I am attempting to make a drop-down menu. My problem is that some of the drop-down items are wider than their parent item. This is causing my other menu items to be displaced. In this case, hovering above "Practice Areas" will displace "About".
Hopefully a css guru can help me with this.
Thanks, CPR
ul {
padding: 0;
}
.main-nav>ul>li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
}
.sub-menu li {
display: none;
position: relative;
top: 0;
left: 0;
}
.main-nav ul li:hover > ul.sub-menu li {
display: block;
}
<div class='main-nav'>
<ul>
<li>Practice Areas
<ul class='sub-menu'>
<li>Consumer Bankruptcy</li>
<li>Personal Injury</li>
</ul>
</li>
<li>About
<ul class='sub-menu'>
<li>Meet us</li>
<li>Testimonials</li>
</ul>
</li>
</ul>
</div>
I've added
.sub-menu {
position: absolute;
}
ul {
padding: 0;
}
.main-nav>ul>li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
}
.sub-menu {
position: absolute;
}
.sub-menu li {
display: none;
position: relative;
top: 0;
left: 0;
}
.main-nav ul li:hover > ul.sub-menu li {
display: block;
}
<div class='main-nav'>
<ul>
<li>Practice Areas
<ul class='sub-menu'>
<li>Consumer Bankruptcy</li>
<li>Personal Injury</li>
</ul>
</li>
<li>About
<ul class='sub-menu'>
<li>Meet us</li>
<li>Testimonials</li>
</ul>
</li>
</ul>
</div>
You need to absolutely position the sub menu....
ul {
padding: 0;
}
.main-nav > ul > li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
}
ul > li > ul {
position: absolute;}
.sub-menu li {
display: none;
}
.main-nav ul li:hover > ul.sub-menu li {
display: block;
}
<div class='main-nav'>
<ul>
<li>Practice Areas
<ul class='sub-menu'>
<li>Consumer Bankruptcy</li>
<li>Personal Injury</li>
</ul>
</li>
<li>About
<ul class='sub-menu'>
<li>Meet us</li>
<li>Testimonials</li>
</ul>
</li>
</ul>
</div>
You can set the width of each li item. I used 30% in the css for .main-nav>ul>li.
.main-nav>ul>li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
width: 30%; <===== added this
}
Of course, this won't work well if you need more than 3 li items in your navigation bar. I would suggest using bootstrap. Here is the bootstrap navbar
ul {
padding: 0;
}
.main-nav>ul>li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
width: 30%;
}
.sub-menu li {
display: none;
position: relative;
top: 0;
left: 0;
}
.main-nav ul li:hover > ul.sub-menu li {
display: block;
}
<div class='main-nav'>
<ul>
<li>Practice Areas
<ul class='sub-menu'>
<li>Consumer Bankruptcy</li>
<li>Personal Injury</li>
</ul>
</li>
<li>About
<ul class='sub-menu'>
<li>Meet us</li>
<li>Testimonials</li>
</ul>
</li>
</ul>
</div>
I need the submenu to show when hovering over the corresponding navigation element on the navigation bar. For the second level navigation list, I set display to none as follows
nav ul ul {
position:absolute; top: 100%;
background-color: #2b0306;
display: none;
}
and set the the first navigation list to display as inline-block as follows:
nav ul li a {
display: inline-block;
color: #fff;
padding: 10px 20px;
text-decoration: none;
width: 250px;
position: relative;
}
nav ul li a:visited { color: #fff; }
nav ul li a:hover { background-color: #6d0911; }
nav ul ul { position:absolute; top: 100%; background-color: #2b0306; display: none; }
nav ul ul li { position: relative; }
nav ul ul ul { left: 100%; top: 0px; }
As for the nav position, I set it to absolute:
nav {
background-color: rbga(0,0,0,.65);
position: absolute;
top: 0px; left: 0px;
padding: 50px 0 0 0;
width: 100%;
}
nav::after { content: ' '; display: block; clear: both; }
nav ul { list-style: none; margin: 0; padding: 0px; }
nav ul li: hover { background-color: #2b0306; }
nav ul li: hover > ul { display: block; }
/* top-level */
nav > ul { padding-left: 300px; }
nav > ul > li { float: left; }
nav > ul > li > a {
width: auto;
padding: 10px 20px 15px 20px;
}
And this is the HTML code:
<nav>
<ul><!--first level navigation-->
<li><a title="About Us" href="aboutATMC.php" >About Us</a></li>
<li>
<a title="Services" href="#" aria-haspopup ="true">Services</a>
<ul><!--Second level navigation-->
<li><a title="Consultancy" href="#">Consultancy</a></li>
<li>
<a title="Learning & Development Solutions" href="#" aria-haspopup ="true">Learning & Development Solutions</a>
<ul><!--Third level navigation-->
<li><a title="Training & Coaching" href="#">Training & Coaching</a></li>
<li><a title="Learning Material" href="#">Learning Material</a></li>
</ul><!--End of third level-->
</li>
</ul><!--end of second level-->
</li>
<li><a onclick="toggleNavPanel ('contact_panel')" id = "contactus" href="#" >Contact Us <span id="navarrow"> ▾</span></a></li>
</ul><!--End of first level-->
</nav>
Any advice please?
Try this:
/* top menu */
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav li a {
padding: 10px;
display: inline-block;
}
nav > ul > li {
display: inline-block;
}
/* sub menu */
nav > ul > li > ul {
display: none;
position: absolute;
background-color: #ccc;
}
nav ul li:hover {
background-color: #ccc;
}
nav ul li:hover ul {
display: block;
}
<nav>
<ul>
<li>
Menu A
<ul>
<li>Submenu 1</li>
<li>Submenu 2</li>
<li>Submenu 3</li>
</ul>
</li>
<li>
Menu B
</li>
</ul>
</nav>
Here I added some HTML code
<nav>
<ul>
<li>
<a href='#'>Test</a>
<ul id='submenu'>
<li>Submenu Test</li>
<li>Submenu Test</li>
<li>Submenu Test</li>
</ul>
</li>
<li><a href='#'>Test</a></li>
<li><a href='#'>Test</a></li>
</ul>
</nav>
<style>
nav {
background-color: rbga(0,0,0,.65);
position: absolute;
top: 0px; left: 0px;
padding: 50px 0 0 0;
width: 100%;
}
nav ul ul {
position:absolute;
top: 100%;
background-color: #2b0306;
display: none;
}
nav ul li:hover > #submenu {
display: block;
}
nav ul li a {
display: inline-block;
padding: 10px 20px;
text-decoration: none;
width: 250px;
position: relative;
}
</style>
If you ever see the box appears as you hover, great!