What is the best way I can make this menu default collapse when at a mobile size and use a toggle to collapse and expand.
I would prefer this to be CSS only but am unsure how to do it.
This is the source I am working from.
http://www.script-tutorials.com/demos/335/index.html
/* common and top level styles */
#nav span {
display: none;
}
#nav, #nav ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
#nav {
background-color: #F5F5F5;
border-bottom: 5px solid #333333;
float: left;
margin-left: 1%;
margin-right: 1%;
position: relative;
width: 98%;
}
#nav ul.subs {
background-color: #FFFFFF;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
color: #333333;
display: none;
left: 0;
padding: 2%;
position: absolute;
top: 54px;
width: 96%;
}
#nav > li {
border-bottom: 5px solid transparent;
float: left;
margin-bottom: -5px;
text-align: left;
-moz-transition: all 300ms ease-in-out 0s;
-ms-transition: all 300ms ease-in-out 0s;
-o-transition: all 300ms ease-in-out 0s;
-webkit-transition: all 300ms ease-in-out 0s;
transition: all 300ms ease-in-out 0s;
}
#nav li a {
display: block;
text-decoration: none;
-moz-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-ms-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-o-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-webkit-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
white-space: normal;
}
#nav > li > a {
color: #333333;
display: block;
font-size: 1.3em;
line-height: 49px;
padding: 0 15px;
text-transform: uppercase;
}
#nav > li:hover > a, #nav > a:hover {
background-color: #F55856;
color: #FFFFFF;
}
#nav li.active > a {
background-color: #333333;
color: #FFFFFF;
}
/* submenu */
#nav li:hover ul.subs {
display: block;
}
#nav ul.subs > li {
display: inline-block;
float: none;
padding: 10px 1%;
vertical-align: top;
width: 33%;
}
#nav ul.subs > li a {
color: #777777;
line-height: 20px;
}
#nav ul li a:hover {
color: #F55856;
}
#nav ul.subs > li > a {
font-size: 1.3em;
margin-bottom: 10px;
text-transform: uppercase;
}
#nav ul.subs > li li {
float: none;
padding-left: 8px;
-moz-transition: padding 150ms ease-out 0s;
-ms-transition: padding 150ms ease-out 0s;
-o-transition: padding 150ms ease-out 0s;
-webkit-transition: padding 150ms ease-out 0s;
transition: padding 150ms ease-out 0s;
}
#nav ul.subs > li li:hover {
padding-left: 15px;
}
/* responsive rules */
#media all and (max-width : 980px) {
#nav {
display: none;
}
#burger:hover #nav {
display: block;
}
#nav > li {
float: none;
border-bottom: 0;
margin-bottom: 0;
}
#nav ul.subs {
position: relative;
top: 0;
}
#nav li:hover ul.subs {
display: none;
}
#nav li #s1:target + ul.subs,
#nav li #s2:target + ul.subs {
display: block;
}
#nav ul.subs > li {
display: block;
width: auto;
}
}
<ul id="nav">
<li>Home</li>
<li>Menu 1
<span id="s1"></span>
<ul class="subs">
<li>Header a
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
<li>Header b
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
</ul>
</li>
<li class="active">Menu 2
<span id="s2"></span>
<ul class="subs">
<li>Header c
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
<li>Header d
<ul>
<li>Submenu x</li>
<li>Submenu y</li>
<li>Submenu z</li>
</ul>
</li>
</ul>
</li>
<li>Menu 3</li>
<li>Menu 4</li>
<li>Menu 5</li>
</ul>
I've wrapped the navigation in a div. Added a button and am toggling the navigation on and off when at a mobile size. As well as hiding the burger button when at full width.
<div class="nav-bar">
<div id="burger">
<input type="button" data-name="show" value="Toggle" id="toggle">
</div>
<!--rest of navigation follows here-->
</div>
<script>
$(document).ready(function () {
$("#burger").click(function () {
if ($(this).data('name') == 'show') {
$("#nav").animate({
height: '0px'
}).hide()
$(this).data('name', 'hide')
} else {
$("#nav").animate({
height: '100%'
}).show()
$(this).data('name', 'show')
}
});
});
</script>
Related
I have a menu with a drop down that works nicely but the issue I noticed is if you hover a link with a child element, it opens the menu, but the link then become unclickable on the menu on the bottom half.
<div class="desktop_navigation">
<ul>
<li>Link 1</li>
<li>Link 2
<ul>
<li>Link 2 child</li>
</ul>
</li>
<li>Link 3</li>
</ul>
</div>
That's the structure for the menu. The child elements are sub <ul> of the main <li> and then hidden using CSS until their parent <li> is hovered.
The following jFiddle will include all the CSS I am using and a working example of the issue I am currently having:
https://jsfiddle.net/nrzfa49s/
Your .desktop_navigation ul li ul is inheriting the padding-top: 30px from its parent ul (.desktop_navigation ul) which is covering the parent link (Link 2) making it unclickable.
To fix your problem, update these styles:
.desktop_navigation ul li ul {
list-style: none;
display: none;
padding-top: 0; /*remove the 30px padding*/
}
.desktop_navigation ul li:hover ul {
display: block;
position: absolute;
top: 100%; /*set your top value to be more dynamic based on the height of the parent*/
z-index: 890;
}
Here is a fiddle demoing this solution.
Normally when you style menus like this it is recommended to use the > when styling menu elements because of the nesting (i.e. .desktop_navigation > ul this will prevent the child ul from inheriting the padding)
Took some time, but the issue is here:
.desktop_navigation ul {
list-style: none;
padding: 0;
margin: 0;
padding-top: 30px; //remove this line
}
You only want the padding-top for the ul's to be on the parent/top-level menu. Then if you remove the top attribute from the nested menus, they will display after the link in the top-level menu.
.desktop_navigation a {
color: #ccc;
text-decoration: none;
display: inline-block;
padding: 12px;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li:hover a {
color: #fff;
background: #444;
text-decoration: none;
display: inline-block;
z-index: 1002;
padding: 12px;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li ul li a:link,
.desktop_navigation ul li ul li a:visited,
.desktop_navigation ul li ul li a:active {
z-index: 1001;
width: 100%;
display: block;
color: #444;
background: #fff;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul li ul li a:hover {
width: 100%;
display: block;
color: #111;
z-index: 1002;
background: #ccc;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}
.desktop_navigation ul {
list-style: none;
padding: 0;
margin: 0;
}
.desktop_navigation > ul {
padding-top: 30px;
}
.desktop_navigation ul li {
display: inline-block;
position: relative;
padding: 0;
margin: 0;
z-index: 1002;
}
.desktop_navigation ul li ul {
list-style: none;
display: none;
}
.desktop_navigation ul li:hover ul {
display: block;
position: absolute;
z-index: 890;
}
.desktop_navigation ul li ul li {
float: none;
position: relative;
min-width: 180px;
z-index: 890;
}
<div class="desktop_navigation">
<ul>
<li>Link 1</li>
<li>
Link 2
<ul>
<li>Link 2 child</li>
</ul>
</li>
<li>Link 3</li>
</ul>
</div>
My page no longer applies the :hover effect to my .tabs li element, but I can't for the life of me figure out why. I commented out my jQuery script and it still won't work.
Here's a JSFiddle: https://jsfiddle.net/qpmg4wzq/
<div id="tabs-container">
<ul class="tabs">
<li class="tab current" data-tab="tab-1">tab 1</li>
<li class="tab" data-tab="tab-2">tab 2</li>
<li class="tab" data-tab="tab-3">tab 3</li>
<li class="tab" data-tab="tab-4">tab 4</li>
</ul>
</div>
#tabs-container {
float: left;
width: 100%;
overflow: hidden;
}
.tabs {
padding: 0px;
list-style: none;
clear: left;
float: left;
left: 50%;
}
.tabs li {
display: block;
float: left;
right: 50%;
margin: 0;
padding: 10px 20px 5px 20px;
cursor: pointer;
font-size: 1.1em;
line-height: 2em;
-webkit-transition: color .2s linear;
-moz-transition: color .2s linear;
-ms-transition: color .2s linear;
-o-transition: color .2s linear;
transition: color .2s linear;
-webkit-transition: background .2s linear;
-moz-transition: background .2s linear;
-ms-transition: background .2s linear;
-o-transition: background .2s linear;
transition: background .2s linear;
}
.tabs li:hover {
background: #88abc2!important;
}
.tabs li.current {
background: #d0e0eb;
color: #49708a;
}
.tab-content {
display: none;
padding: 15px;
line-height: 1.4;
}
.tab-content.current {
display: inherit;
}
Thanks.
Your .tab-content is overlapping the .tabs-container so any :hover action you make is actually registering as a hover on the .tab-content element.
A couple of options to solve this
Move the tab-content down using margin-top
.tab-content.current {
display: inherit;
margin-top: 60px;
}
Remove float: left from #tabs-container
I want to target strictly the menu when hovering. The Menu would change color and size but without affecting the li menu. How would I do that?
Note that this is suppose to be an accordion menu. The nav (sub) expands when hovering over Menu. I have spent a great deal of time but I cannot target the Menu without messing up the sub.
<nav id="menu_box">
<ul class="menu">
<li> Menu
<ul>
<li>sub</li>
<li>sub</li>
<li>sub</li>
</ul>
</li>
<li> Menu
<ul>
<li>sub</li>
<li>sub</li>
<li>sub</li>
</ul>
</li>
<li> Menu
<ul>
<li>sub</li>
<li>sub</li>
</ul>
</li>
</ul>
</nav>
here is the css:
https://jsfiddle.net/kgrxqL0k/1/
Maybe this:
.menu > li li a:hover {
color: pink;
}
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a {
margin: 0;
padding: 0;
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#cssmenu {
width: 220px;
font-family: Raleway, sans-serif;
color: #ffffff;
}
#cssmenu ul ul {
display: none;
}
#cssmenu > ul > li.active > ul {
display: block;
}
.align-right {
float: right;
}
#cssmenu > ul > li > a {
padding: 16px 22px;
cursor: pointer;
z-index: 2;
font-size: 16px;
text-decoration: none;
color: #ffffff;
-webkit-transition: color .2s ease;
-o-transition: color .2s ease;
transition: color .2s ease;
}
#cssmenu > ul > li > a:hover {
color: #d8f3f0;
}
#cssmenu ul > li.has-sub > a:after {
position: absolute;
right: 26px;
top: 19px;
z-index: 5;
display: block;
height: 10px;
width: 2px;
content: "";
-webkit-transition: all 0.1s ease-out;
-moz-transition: all 0.1s ease-out;
-ms-transition: all 0.1s ease-out;
-o-transition: all 0.1s ease-out;
transition: all 0.1s ease-out;
}
#cssmenu ul > li.has-sub > a:before {
position: absolute;
right: 22px;
top: 23px;
display: block;
width: 10px;
height: 2px;
content: "";
-webkit-transition: all 0.1s ease-out;
-moz-transition: all 0.1s ease-out;
-ms-transition: all 0.1s ease-out;
-o-transition: all 0.1s ease-out;
transition: all 0.1s ease-out;
}
#cssmenu ul > li.has-sub.open > a:after,
#cssmenu ul > li.has-sub.open > a:before {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
#cssmenu ul ul li a {
padding: 14px 22px;
cursor: pointer;
z-index: 2;
font-size: 14px;
text-decoration: none;
color: #dddddd;
-webkit-transition: color .2s ease;
-o-transition: color .2s ease;
transition: color .2s ease;
}
#menu ul li:hover a {
color: pink;
}
#cssmenu ul ul ul li a {
padding-left: 32px;
}
#cssmenu ul ul li a:hover {
color: #ffffff;
}
#cssmenu ul ul > li.has-sub > a:after {
top: 16px;
right: 26px;
}
#cssmenu ul ul > li.has-sub > a:before {
top: 20px;
}
.menu {
margin: 0 auto;
padding: 0;
width: 350px;
}
.menu li {
list-style: none;
}
.menu li a {
display: table;
margin-top: 1px;
padding: 14px 10px;
width: 100%;
text-decoration: none;
text-align: left;
vertical-align: middle;
color: #ccc;
font-weight: 600;
font-size: 18px;
overflow: hidden;
-webkit-transition-property: background;
-webkit-transition-duration: 0.4s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.4s;
transition-timing-function: ease-out;
}
.menu > li:first-child a {
margin-top: 0;
}
.menu >li a:hover a:first-child {
font-size: 25px;
color: #547b16;
}
.menu li a:hover {
] -webkit-transition-property: background;
-webkit-transition-duration: 0.2s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.2s;
transition-timing-function: ease-out;
}
.menu li ul {
margin: 0;
padding: 0;
}
.menu li li a {
display: block;
margin-top: 0;
padding: 0 10px;
height: 0;
color: #1F3D39;
-webkit-transition-property: all;
-webkit-transition-duration: 0.5s;
-webkit-transition-timing-function: ease-out;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: ease-out;
}
.menu > li:hover li a {
display: table;
margin-top: 1px;
padding: 10px;
width: 100%;
height: 1em;
-webkit-transition-property: all;
-webkit-transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-property: all;
transition-duration: 0.3s;
transition-timing-function: ease-out;
}
.menu > li:hover li a:hover {
-webkit-transition-property: background;
-webkit-transition-duration: 0.2s;
-webkit-transition-timing-function: ease-out;
transition-property: background;
transition-duration: 0.2s;
transition-timing-function: ease-out;
}
.menu > li li a:hover {
color: pink;
}
<nav id="menu_box">
<ul class="menu">
<li> Menu
<ul>
<li>sub
</li>
<li>sub
</li>
<li>sub
</li>
</ul>
</li>
<li> Menu
<ul>
<li>sub
</li>
<li>sub
</li>
<li>sub
</li>
</ul>
</li>
<li> Menu
<ul>
<li>sub
</li>
<li>sub
</li>
</ul>
</li>
</ul>
</nav>
i'm fairly new to html and I need to create a basic website for my college assignment but I want to go a step up from what everyone is doing and rather then creating a basic navigation menu I decided to create something more interesting and fun, a drop down navigation menu with changing colors and transition effects (HERE IS THE ISSUE) - BUT when i hover over menu the lists do not appear.. here is my html/css codes for that section:
<ul id="navmenu">
<li>Menu</li>
<ul>
<li>Education</li>
<li>Hobbies</li>
<li>Interests</li>
</ul>
</ul>
ul
{
list-style-type: none;
}
#navmenu a {
text-decleration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: ;
border: 1px solid #CCC;
border-radius: 5px;
font-family: Magneto;
font-size: 20px;
color: ffffff;
transition: ease-in all 400ms;
-moz-transition: ease-in all 300ms;
-webkit-transition: ease-in all 300ms;
-o-transition: ease-in all 300ms;
}
#navmenu ul {
display: none;
}
#navmenu li:hover ul {
display: block;
}
#navmenu li:hover > a {
background-color: #535ffb;
transition: ease-in all 300ms;
-moz-transition: ease-in all 300ms;
-webkit-transition: ease-in all 300ms;
-o-transition: ease-in all 300ms;
position: relative;
left: 10px;
}
#navmenu a {
margin-top: 3px;
}
Probably because your second ul is not nested under the menu item. Try this:
ul {
list-style-type: none;
}
#navmenu a {
text-decleration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: ;
border: 1px solid #CCC;
border-radius: 5px;
font-family: Magneto;
font-size: 20px;
color: ffffff;
transition: ease-in all 400ms;
-moz-transition: ease-in all 300ms;
-webkit-transition: ease-in all 300ms;
-o-transition: ease-in all 300ms;
}
#navmenu ul {
display: none;
}
#navmenu li:hover ul {
display: block;
}
#navmenu li:hover > a {
background-color: #535ffb;
transition: ease-in all 300ms;
-moz-transition: ease-in all 300ms;
-webkit-transition: ease-in all 300ms;
-o-transition: ease-in all 300ms;
position: relative;
left: 10px;
}
#navmenu a {
margin-top: 3px;
}
<ul id="navmenu">
<li>
Menu
<ul>
<li>Education
</li>
<li>Hobbies
</li>
<li>Interests
</li>
</ul>
</li>
</ul>
In my code, I have a drop-down menu. However, the grey box extends to the end of the screen. How can I limit the grey box?
Code from here: http://cssdeck.com/labs/7rsznauv
HTML:
<nav>
<ul class="cf">
<li><a class="dropdown" href="#">Menu Item 2</a>
<ul>
<li>Sub-menu Item 1</li>
<li>Sub-menu Item 2</li>
<li>Sub-menu Item 3</li>
</ul>
</li>
</ul>
</nav>
CSS:
nav ul {
-webkit-font-smoothing:antialiased;
text-shadow:0 1px 0 #FFF;
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
nav li {
float: left;
margin: 0;
padding: 0;
position: relative;
min-width: 25%;
}
nav a {
background: #ddd;
color: #444;
display: block;
font: bold 16px/50px sans-serif;
padding: 0 25px;
text-align: center;
text-decoration: none;
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
nav .dropdown:after {
content: ' ▶';
}
nav .dropdown:hover:after{
content:'\25bc'
}
nav li:hover a {
background: #ccc;
}
nav li ul {
float: left;
left: 0;
opacity: 0;
position: absolute;
top: 35px;
visibility: hidden;
z-index: 1;
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
nav li:hover ul {
opacity: 1;
top: 50px;
visibility: visible;
}
nav li ul li {
float: none;
width: 100%;
}
nav li ul a:hover {
background: #bbb;
}
/* Clearfix */
.cf:after, .cf:before {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}
nav ul {
-webkit-font-smoothing:antialiased;
text-shadow:0 1px 0 #FFF;
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 90%; // I made it 90% you can make it even narrower
}
Just reduce the width by reducing the percentage or like width: 180px;
just change the width in hnav ul{....}, actually is 100%, change it to 50% or whatever you want