I am trying to create a toggle menu, i have a menu that expands when i click on it and closes when i click on it again.
HTML code for it is
<label for="menu-toggle"><img src="icons/navicon.png"></label>
<input type="checkbox" id="menu-toggle"/>
<ul id="menu">
<li>First link</li>
<li>Second link</li>
<li>Third link</li>
</ul>
css is
<style>
label
{
cursor: pointer;
}
#menu-toggle
{
display: none; /* hide the checkbox */
}
#menu
{
display: none;
}
#menu-toggle:checked + #menu
{
display: block;
}
</style>
the toggle looks like this
when i click on the 3 lines image it looks like this
I want that when i click on the 3 lines image, the menu should get displayed in a vertical list, within a block with white background . something like this
can anyone tell me how i can do so
the images are not visible here. but what you can do is css for background color and width tag for a block.
something like
#menu
{
display: none;
width: 30%;
background-color:green;
Hey I just did some adjustments to your given code, and hope the output is close to the one in your given picture.
<style>
label
{
cursor: pointer;
}
a {
text-decoration: none;
color: #9A9494;
}
li {
border-bottom:#ccc solid 1px; /* This is the divider line in between the list items */
}
#menu-toggle
{
display: none; /* hide the checkbox */
}
#menu
{
display: none;
}
#menu-toggle:checked + #menu
{
display: block;
}
.xyz{
padding-bottom: 10px;
font-size: 30px;
background-color: #fff;
}
.container{
background-color: #ccc;
width:15%;
border-radius: 10px;
}
</style>
<!DOCTYPE html>
<html>
<head>
<title>DropDown</title>
</head>
<body>
<label for="menu-toggle"><img src="icons/navicon.png"></label>
<div class="container">
<input type="checkbox" id="menu-toggle"/>
<ul id="menu" style="list-style: none; padding:15px; border-radius: 15px;" >
<li class="xyz" ><a href="#" >First link</a></li>
<li class="xyz">Second link</li>
<li class="xyz">Third link</li>
</ul>
</div>
</body>
</html>
I combined stylesheet with the html... Nice helping you.. :)
Update following CSS:
#menu {
display: none;
list-style-type: none;
border-radius: 5px;
background-color: lightgray;
padding-left: 5px;
}
#menu-toggle:checked + #menu {
display: block;
}
li a {
color: #000;
text-decoration: none;
}
#menu li {
padding: 10px;
border-bottom: 1px solid #000;
margin-left: 5px;
margin-right: 5px;
}
DEMO
Related
I have to create a drop down with two columns just like the image It should be like when i click on the drop down menu it should display like this so far i am not successful!. This is the sample code i am working with.. if i execute this code it is no where what i am expecting and also i am new to coding.
$(document).ready(function() {
// NAV TOGGLE ONCLICK WITH SLIDE
$(".clickSlide ul").hide();
$(".clickSlide").click(function() {
$(this).children("ul").stop(true, true).slideToggle("fast"),
$(this).toggleClass("dropdown-active");
});
// NAV TOGGLE ONCLICK WITH FADE
$(".clickFade ul").hide();
$(".clickFade").click(function() {
$(this).children("ul").stop(true, true).fadeToggle("fast"),
$(this).toggleClass("dropdown-active");
});
// NAV TOGGLE ONHOVER WITH SLIDE
$(".hoverSlide ul").hide();
$(".hoverSlide").hover(function() {
$(this).children("ul").stop(true, true).slideToggle("fast"),
$(this).toggleClass("dropdown-active");
});
// NAV TOGGLE ONHOVER WITH FADE
$(".hoverFade ul").hide();
$(".hoverFade").hover(function() {
$(this).children("ul").stop(true, true).fadeToggle("fast"),
$(this).toggleClass("dropdown-active");
});
});
/**/
#navbar {
width: 100%;
padding: 10 10 10 10;
}
#dropdown1 {
width: 960px;
margin: 0 auto;
padding: 0;
height: 1em;
font-weight: bold;
}
#dropdown1 li {
list-style: none;
float: left;
}
#dropdown1 li a {
padding: 0px 0px 0px 5px;
text-decoration: none;
}
#dropdown1 li ul {
display: none;
background-color: #fff;
}
#dropdown1 li:hover ul,
#navbar li.hover ul {
display: block;
position: absolute;
margin: 0;
padding: 0;
}
#dropdown1 li:hover li,
#navbar li.hover li {
float: left;
}
#dropdown1 li:hover li a,
#navbar li.hover li a {
background-color: #fff;
color: #000;
}
.topnav a {
color: #000;
text-decoration: none;
}
.topnav a:hover {
border-bottom: 1px solid gold;
}
.column {
display: inline-block;
list-style-type: none;
float: left;
margin: 5px 0 0 0;
padding: 0 5px 0 0;
width: 500px !important;
}
.column li {
float: left;
display: inline;
}
.column a {
color: #999;
text-decoration: none;
font-size: .7em;
}
.column a:hover {
border-bottom: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="navbar">
<ul id=dropdown1>
<li class="topnav">
<div class="column">
Services <span>▼</span>
<ul>
<li>Web hosting</li>
<li>Web builder</li>
<li>Themes</li>
</ul>
</div>
<div class="column">
<ul>
<li>Web hosting</li>
<li>Web builder</li>
<li>Themes</li>
</ul>
</div>
</ul>
</div>
if i understood properly,
This jsfiddle maybe helpful
looks like u use two div but have same class name, then with your css only set "column" .
so the two div will display in same position, that's why u have two div but only display one.
<div class="navbar">
<ul id=dropdown1>
<li class="topnav">
<div class="column">
Services <span>▼</span>
<ul>
<li>Web hosting</li>
<li>Web builder</li>
<li>Themes</li>
</ul>
</div>
<div class="column2">
<ul>
<li>Web hosting</li>
<li>Web builder</li>
<li>Themes</li>
</ul>
</div>
</ul>
then you should set column2's css
UPDATE:
this fiddle
look this fiddle above
i change the li and set
.column a:after{
content:"\a";
white-space: pre;
}
\a means line break, character U+000A, and white-space: pre tells browsers to treat it as a line break in rendering.
found answer here Line break (like <br>) using only css
I have developed drop-down as per your image, without using any plugin.
I am using:
HTML
CSS
Explaination:
I am using table tag to design multiple columns.
<table>
<tr>
<td></td>// for left-col links
<td></td>// for right-col links
</tr>
</table>
Then inside <td> tag I am using <ul><li></li></ul>tag. Inside <li> you can have multiple links.
Similarly I did for right-side column.
Full Code
CSS code
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
.link-format {
list-style-type:none;
width:100px
}
</style>
HTML code
<body>
<h2>Dropdown Menu with multiple columns</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<table>
<tr>
<td>
<ul class="link-format" style="border-right:1px gray dashed">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</td>
<td>
<ul class="link-format">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
Working JsFiddle: https://fiddle.jshell.net/mayankBisht/cvsrqn3r/1/
Hope this helps.
Please reach out to me for more information/help.
Thanks.
I am trying to make a drop down menu, but I want to show and hide drop down just by click on the button. Currently drop down is hiding even when I click on one of the children. How to fix it?
Fiddle:
<ul id="nav">
<li class="parent"><button>Childrens</button>
<ul class="sub-nav">
<li>Johnny</li>
<li>Julie</li>
<li>Jamie</li>
</ul>
</li>
</ul>
Try it:
http://jsfiddle.net/zyfv6nd0/
$(document).ready(function() {
$('button').click(function() {
$('.sub-nav').toggleClass('visible');
});
});
Change your selector from $('.parent') to $('.parent button'):
$(document).ready(function() {
$('.parent button').click(function() {
$(this).next('.sub-nav').toggleClass('visible');
});
});
#nav {
list-style: none;
padding: 0;
margin: 0;
}
#nav > li {
display: inline-block;
vertical-align: top;
position: relative;
}
#nav ul.sub-nav {
position: absolute;
min-width: 200px;
display: none;
top: 100%;
left: 0;
}
#nav ul.visible {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="nav">
<li class="parent">
<button>Childrens</button>
<ul class="sub-nav">
<li>Johnny</li>
<li>Julie</li>
<li>Jamie</li>
</ul>
</li>
<li class="parent">
<button>Childrens</button>
<ul class="sub-nav">
<li>Johnny</li>
<li>Julie</li>
<li>Jamie</li>
</ul>
</li>
</ul>
Drop down menu only by click on button example classList.toggle pure js
/* Cross-browser solution for classList.toggle() */
function myFunction2() {
var x = document.getElementById("myDropdown");
if (x.classList) {
x.classList.toggle("show");
} else {
var classes = x.className.split(" ");
var i = classes.indexOf("show");
if (i >= 0)
classes.splice(i, 1);
else
classes.push("show");
x.className = classes.join(" ");
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block}
<p>The classList property is not supported in Internet Explorer 9 and earlier.</p>
<div class="dropdown">
<button id="myBtn" class="dropbtn" onclick="myFunction2()">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
</div>
I try to show a dropdown menu after the user hovers over the link with id galerie. I solved it like this:
$("#gallerie").mouseover
(
function()
{
$("#gallerie_drop").css("display","block");
}
);
$("#gallerie_drop").mouseleave
(
function()
{
$("#gallerie_drop").css("display","none");
}
);
#header {
text-align: right;
padding:20px;
}
#header a {
padding: 20px;
}
#gallerie_drop {
display:none;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: block;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
Galerie
Videos
Photos
Foo
Bar
</div>
<div class="dropdown-content" id="gallerie_drop">
Link 1
Link 2
Link 3
</div>
But how can i let the box appear directly under the link?
JSFIDDLE: https://jsfiddle.net/c544es76/2/
If you are creating Menu, please use UL, LI.
Here is demo, with hoverable item. All without JS:
http://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_dropdown_navbar
Need changes.
You HTML Should be like this,
<div class="yourclass">
Galerie
<div class="dropdown-content" id="gallerie_drop">
Link 1
Link 2
Link 3
</div>
</div>
And CSS,
.yourclass{
position: relative;
}
#gallerie_drop{
position: absolute;
top: 10px;
left: 10px;
}
And the script is correct to show/hide the element.
Add position:relative to .gallery_drop
#gallerie_drop {
display:none;
position:relative;
}
and change your mouseover callback to
$("#galerie").mouseover
(
function()
{
$(this).append($('#gallery_drop'));
$("#gallerie_drop").css("display","block");
}
);
This will append the div to your hover link. You have size it appropriately.
Use Like This
Javascript is not needed
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
float:right;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<ul>
<li class="dropdown">
Galerie
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
<li><a class="active" href="#Videos">Videos</a></li>
<li> <a href="#photos" >Photos</a></li>
<li> <a href="#Foo" > Foo</a></li>
<li> <a href="#Bar" > Bar </a></li>
</li>
</ul>
</body>
</html>
Basically, I have created a navigation bar that has an underline when hovered and also when it is active. I also have a line spanning the page. When the cursor is hovered over the list, the 4px line appears under the word but it also pushed the line spanning the page down by 4 pixels. Can someone help please.
Here's the HTML:
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link rel="stylesheet" href="new1.css">
</head>
<body>
<nav id="ddmenu">
<div class="menu-icon"></div>
<ul>
<li class='top-heading'><a href='#'><span>New in</span></a>
</li>
<li class='top-heading'><a href='#'><span>Homeware</span></a>
</li>
<li class='top-heading'><a href='#'><span>Decorating</span></a>
</li>
<li class='top-heading'><a href='#'><span>DIY</span></a>
</li>
<li class='top-heading'><a href='#'><span>Furniture</span></a>
</li>
<li class='top-heading'><a href='#'><span>Bathroom</span></a>
</li>
<li class='top-heading'><a href='#'><span>Garden</span></a>
</li>
<li class='top-heading'><a href='#'><span>Offers</span></a>
</li>
</ul>
</nav>
</body>
</html>
Here's the CSS
#ddmenu {
zoom: 1;
width: 100%;
background: #FFF;
padding: 0px 0;
}
#ddmenu:before {
content: '';
display: block;
}
#ddmenu:after {
content: '';
display: block;
clear: both;
}
#ddmenu ul {
list-style-type: none;
position: relative;
display: block;
font-size: 12px;
font-family: Verdana, Helvetica, Arial, sans-serif;
margin: 0px;
padding-top: 4px;
border-bottom: 1px solid #EAEBEB;
border-top: 1px solid #EAEBEB;
zoom: 1;
}
#ddmenu ul:before {
content: '';
display: block;
}
#ddmenu ul:after {
content: '';
display: block;
clear: both;
}
#ddmenu li {
display: block;
float: left;
margin: 0;
padding: 0;
}
#ddmenu li a {
float: left;
color: #000;
text-decoration: none;
height: 20px;
padding: 1px 50px 0;
font-weight: normal;
}
#ddmenu li:hover, #ddmenu .active {
text-decoration: none;
border-bottom: 4px solid #EAEBEB;
colour: #000
}
#ddmenu .active a {
font-weight: 700;
}
When you add a border you're increasing the height of the element so naturally it affects items below it.
Without a set height the optimal solution is to have the border already there but transparent and just change the color on hover.
#ddmenu li {
display: block;
float: left;
margin: 0;
padding: 0;
border-bottom:4px solid transparent;
}
JSfiddle Demo
Note: Just a suggestion but you have a lot of padding on the links there. As you can see it in the doesn't look quite so nice at smaller viewports...you might want to look into that.
Here's my code:
HTML:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Responsive Menu</title>
<link rel="stylesheet" href="css/colors.css"/>
<link rel="stylesheet" href="css/font-awesome.css"/>
</head>
<body>
<header id="topbar">
<p>Some title</p>
<nav class="menu">
<ul>
<li>Home</li>
<li>Work</li>
<li>Contact Me</li>
</ul>
</nav>
<div class="nav-trigger">
<span><i class="fa fa-bars fa-2x"></i></span>
</div>
<nav class="mobile-menu-enabler">
<span></span>
</nav>
</header>
<nav class="mobile-menu">
<ul>
<li>Home</li>
<li>Work</li>
<li>Contact Me</li>
</ul>
</nav>
<!-- Begin main sample site content -->
<div id="wrapper">
<p>Lorem ipsum dolor set amet...</p>
</div>
<script src="js/jquery-2.1.3.js"></script>
<script src="js/script.js"></script>
</body>
</html>
CSS:
body {
font-family: "Ludica Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
}
p {
margin: 0;
padding: 0;
display: inline-block;
}
header {
margin: 0;
padding: 0;
display: inline-block;
position: absolute;
top: 0;
left: 0;
background-color: azure;
border: 1px solid black;
width: 100%;
}
/* Begin Menu Code */
.menu {
display: inline-block;
float: right;
}
.menu ul {
list-style: none;
margin: 5px 0;
}
.menu ul:last-child {
margin-right: 10px;
}
.menu ul li {
display: inline-block;
margin: 0 2px;
padding: 0;
border: 1px solid black;
}
.menu ul li a {
text-decoration: none;
color: black;
padding: 3px;
}
.menu ul li>:hover {
background-color:black;
color: white;
}
/* End Menu Code */
/* Mobile Menu Begin */
.mobile-menu-enabler {
display: none;
background-color: aqua;
}
.mobile-menu {
display: none;
position: relative;
top: 38px;
width: 100%;
border-top: 1px solid black;
}
.mobile-menu ul {
list-style: none;
margin: 0;
padding: 0;
}
.mobile-menu ul li {
margin: 0;
padding: 0;
border: 1px solid black;
border-top: 1px solid transparent;
text-align: center;
}
.mobile-menu ul li a {
margin: 0;
padding: 0;
text-decoration: none;
color: black;
}
.mobile-menu ul li:hover {
background-color: darkkhaki;
}
/* Mobile Menu End */
/* Mobile Trigger Button */
.nav-trigger {
display: none;
text-transform: uppercase;
color: black;
background-color: transparent;
margin-right: 10px;
padding: 0;
text-align: center;
}
.nav-trigger:hover {
color: red;
background-color: transparent;
}
/* End Mobile Trigger Button */
/* Dummy wrapper stuff */
#wrapper {
clear: both;
display: inline-block;
position: relative;
}
/* End Dummy wrapper stuff */
#media screen and (max-width: 900px) {
.menu {
display: none;
}
.mobile-menu-enabler {
display: block;
}
.nav-trigger {
display: inline-block;
float: right;
}
/*.mobile-menu {*/
/*display: block;*/
/*}*/
}
JavaScript:
$(document).ready(function(){ // Begin document ready function
var headerHeight = $("#topbar").height();
$(".nav-trigger").click(function() {
$(".mobile-menu").slideToggle(400)
});
$(".mobile-menu").css("top", headerHeight);
$("#wrapper").css("top", headerHeight);
}); // End document ready function
When the page loads I have a top header bar which contains dummy links. When the browser width is shrunk below the breakpoint at 900px the responsive menu kicks in, at this point upon clicking on the "3 bars icon" the dropdown menu appears as expected.
However, if I were to resize the screen back to full size without first closing the drop down menu, it continues to stay even after the browser is fully expanded.
How can I make the drop-down menu to automatically "untoggle" at the point of growing wider than the breakpoint of 900px?
Thanks
EDIT: Screenshots:
Here's how I'd like it to behave:
callmenick.com/lab-demos/10-simple-responsive-navigation
When the mobile navigation is fully expanded, it automatically slides up once the browser window is expanded
Since you're using Javascript to toggle the display when you click that menu bar icon, it pretty much nullifies the 'display' css for .mobile-menu to some extent (since it is changing the style after the css has already loaded). So you'll need to toggle the UL display value (child), and not the .mobile-menu (parent). That way the responsive .mobile-menu css stays in tact... I believe this is what that example site you mentioned does.
<nav class="mobile-menu">
<ul class="mobile-menu-items">
<li>Home</li>
<li>Work</li>
<li>Contact Me</li>
</ul>
</nav>
keep css for .mobile-menu as display: none on large screens, and display:block for smaller. Then for new class ".mobile-menu-items" you can initially set that to display: none, and toggle that with your jQuery:
$('.nav-trigger').click(function(){
$('.mobile-menu-items').slideToggle();
});
Alternatively you could toggle the visibility instead of the display.
$('.nav-trigger').click(function(){
if ( $('.mobile-menu').css('visibility') == 'hidden' )
$('.mobile-menu').css('visibility','visible');
else
$('.mobile-menu').css('visibility','hidden');
});
If you want to actually retract the mobile menu (and not just hide it in CSS) when the window gets too wide, try this :
var $window = $(window), // caching the objects for performance
$mobileMenu = $(".mobile-menu");
$window.resize(function(){
if( $window.width() > 900) $mobileMenu.slideUp();
})
updated.extremely crude example. resize
ul{list-style-type:none;}
#media screen and (min-width: 500px) {
#big li{display:inline-block;width:100px;height:32px;border:1px solid red;}
#small{display:none;}
}
#media screen and (max-width: 500px) {
#big{display:none;}
#small{display:block;}
.smaller{display:none;}
#small li:hover~ul{display:block}
.smaller:hover{display:block;}
}
<ul id="big">
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
<ul id="small">
<li>home1</li>
<ul class="smaller">
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
</ul>
<li>home2</li><ul class="smaller">
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
</ul>
</ul>