How to change navigation drop down from hover to click - html

I'm making a responsive navigation bar that has a dropdown that expands when you hover over it. My problem is that this hover feature is not suitable for mobile devices. As such, I am wanting to change my navigation bar to expand on click instead of on hover.
I have tried using the checkbox method but cannot get it to work. I am wanting to only use HTML and CSS if it is possible. Any help will be greatly appreciated!
See it here: JSFiddle
HTML:
<ul id="navigation">
<li>Product 1
<ul>
<li>rx.com </li>
<li>Resources</li>
<li>Copay Cards</li>
</ul>
</li>
<li>Product 2
<ul>
<li>e.com </li>
<li>Resources</li>
<li>Copay Cards</li>
<li>Informational Video</li>
</ul>
</li>
<li>INSOMNIA EDUCATION
<ul>
<li>Complete Night Sleep Resources</li>
<li>sleep.com </li>
</ul>
</li>
<li>PAIN MGMT EDUCATION
<ul>
<li>Safer Pain Management Resources</li>
<li>painmanagement.com </li>
</ul>
</li>
<li>MEDICAL AFFAIRS
<ul>
<li>Request Clinical Reprints</li>
<li>Schedule a Scientific Session </li>
</ul>
</li>
</ul>
CSS:
/* Main Navigation */
#nav {
position:relative;
width:620px;
margin:0 auto;
margin-top:50px;
padding:10px;
}
ul#navigation {
margin:0px auto;
position:relative;
float:left;
}
ul#navigation li {
display:inline;
font-size:12px;
font-weight:bold;
margin:0;
padding:0;
float:left;
position:relative;
}
ul#navigation li a {
width: 234px;
padding:10px 25px;
color: black;
text-decoration:none;
display:inline-block;
background: #ebebeb;
}
ul#navigation li a:hover {
background:#ebebeb;
color:black;
}
ul#navigation li a.first {
border-left: 0 none;
}
ul#navigation li a.last {
border-right: 0 none;
}
ul#navigation li:hover > a {
background:#ebebeb;
}
/* Drop-Down Navigation */
ul#navigation li:hover > ul
{
/*these 2 styles are very important,
being the ones which make the drop-down to appear on hover */
display: block; /* here change visiblity > display */
opacity:1;
}
ul#navigation ul, ul#navigation ul li ul {
list-style: none;
margin: 0;
padding: 0;
/*the next 2 styles are very important,
being the ones which make the drop-down to stay hidden */
/*visibility:hidden;*/
opacity:0;
/*position: absolute; */
display: none; /* change visibility > display */
z-index: 99999;
width:234px;
background:#ebebeb;
}
ul#navigation ul {
top: 43px;
left: 1px;
}
ul#navigation ul li ul {
top: 0;
left: 181px; /* strong related to width:180px; from above */
}
ul#navigation ul li {
clear:both;
width:55px;
}
ul#navigation ul li a {
background:none;
padding:7px 15px;
color:black;
text-decoration:none;
display:inline-block;
border:0 none;
float:left;
clear:both;
width:150px;
}

Add these lines at the bottom of your css file or tag.
.hidden{
display: none;
}
.shown{
display: block;
opacity: 1;
}
The html file:
<li>
<a id="prod1" href="#">Product 1</a>
<ul id="prod1list" class="hidden">
<li>rx.com </li>
<li>Resources</li>
<li>Copay Cards</li>
</ul>
</li>
<script>
document.getElementById('prod1').addEventListener('click', function(e){
e.preventDefault();
document.getElementById('prod1list').classList.toggle('shown');
});
</script>
I tried it and it seems to work fine.

Related

Add second level to existing nav bar Dropdown menu

I have a nice drop down menu in a nav bar. I want to add a second level to it so the second-level menu options pop out to the right from the current drop down options. I found the code for this online a long time ago, forget where.. I don't have enough CSS experience to work it out.
The HTML code - only showing one menu for brevity:
<table border=0 align=center width=900>
<tr>
<td>
<header class="site-header-wrap">
</header>
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix menu">
<!-- NETWORK -->
<li>NETWORK<span class="arrow">▼</span>
<ul class="sub-menu">
<li>BY CLIENT</li>
<li>BY FIREWALL</li>
<li>BY SWITCH</li>
<li>BY ROUTER</li>
<li>ROUTER INTERFACES</li>
</ul>
</li>
</ul>
</nav>
</div>
</td>
</tr>
</table>
the CSS code:
.clearfix:after {
display:block;
clear:both;
content:'';
}
a, a:visited, a:active
{
font-family: arial, verdana;
color: #003333;
font-size: 11px;
text-decoration: underline;
}
a:hover
{
#color: #666666;
color: #534F85;
text-decoration: underline;
}
/*This is for the color of nav panel */
/*----- Menu Outline -----*/
.menu-wrap {
width:100%;
box-shadow:1px 1px 3px rgba(0,0,0,0.2);
background:#282467;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0), #7E2F86, #282467, #B2377E, #282467, #282467, #B2377E, #282467, #7E2F86, rgba(0, 0, 0, 0));
}
.menu {
width:900px;
margin:0px auto;
}
.menu li {
margin:0px;
list-style:none;
/*----- font-family:'Ek Mukta'; -----*/
}
.menu a {
transition:all linear 0.15s;
color:#ffffff;
text-decoration:none;
font-weight:bold;
}
/*This is for the hover text color in the nav panel */
.menu li:hover > a, .menu .current-item > a {
text-decoration:none;
color:#A9A7C2;
font-weight:bold;
}
.menu .arrow {
font-size:11px;
line-height:0%;
}
/*----- Top Level -----*/
.menu > ul > li {
float:left;
display:inline-block;
position:relative;
font-size:12px;
text-decoration:none;
font-family:trebuchet ms,arial,helvetica,sans-serif;
font-weight:bold;
white-space: nowrap;
}
.menu > ul > li > a {
padding:20px 11px;
display:inline-block;
text-shadow:0px 1px 0px rgba(0,0,0,0.4);
}
.menu > ul > li:hover > a, .menu > ul > .current-item > a {
background:#3D3976;
}
/*----- Bottom Level -----*/
.menu li:hover .sub-menu {
z-index:1;
opacity:1;
}
.sub-menu {
width:190%;
padding:2px 6px;
position:absolute;
top:100%;
left:0px;
z-index:-1;
opacity:0;
transition:opacity linear 0.15s;
box-shadow:0px 2px 3px rgba(0,0,0,0.2);
background:#534F85;
}
.sub-menu li {
display:block;
font-size:14px;
}
.sub-menu li a {
padding:3px 15px;
display:block;
}
.sub-menu li a:hover, .sub-menu .current-item a {
background:#282467;
}
So for instance, I want to add 2 submenus under the FIREWALL option for types of firewalls, cisco and paloalto. I tried this:
<li>BY FIREWALL
<ul>
<li>CISCO</li>
<li>PALOALTO</li>
<ul>
<li>
of course it doesn't work. Do I need another class, say something like second-level-menu ? and if so, what does that look like?
For Example:
In CSS:
a {
color: #FFF;
}
nav {
margin: 50px 0;
background-color: #E64A19;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
background-color: #E64A19;
}
nav a {
display:block;
padding:0 10px;
color:#FFF;
font-size:20px;
line-height: 60px;
text-decoration:none;
}
nav a:hover {
background-color: #000000;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
top: 60px; /* the height of the main nav */
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
top:-60px;
left:170px;
}
/* Change this in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
In Html:
<nav>
<ul>
<li>What is it?</li>
<li>
Inventory
<ul>
<li>
X-box 360
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
<li>
PC
<ul>
<li>Building Blocks</li>
<li>Decoration Blocks</li>
<li>Redstone</li>
<li>Transportation</li>
<li>Miscellaneous</li>
<li>Foodstuff</li>
<li>Tools</li>
<li>Combat</li>
<li>Brewing</li>
<li>Materials</li>
</ul>
</li>
<li>
Mobile
<ul>
<li>Materials</li>
<li>Tools &amp; Weapons</li>
<li>Decoration Blocks</li>
<li>Building Blocks</li>
</ul>
</li>
<li>
PS4
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>

How to prevent vertically stacked navigation drop downs from covering other nav links in mobile

I'm making a responsive top navbar that becomes vertically stacked when viewing on smaller screens. My problem is that in mobile view when you hover over a navigation item to expand the dropdown, the dropdown menu covers the other navigation links.
I'm trying to make the lower navigation elements move down to make room for the dropdown menu when a navigation link hovers over. I want to avoid using jQuery or Bootstrap and prefer to only use HTML and CSS if possible.
JSFiddle link (Be sure to resize the window or view on mobile):
https://jsfiddle.net/adilworth/fzrnvukm/3/
What I've tried:
I have played around extensively with changing the CSS position element but I cannot seem to get it to behave as I want.
HTML:
<ul id="navigation">
<li>Product 1
<ul>
<li>rx.com </li>
<li>Resources</li>
<li>Copay Cards</li>
</ul>
</li>
<li>Product 2
<ul>
<li>e.com </li>
<li>Resources</li>
<li>Copay Cards</li>
<li>Informational Video</li>
</ul>
</li>
<li>INSOMNIA EDUCATION
<ul>
<li>Complete Night Sleep Resources</li>
<li>sleep.com </li>
</ul>
</li>
<li>PAIN MGMT EDUCATION
<ul>
<li>Safer Pain Management Resources</li>
<li>painmanagement.com </li>
</ul>
</li>
<li>MEDICAL AFFAIRS
<ul>
<li>Request Clinical Reprints</li>
<li>Schedule a Scientific Session </li>
</ul>
</li>
</ul>
CSS:
/* Main Navigation */
#nav {
position:relative;
width:620px;
margin:0 auto;
margin-top:50px;
padding:10px;
}
ul#navigation {
margin:0px auto;
position:relative;
float:left;
}
ul#navigation li {
display:inline;
font-size:12px;
font-weight:bold;
margin:0;
padding:0;
float:left;
position:relative;
}
ul#navigation li a {
width: 234px;
padding:10px 25px;
color: black;
text-decoration:none;
display:inline-block;
background: #ebebeb;
}
ul#navigation li a:hover {
background:#ebebeb;
color:black;
}
ul#navigation li a.first {
border-left: 0 none;
}
ul#navigation li a.last {
border-right: 0 none;
}
ul#navigation li:hover > a {
background:#ebebeb;
}
/* Drop-Down Navigation */
ul#navigation li:hover > ul
{
/*these 2 styles are very important,
being the ones which make the drop-down to appear on hover */
visibility:visible;
opacity:1;
}
ul#navigation ul, ul#navigation ul li ul {
list-style: none;
margin: 0;
padding: 0;
/*the next 2 styles are very important,
being the ones which make the drop-down to stay hidden */
visibility:hidden;
opacity:0;
position: absolute;
z-index: 99999;
width:234px;
background:#ebebeb;
}
ul#navigation ul {
top: 43px;
left: 1px;
}
ul#navigation ul li ul {
top: 0;
left: 181px; /* strong related to width:180px; from above */
}
ul#navigation ul li {
clear:both;
width:55px;
}
ul#navigation ul li a {
background:none;
padding:7px 15px;
color:#black;
text-decoration:none;
display:inline-block;
border:0 none;
float:left;
clear:both;
width:150px;
}
The problem is in using position: absolute;. Then you find that using visibility: hidden/visible isn't good too :-)
/* Main Navigation */
#nav {
position:relative;
width:620px;
margin:0 auto;
margin-top:50px;
padding:10px;
}
ul#navigation {
margin:0px auto;
position:relative;
float:left;
}
ul#navigation li {
display:inline;
font-size:12px;
font-weight:bold;
margin:0;
padding:0;
float:left;
position:relative;
}
ul#navigation li a {
width: 234px;
padding:10px 25px;
color: black;
text-decoration:none;
display:inline-block;
background: #ebebeb;
}
ul#navigation li a:hover {
background:#ebebeb;
color:black;
}
ul#navigation li a.first {
border-left: 0 none;
}
ul#navigation li a.last {
border-right: 0 none;
}
ul#navigation li:hover > a {
background:#ebebeb;
}
/* Drop-Down Navigation */
ul#navigation li:hover > ul
{
/*these 2 styles are very important,
being the ones which make the drop-down to appear on hover */
display: block; /* here change visiblity > display */
opacity:1;
}
ul#navigation ul, ul#navigation ul li ul {
list-style: none;
margin: 0;
padding: 0;
/*the next 2 styles are very important,
being the ones which make the drop-down to stay hidden */
/*visibility:hidden;*/
opacity:0;
/*position: absolute; */
display: none; /* change visibility > display */
z-index: 99999;
width:234px;
background:#ebebeb;
}
ul#navigation ul {
top: 43px;
left: 1px;
}
ul#navigation ul li ul {
top: 0;
left: 181px; /* strong related to width:180px; from above */
}
ul#navigation ul li {
clear:both;
width:55px;
}
ul#navigation ul li a {
background:none;
padding:7px 15px;
color:black;
text-decoration:none;
display:inline-block;
border:0 none;
float:left;
clear:both;
width:150px;
}
<ul id="navigation">
<li>Product 1
<ul>
<li>rx.com </li>
<li>Resources</li>
<li>Copay Cards</li>
</ul>
</li>
<li>Product 2
<ul>
<li>e.com </li>
<li>Resources</li>
<li>Copay Cards</li>
<li>Informational Video</li>
</ul>
</li>
<li>INSOMNIA EDUCATION
<ul>
<li>Complete Night Sleep Resources</li>
<li>sleep.com </li>
</ul>
</li>
<li>PAIN MGMT EDUCATION
<ul>
<li>Safer Pain Management Resources</li>
<li>painmanagement.com </li>
</ul>
</li>
<li>MEDICAL AFFAIRS
<ul>
<li>Request Clinical Reprints</li>
<li>Schedule a Scientific Session </li>
</ul>
</li>
</ul>
<p> </p>
<div id="doral_resources">
<h4>Resources</h4>
</div>
<p><br /><br /><br /><br /><br /><br /> <br /><br /></p>
<div id="doral_copaycards">
<h4>Copay Cards</h4>
</div>

Why isnt my navigation rendering properly in IE9

Can anyone tell me why this nav renders as it should do in everything but IE9 and how i can work around this.
The project needs to be IE9 compatible.
This works fine in IE11
Here is my code. Thanks.
HTML
<li> Tills
<ul>
<li>Base Unit</li>
<li>Card Reader
<ul id="secondMenu" onclick="navClick()">
<li>Not Reading Cards</li>
<ul></li>
</ul></li></ul>
<li>Cash Drawer</li>
<li>Customer Display</li>
<li>Reciept Printer
<ul id="secondMenu" onclick="navClick()">
<li> Printing with blank areas </li>
<ul></li>
</ul></li></ul>
<li>Scanner
<li>Touchscreen
<ul id="secondMenu" onclick="navClick()">
<li>Black or faint pictiure</li>
<li>Distorted or fuzzy display</li>
</ul>
</li>
</ul>
</li>
<li> Self Check Out</li>
<li> Controller</li>
<li> Photo</li>
<li> iPads
<ul id="secondMenu" onclick="navClick()">
<li> Not connecting to BUK Corporate </li>
<ul></li>
</ul></li></ul>
<li> Handhelds</li>
<li> Personal Computing</li>
<li> Printing</li>
<li> Password Resets</li>
<li> Other</li>
</ul>
</nav>
Here is the CSS
nav {
display:block;
margin:8px 0px 10px 0px;
padding:0;
border-radius:10px;
text-align:center;
width:15.6%;
font-weight: 4000;
float:left;
z-index:999;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul{
list-style:none;
background:#5e6ea6;
}
nav ul li {
position:relative;
text-align:center;
border:4px solid #2f4389;
color:#FFF;
z-index:98;
font-size: 118%;
}
nav ul li a{
text-decoration:none;
color:white;
display:block;
background:#5e6ea6;
padding-bottom:7.9%;
padding-top:7.9%;
}
nav ul li:hover > a{
background-color:orange;
display: block;
}
nav ul ul{
position:absolute;
display:none;
}
nav ul ul {
background: #5f6975;
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
font-weight: 4000;
font-size: 70%;
}
nav ul ul li {
float: none;
border-top: 1px solid #2f4389 ;
border-bottom: 1px solid #2f4389;
position: relative;
width:275px;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li:hover > a {
background: orange;
border:1px solid white;
}
nav ul li:hover ul{
width:100%;
position:absolute;
left:100%;
top:0;
}

css styling of drop down menu

I am trying to style my drop down menu so it would look like on my picture:
![drop down menu]()
So far I got this:
`<nav>
<ul id="home">
<li>HOME</li>
</ul>
<ul id="about">
<li>ABOUT</li>
</ul>
<ul id="business">
<li>BUSINESS GROWTH
<ul class="sub-menu">
<li>BUSINESS GROWTH </li>
<li>FAQs</li>
<li>FEES & SCHEDULING</li>
<li>QUESTIONNAIRE</li>
</ul>
</li>
</ul>
`
#business,
#home,
#about
{
list-style-type: none;
}
#business li{
position:relative;
background-color: #004473;
display:inline-block;
width: 180px;
}
#business li a {
color: #fff;
text-decoration: none;
display:inline-block;
}
#business li ul {
position: absolute;
left: -9999px;
background-color: grey;
margin:0;
padding:0;
list-style-type: none;
width: auto;
}
#business li:hover ul {
left: 0px;
display:inline-block;
}
#business li ul li {
background-color: #004473;
width: 180px;
display:inline-block;
margin:0;
padding:0;
}
#business li ul li a {
color:#fff;
text-decoration: none;
font-style: italic;
}
#business li ul li a:hover {
text-decoration:underline;
}
main issues:
display menu horizontally (home about business growth should be on the same horizontal line)
arrow shape of main menu item
a gap between main menu items and the sub-items
http://codepen.io/anon/pen/oDzbH
Could you please help? Very appreciated.
display menu horizontally (home about business growth should be on the same horizontal line)
add this in css
nav,nav ul{
display:inline-block;
}
arrow shape of main menu item
i assume you want it on hover
ul li > a:hover{
display:block;
background:#fff url(images/arrow.png) no-repeat center bottom;
}
a gap between main menu items and the sub-items
add this in css
ul.sub-menu{
margin-top:10px; /* or whatever suits your layout*/
}

Horizontal navigation sub menu alignment CSS

http://i.stack.imgur.com/OP0kc.jpg
the navigation menu i have created using the CSS and Html code is not working correctly.
The alignment of the navigation menu is not perfect i want in this way : http://i.stack.imgur.com/h4oPK.jpg
I want to convert this to CSS..
Please help
<style>
/* Targeting both first and second level menus */
#nav li {
list-style:none;
float: left;
position: relative;
}
#nav li a {
display: block;
padding: 8px 12px;
text-decoration: none;
}
#nav li a:hover {
background-color:red;
color:#FFF;
opacity:1;
}
/* Targeting the first level menu */
#nav {
top:150px;
min-width:850px;
background:#fff;
opacity:0.5;
display: block;
height: 34px;
z-index: 100;
position: absolute;
}
#nav > li > a {
}
/* Targeting the second level menu */
#nav li ul {
color: #333;
display: none;
position: absolute;
width:850px;
}
#nav li ul li {
display: inline;
}
#nav li ul li a {
background: #fff;
border: none;
line-height: 34px;
margin: 0;
padding: 0 8px 0 10px;
}
#nav li ul li a:hover {
background-color:red;
color:#FFF;
opacity:1;
}
/* Third level menu */
#nav li ul li ul{
top: 0;
}
ul.child {
background-color:#FFF;
}
/* A class of current will be added via jQuery */
#nav li.current > a {
background: #f7f7f7;
float:left;
}
/* CSS fallback */
#nav li:hover > ul.child {
left:0;
top:34px;
display:inline;
position:absolute;
text-align:left;
}
#nav li:hover > ul.grandchild {
display:block;
}
</style>
<ul id="nav">
<li>Home</li>
<li>
Products
<ul class="child">
<li>Hard Drives</li>
<li>Monitors</li>
<li>Speakers
<ul class="child">
<li>10 watt</li>
<li>20 watt</li>
<li>30 watt</li>
</ul>
</li>
<li>Random Equipment</li>
</ul>
</li>
<li>
Services
<ul class="child">
<li>Repairs</li>
<li>Installations</li>
<li>Setups</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
</ul>
Fixed. I think this is what you want http://dabblet.com/gist/2792978
Simply remove position: relative from #nav li and give it to #nav.