I have a menu in a tag called sf-menu
.
I need the visibility to change to none
when the link is clicked and toggled back when clicked again.
Can I achieve this with CSS only or do I need JavaScript?
Hoping someone can help me with an example.
.sf-menu {visibility: visible}
<a class="closed" href="#sidewidgetarea"> Switch</a>
You can do it using only CSS. The trick is to leave a trail that could be clicked and work as a switch/button for turning display on or off. You can accomplish that by wrapping your content needed to be toggled into some div or any other element suitable to you.
Using this as a example. Yes, putting an anchor around a block is valid in HTML5 specs. Try cutting and pasting the script into the free online validator or pasting the link to your web page.
By setting the text-decoration to none to remove the underline from anchor tag. Syntax: text-decoration: none; Example 1: This example sets the text-decoration property to none.
The <link> tag defines the relationship between the current document and an external resource. The <link> tag is most often used to link to external style sheets or to add a favicon to your website.
http://jsfiddle.net/gSPqX/1/
<input type="checkbox" style="display: none" id="cb">
<label for="cb">Click Here</label>
<div>
Hello. This is some stuff.
</div>
:target
#menu .menu{
display:none;
}
#menu:target .menu{
display: block;
}
#menu:target .menu__open{
display: none;
}
#menu .menu__close{
display: none;
}
#menu:target .menu__close{
display: block;
}
<div id="menu">
<a href="#menu" class="menu__open">Open menu</a>
<a href="#" class="menu__close">Close menu</a>
<div class="menu">
Hello. This is some stuff.
</div>
</div>
If you are using jquery, you can use jquery.toggle()
Example:
$(".closed").click(function(event) {
event.stopPropagation()
$(".sf-menu").toggle();
});
Source: https://api.jquery.com/toggle/
You can do it using only CSS. The trick is to leave a trail that could be clicked and work as a switch/button for turning display on or off. You can accomplish that by wrapping your content needed to be toggled into some div or any other element suitable to you.
Here is a little example for that.
label {
cursor: pointer;
}
#menu-toggle {
display: none; /* hide the checkbox */
}
#menu {
display: none;
}
#menu-toggle:checked + #menu {
display: block;
}
<label for="menu-toggle">Click me to toggle menu</label>
<input type="checkbox" id="menu-toggle"/>
<ul id="menu">
<li><a href="#">First link</a></li>
<li><a href="#">Second link</a></li>
<li><a href="#">Third link</a></li>
</ul>
Here is a super simple solution in jQuery, using the tags you have mentioned. But first remove your CSS-row!
Please not that this example doesnät seem to execute on stackoverflow.com but works fine on jsfiddle.net.
$('.closed').click(function(){
$('.sf-menu').toggle();
});
<a class="closed" href="#sidewidgetarea">Switch</a>
<div class="sf-menu">The menu</div>
In HTML :
<h1 class="sf-menu"> TestText </h1>
JavaScript :
var sfmenu = document.getElementsByClassName("sf-menu");
sfmenu[0].onclick = function () {
if (this.style.display == 'none' ) {
this.style.display = '';
} else {
this.style.display = 'none';
}
};
If you're able to edit the DOM, there is a PureCSS solution (see below), but I think that the best solution is using Javascript, jQuery offers you a crossbrowser solution!
Hope Helps!!
var HIDDEN_CLASS = 'sf-menu-hidden';
/** jQuery **/
jQuery(document).ready(function($) {
$('#jQueryTest .closed').click(function closeWithJQuery(event) {
event.preventDefault();
$('#jQueryTest .sf-menu').toggleClass(HIDDEN_CLASS);
});
});
/** VanillaJS **/
document.addEventListener("DOMContentLoaded", function(event) {
document
.querySelector('#VanillaJSTest .closed')
.addEventListener('click', function() {
var el = document.querySelector('#VanillaJSTest .sf-menu');
var task = el.classList.contains(HIDDEN_CLASS) ? 'remove' : 'add';
el.classList[task](HIDDEN_CLASS);
});
});
* {
-webkit-user-select: none;
user-select: none;
}
.sf-menu-hidden {
visibility: hidden;
}
a, label {
cursor: pointer;
}
article + article {
margin-top: 10px;
border-top: 1px solid green;
}
#PureCSSTest [type="checkbox"] {
display: none;
}
#PureCSSTest [type="checkbox"]:checked + .sf-menu {
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article id="jQueryTest">
<h1>Test jQuery</h1>
<a class="closed">TOGGLE MENù</a>
<div class="sf-menu">
<ul>
<li> Hey I Am On THE SCREEN</li>
</ul>
</div>
</article>
<article id="VanillaJSTest">
<h1>VanillaJS</h1>
<a class="closed">TOGGLE MENù</a>
<div class="sf-menu">
<ul>
<li> Hey I Am On THE SCREEN</li>
</ul>
</div>
</article>
<article id="PureCSSTest">
<h1>PURE CSS</h1>
<a class="closed"><label for="toggleClosed">TOGGLE MENù</label></a>
<input type="checkbox" id="toggleClosed">
<div class="sf-menu">
<ul>
<li> Hey I Am On THE SCREEN</li>
</ul>
</div>
</article>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With