Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change css of tag from an outside link

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>
like image 847
Erik Avatar asked Sep 21 '15 00:09

Erik


People also ask

How do I change a tag in CSS?

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.

Can you wrap a div in a link?

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.

How do you remove a tag in CSS?

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.

Which tag is used for external linking?

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.


6 Answers

  1. You can use input type='checkbox' for it :

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>
  1. One more better solution using :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>
like image 140
sglazkov Avatar answered Oct 04 '22 10:10

sglazkov


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/

like image 30
Hieu Le Avatar answered Oct 04 '22 12:10

Hieu Le


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>
like image 25
Haseeb Zulfiqar Avatar answered Oct 04 '22 10:10

Haseeb Zulfiqar


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>
like image 45
Fabian Mossberg Avatar answered Oct 04 '22 11:10

Fabian Mossberg


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';
    }
};
like image 39
Osama AbuSitta Avatar answered Oct 04 '22 10:10

Osama AbuSitta


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>
like image 43
Hitmands Avatar answered Oct 04 '22 11:10

Hitmands