I made a menu that appears when clicking a hamburger button with CSS and a checkbox. It works nicely so far. But now I want it to disappear when clicking elsewhere on the page, not only when clicking on the hamburger button again. How do I do that, please?
HTML:
<div class="navigatie">
<input class="nav-toggle" id="nav-toggle" type="checkbox">
<nav>
<ul>
<li>Home</li>
<li>Werkwijze</li>
<li>Over ons</li>
<li>Faq</li>
<li>Accreditatie</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span><i class="fas fa-bars"></i></span>
</label>
</div>
CSS:
.nav-toggle {
display: none;
}
nav {
position: absolute;
bottom: 100%;
left: 30%;
background-color: #f1f1f1;
width: 40%;
transform: scale(1,0);
transform-origin: bottom;
transition: transform 400ms ease-in-out;
}
nav a {
color: #65A624;
font-size: 1rem;
opacity: 0;
transition: opacity 150ms ease-in;
display: block;
}
nav a:hover:not(.active) {
background-color: #555;
color: white;
}
nav a.active {
background-color: #65A624;
color: white;
}
.nav-toggle:checked ~ nav {
transform: scale(1,1);
}
.nav-toggle:checked ~ nav a {
opacity: 1;
transition: opacity 250ms ease-in-out 250ms;
}
Greetings,
Sandra.
$(document).ready(function () {
$(document).click(function (event) {
var clickover = $(event.target);
var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
$("button.navbar-toggle").click();
}
});
});
Check out this jsfiddle sample.
http://jsfiddle.net/52VtD/5718/
CSS Solution
If you want it purely CSS you can do the example below, I've edited a few things because your code missed some CSS and HTML to make it reproducible correctly. But the point should be clear. ;)
A overlay appears after opening, you can close the menu by clicking anywhere in the overlay (darker background)
Advice: use JavaScript
I would recommend however to make it easier and use JavaScript if that is a option.
.nav-toggle-label {
width: 3rem;
height: 3rem;
margin: 0;
display: block;
background-color: #0F0;
}
.nav-toggle {
width: 0.1rem;
height: 0.1rem;
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
nav {
width: 40%;
position: absolute;
top: 0%;
left: 30%;
z-index: 110;
background-color: #f1f1f1;
transform: scale(1, 0);
transform-origin: bottom;
transition: transform 400ms ease-in-out;
}
nav a {
color: #65A624;
font-size: 1rem;
opacity: 0;
transition: opacity 150ms ease-in;
display: block;
}
nav a:hover:not(.active) {
background-color: #555;
color: white;
}
nav a.active {
background-color: #65A624;
color: white;
}
.nav-toggle:checked~.navigatie nav {
transform: scale(1, 1);
}
.nav-toggle:checked ~ .navigatie a {
opacity: 1;
transition: opacity 250ms ease-in-out 250ms;
}
.nav-toggle-close {
width: 100%;
height: 100%;
position: absolute;
top: 0%;
left: 0%;
background-color: rgba(0, 0, 0, 0.3);
display: none;
z-index: 100;
}
.nav-toggle:checked ~ .nav-toggle-close {
display: block;
}
<input class="nav-toggle" id="nav-toggle" type="checkbox">
<label for="nav-toggle" class="nav-toggle-close"></label>
<div class="navigatie">
<nav>
<ul>
<li>Home</li>
<li>Werkwijze</li>
<li>Over ons</li>
<li>Faq</li>
<li>Accreditatie</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span><i class="fas fa-bars">Menu</i></span>
</label>
</div>
Related
i have hamburger menu which i took from here. https://codepen.io/erikterwan/pen/EVzeRP
it is using only css by using a checkbox to expand and collapse the menu.
so what i want is when i click outside the menu (anywhere on the page), the menu should collapse. can someone help me.
this is the html code
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Info</li>
<li>Contact</li>
<li>Show me more</li>
</ul>
</div>
</nav>
this is the css code
body
{
margin: 0;
padding: 0;
background: #232323;
color: #cdcdcd;
font-family: "Avenir Next", "Avenir", sans-serif;
}
#menuToggle
{
display: block;
position: relative;
top: 50px;
left: 50px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
#menuToggle a
{
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
#menuToggle a:hover
{
color: tomato;
}
#menuToggle input
{
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}
#menuToggle span:first-child
{
transform-origin: 0% 0%;
}
#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0, -1px);
}
#menu
{
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}
#menu li
{
padding: 10px 0;
font-size: 22px;
}
#menuToggle input:checked ~ ul
{
transform: none;
}
You could use a label that expands to the whole width and height, and positioned under the nav: <label for="checkbox-id"></label>. You would need to add an id to your checkbox. Make sure to set the label background to transparent.
EDIT: You would show the label only when #checkbox-id is checked. This can be achieved using only CSS. You might need to restructure your HTML a little bit. TBH, your implementation can be much simpler than in the codepen you're drawing inspiration from. Please let me know in a comment if you want more details.
What have i done is i detected a click outside an element and setting the checkbox to false.
$(document).ready(function() {
$(document).on("click", function(event) {
if ($(event.target).closest("#checkbox").length === 0) {
$("#checkbox").prop("checked", false);
}
});
});
I am trying to create a simple, pure CSS push menu. The menu opens and closes when clicking on the Settings icon. This works already. However, I wish to move the icon inside the grey div and have it activate the menu when clicked. At the moment, the settings click only works when the icon is in the position it currently sits. If I move it inside the div, nothing happens.
Also, I would like to close the menu by clicking on the Settings text or left chevron icon inside the menu. I tried creating a second menu checkbox and associated styles, like so;
#menu-toggle-2:checked + .menu-icon {
right: 30%;
}
But doing this only moved the Settings div INSIDE the menu; it did not close the menu itself.
Here is the html code for the page and menu;
<head>
<style>
.content-container {
z-index: 0;
padding: 20px;
overflow: auto;
transition: all 300ms ease-in-out;
}
.slideout-sidebar {
position: fixed;
right: -28%;
z-index: 2;
width: 28%;
height: 100%;
padding: 20px;
transition: all 300ms ease-in-out;
}
.slideout-sidebar ul {
list-style: none;
}
.slideout-sidebar ul li {
cursor: pointer;
padding: 18px 0;
border-bottom: 1px solid #D1D4D7;
}
#menu-toggle {
display: none;
}
.menu-icon {
position: absolute;
top: 20px;
right: 20px;
z-index: 2;
transition: all 300ms ease-in-out;
cursor: pointer;
}
#menu-toggle:checked ~ .slideout-sidebar {
right: 0px;
}
#menu-toggle:checked + .menu-icon {
right: 30%;
}
#menu-toggle:checked ~ .content-container {
padding-right: 28%;
}
</style>
</head>
Body;
<body>
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" class="menu-icon">
<img class="icon" src="images2/ic-of-settings-gy.svg" style="width:33px;height:33px;" />
</label>
<div id="divMenu" class="slideout-sidebar">
<ul>
<li>
<img class="icon" src="images2/ic-of-chevron-left-gy.svg" />
<span style="padding-left:10px">Settings</span>
</li>
<li>
About
</li>
<li>
Contact
</li>
</ul>
</div>
<div class="content-container">
<div class="container">
<div style="width:780px;height:800px;background-color:grey;">
</div>
</div>
</div>
</body>
And here is the image of the Settings menu item, which I would like to close the menu when clicked;
Thanks in advance
Managed to figure it out, if anyone needs the info. The trick is to stick a hidden input on the page, and a second one in your desired location, with the same ID. Code below.
<style>
#menu-toggle {
display: none;
}
.menu-icon {
position: absolute;
top: 1.25rem;
right: 1.25rem;
z-index: 2;
cursor: pointer;
}
content {
-ms-overflow-style: none;
-moz-transition: all 200ms ease-in;
-webkit-transition: all 200ms ease-in;
-o-transition: all 200ms ease-in;
transition: all 200ms ease-in;
overflow-y: auto;
}
nav {
position: fixed;
right: 0;
width: 28%;
height: 100%;
margin: 0 -28% 0 0;
-moz-transition: all 200ms ease-in;
-webkit-transition: all 200ms ease-in;
-o-transition: all 200ms ease-in;
transition: all 200ms ease-in;
}
nav ul {
height: 100%;
list-style: none;
padding: 0;
}
nav li {
padding: 1.125rem 0 1.125rem 0;
border-bottom: 0.0625rem solid rgba(209, 212, 215, 1);
}
label {
display: block;
cursor: pointer;
-moz-transition: all 200ms ease-in;
-webkit-transition: all 200ms ease-in;
-o-transition: all 200ms ease-in;
transition: all 200ms ease-in;
z-index: 500;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked ~ nav {
margin: 0;
}
input[type="checkbox"]:checked ~ content {
-webkit-transform: translate3d(-28%, 0, 0);
-moz-transform: translate3d(-28%, 0, 0);
-o-transform: translate3d(-28%, 0, 0);
transform: translate3d(-28%, 0, 0);
}
</style>
<input type="checkbox" id="menu-toggle" />
<nav>
<ul>
<li class="font-weight-bold settings-list-item">
<label for="menu-toggle" class="list-spacer">
<img src="images/chevron-left.png" />
<span><b class="ng-tns-c1354-3 font-15 padding-left-20">Settings</b></span>
</label>
</li>
<li class="font-weight-bold settings-list-item">
Home
</li>
<li class="list-spacer">
About
</li>
<li class="list-spacer">
Contact
</li>
</ul>
</nav>
<content>
<div style="width:780px;height:800px;background-color:grey;">
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" class="nav-icon margin-top-35">
<img class="settings-icon" src="images/settings.png" />
</label>
</div>
</content>
I would like to know how to animate the link when I hover on it. I want the top border to move down and the bottom border to move up. The borders should fade away, when the animation ends. When I hover on the link, the borders should appear and show the animation.
HTML:
<ul>
<li class="active">Home</li>
<li>About Me</li>
<li>My Passion</li>
</ul>
CSS:
ul li a {
border-bottom: 3px transparent solid;
border-top: 3px transparent solid;
}
ul li a::before {
position: absolute;
left: 0;
top: 0;
visibility: hidden;
-moz-transition: 0.3s;
-o-transition: 0.3s;
-webkit-transition: 0.3s;
transition: 0.3s;
}
ul li a::after {
position: absolute;
left: 0;
top: 100%;
visibility: hidden;
-moz-transition: 0.3s;
-o-transition: 0.3s;
-webkit-transition: 0.3s;
transition: 0.3s;
}
ul li a:hover {
color: blue;
}
ul li a:hover::before {
visibility: visible;
top: 100%;
background: blue;
}
ul li a:hover::after {
visibility: visible;
top: 0;
background: blue;
}
Expected result:
I've re-created the example in your GIF.
For the text hover effect, make sure to use transition to ease the effect, e.g.:
a {
color: white;
text-decoration: none;
transition: 800ms ease color;
display: block;
}
a:hover {
color: gold;
}
I'm displaying the a tag as block to make sure it covers the whole li elements when hovered.
When using CSS pseudo elements always set a content property (content: '' is you want to style it):
li::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 5px;
background: gold;
opacity: 0;
}
Instead of using a border, I'm animating the pseudo before en after elements on hover. You can do this by using CSS keyframes.
li:hover::before {
opacity: 1;
animation: flyDown 300ms ease forwards;
}
li:hover::after {
opacity: 1;
animation: flyUp 300ms ease forwards;
}
#keyframes flyDown {
from {
transform: translateY(0px)
}
to {
transform: translateY(90px)
}
}
#keyframes flyUp {
from {
transform: translateY(00px)
}
to {
transform: translateY(-90px)
}
}
Please find jsfiddle here: https://jsfiddle.net/sanderdebr/fn4pgwv6/30/
---html---
<span class="a-border" onclick="menuclick(this)">ABOUT US</span>
<span class="a-border" onclick="menuclick(this)">CREATERS</span>
<span class="a-border" onclick="menuclick(this)">NEWS</span>
<span class="a-border" onclick="menuclick(this)">CONTACT</span>
---css---
a {
text-decoration: none;
}
a:hover {
cursor: pointer;
}
.a-border {
display: inline-block;
position: relative;
color: white;
padding: 0.5rem 0.25rem;
margin: 0 1.5rem;
overflow: hidden;
}
.a-border::after {
content: "";
display: block;
position: absolute;
bottom: -8px;
left: -3%;
width: 106%
border-top: 2px solid white;
transform: scale(0, 1);
transform-origin: 0% 100%;
transition: transform 0.4s;
}
a:hover .a-border::after {
transform:scale(1, 1);
}
a.focused .a-border::after {
transform: none;
}
---js---
function menuclick(underline) {
var focused = document.getElementsByClassName("focused");
var i;
for (i = 0; i < focused.length; i++) {
var under = focused[i];
if (under.classList.contains('focused')) {
under.classList.remove('focused');
}
}
if (!underline.parentElement.classList.contains('focused')) {
underline.parentElement.classList.add('focused');
}
}
I am working on a menu. when i hover over hamburger icon menu animation works good,but when mouse leaves it menu disappear suddenly. I want that it disappears in reverse animation.How can i do it?Please help.
https://codepen.io/sen-tanmoy/pen/NWPvLLK
HTML code
<div class="wrapper">
<nav class="navbar">
<ul>
<li class="no">
<div class="hamburger-menu-icon">
<div class="line line1"></div>
<div class="line line2"></div>
<div class="line line3"></div>
</div>
<ul class="dropdown" aria-label="submenu">
<li>Home</li>
<li>Projects</li>
<li>News/Awards</li>
<li>Upcoming Projects</li>
<li>About Us</li>
<li>Our Team</li>
<li>Contact Us</li>
</ul>
</li>
</ul>
</nav>
</div>
It looks like it was disappearing abruptly because you were changing the display between block and none on hover. I removed that and added a separate closing animation that plays when you're not hovering.
The problem with keeping it set to display: block; is that you can trigger the animation by hovering over the area where the menu will be, even when it's closed.
So I added another animation to scale the .dropdown div after the menu items are closed.
At this point, it looked ok except the menu would start out open and then collapse after the page loads, so I added another animation to keep it hidden until the menu items are finished collapsing.
You may want to mess with the timings a bit, but this should get you pretty close.
* {
margin: 0;
padding: 0;
}
.navbar {
width: 330px;
height: 95vh;
margin-top: 5vh;
position: fixed;
display: flex;
transition: opacity 1.5s ease;
}
.hamburger-menu-icon {
width: 55px;
height: 50px;
padding: 18px;
display: flex;
flex-direction: column;
justify-content: space-around;
cursor: pointer;
background-color: silver;
border-radius: 50%;
}
.line {
width: 100%;
height: 5px;
background-color: rgb(255, 255, 255);
transition: all 0.8s;
display: flex;
justify-content: center;
align-items: center;
}
nav a {
text-decoration: none;
font-size: 24px;
}
nav ul {
list-style: none;
margin: 0;
padding-left: 0;
margin-right: auto;
}
nav li {
color: black;
/* background: #262626; */
display: block;
padding: 10px 25px;
position: relative;
text-decoration: none;
transition-duration: 500ms;
}
.no {
background: none;
}
.dropdown {
padding-top: 20px;
width: 250px;
transform-origin: top left;
animation: lateClose 2000ms both ease;
/* transition-duration: 500ms; */
}
.same {
position: relative;
padding-bottom: 10px;
animation: initialArrival 1500ms both ease;
}
.same::after {
content: "";
width: 100%;
height: 2px;
position: absolute;
/* left: 10px; */
bottom: 0;
right: 0;
background-color: #2A324B;
transform: scaleX(0);
transition: transform 0.5s;
}
.same:hover::after {
transform: scaleX(1);
transform-origin: left;
}
nav li a {
color: inherit;
}
.no:hover .dropdown {
animation: animateOpen 1ms both ease;
}
.dropdown:hover,
.dropdown:focus {
/* background-color: #cc2a36; */
color: #fff;
cursor: pointer;
}
nav ul li ul {
position: absolute;
transition: background-color 0.5s ease-in;
margin-top: 10px;
left: 0;
display: block;
}
nav ul li:hover >ul,
nav ul li:focus >ul,
.dropdown:hover,
.dropdown:focus {
opacity: 1;
/* display: block; */
/* transition: all 1s ease; */
}
nav ul li ul li {
clear: both;
width: 100%;
animation: animateClosed 900ms both ease;
}
nav:hover ul li ul li {
clear: both;
width: 100%;
animation: animateOpen 900ms both ease;
}
nav ul li ul li:nth-of-type(1) {
transform-origin: top left;
transform: skewX(45deg) scaleY(0);
}
nav ul li ul li:nth-of-type(2) {
transform-origin: top right;
transform: skewX(-45deg) scaleY(0);
animation-delay: 150ms;
}
nav ul li ul li:nth-of-type(3) {
transform-origin: top right;
transform: skewX(45deg) scaleY(0);
animation-delay: 300ms;
}
nav ul li ul li:nth-of-type(4) {
transform-origin: top right;
transform: skewX(-45deg) scaleY(0);
animation-delay: 450ms;
}
nav ul li ul li:nth-of-type(5) {
transform-origin: top right;
transform: skewX(45deg) scaleY(0);
animation-delay: 600ms;
}
nav ul li ul li:nth-of-type(6) {
transform-origin: top right;
transform: skewX(-45deg) scaleY(0);
animation-delay: 750ms;
}
nav ul li ul li:nth-of-type(7) {
transform-origin: top right;
transform: skewX(45deg) scaleY(0);
animation-delay: 900ms;
}
#keyframes animateOpen {
0% {
transform: skewX(0deg) scaleY(0);
}
100% {
transform: skewX(0deg) scaleY(1);
}
}
#keyframes animateClosed {
0% {
transform: skewX(0deg) scaleY(1);
}
100% {
transform: skewX(0deg) scaleY(0);
}
}
#keyframes lateClose {
0% {
transform: scaleY(1);
}
99% {
transform: scaleY(1);
}
100% {
transform: scaleY(0);
}
}
#keyframes initialArrival {
0% {
opacity: 0;
}
99% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<div class="wrapper">
<nav class="navbar">
<!-- <h2>Saffron</h2> -->
<ul>
<li class="no">
<div class="hamburger-menu-icon">
<div class="line line1"></div>
<div class="line line2"></div>
<div class="line line3"></div>
</div>
<ul class="dropdown" aria-label="submenu">
<li>Home</li>
<li>Projects</li>
<li>News/Awards</li>
<li>Upcoming Projects</li>
<li>About Us</li>
<li>Our Team</li>
<li>Contact Us</li>
</ul>
</li>
</ul>
</nav>
</div>
I'm really new to HTML and CSS. I have a navbar which underlines the menu item you hover with a fade in, fade out-effect from the middle,
but how can I keep an active menu item underlined with the same style?
I'm also using Typo3 / Fluid which creates me the html code and assigns the "active" class to the active menu item.
This is how it looks like so far: https://jsfiddle.net/wr5w09r0/
css
div#top_nav{
text-align: center;
}
div#top_nav li{
display: inline;
padding: 15px;
}
div#top_nav a {
display: inline-block;
position: relative;
}
div#top_nav a:hover{
color: orange;
}
div#top_nav a:before{
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: -3px;
left: 0;
background-color: orange;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.15s ease-in-out 0s;
transition: all 0.15s ease-in-out 0s;
}
div#top_nav a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
.active {
color: orange;
}
Hope this will help you. I have added this simple element.
div#top_nav a:hover:before , div#top_nav a.active:before
a {
text-decoration: none;
}
div#top_nav{
text-align: center;
}
div#top_nav li{
display: inline;
padding: 15px;
}
div#top_nav a {
display: inline-block;
position: relative;
}
div#top_nav a:hover{
color: orange;
}
div#top_nav a:before{
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: -3px;
left: 0;
background-color: orange;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.15s ease-in-out 0s;
transition: all 0.15s ease-in-out 0s;
}
div#top_nav a:hover:before , div#top_nav a.active:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
.active {
color: orange;
}
<div id="top_nav">
<ul>
<li class="mainMenu-itemLevel1">
seite1</li>
<li class="mainMenu-itemLevel1">
seite2
</li><li class="mainMenu-itemLevel1">
seite3</li>
<li class="mainMenu-itemLevel1">
seite4</li>
<li class="mainMenu-itemLevel1">
Seite5 lang Hover</li>
</ul>
</div>
Hope this will help you.
You need to use javascript to do this.
Your javascript would add a class, like active to the relevant menu item, and you can then use your css to style it appropriately.
After adding the active class to the element, to apply your orange style use this:
div#top_nav.active a{
color: orange;
}
Note the addition of .active which selects only active items, and the omission of :hover on the link, since you no longer care about hover.
You may find the , css operator useful here, for applying the same styles to different selectors (active, and hover) as shown in my example below:
var items = document.getElementsByClassName('item');
var activeClassName = 'active';
function unselectItems() {
for (var i = 0; i < items.length; i++) {
items[i].classList.remove(activeClassName);
}
}
function selectItem(item) {
unselectItems();
item.classList.add(activeClassName);
}
function onItemClick(event) {
selectItem(event.target);
}
for (var i = 0; i < items.length; i++) {
items[i].addEventListener('click', onItemClick);
}
span {
border: 1px solid black;
padding: 5px;
}
span:hover, span.active {
color: white;
background-color: black;
}
<nav>
<span class="item">Home</span>
<span class="item">About</span>
<span class="item">Contact</span>
<nav>