1

How do I make an image hidden after clicking anywhere inside a div and make it stay hidden until page refresh?

<style>
#containter:active img {display: none}
</style>

<div id="containter">
<img src="image.png">
</div>

This works but as soon as you move the mouse outside of the div, the image reappears. I know it supposed to do that, but how to make it remain hidden?

5
  • There are :active and :focus pseudo-classes available in css. If you want something else you can use an inline (or unobtrusive) function call such as <div onclick="func()"> and set it via javascript. Commented Jun 27, 2013 at 22:47
  • I'm lousy with javascript so please correct me. It should be something like this? onclick="img.style.visibility='hidden'" Commented Jun 27, 2013 at 23:22
  • Something like <div id="tempDiv" onclick="document.getElementById('tempDiv').style.visibility='hidden';">click me</div> would work, but this is just an example, and not the way it should be done. Commented Jun 27, 2013 at 23:30
  • If you want it to stay hidden, then you have to use javascript. Commented Jun 27, 2013 at 23:55
  • Thanks everyone, you helped me. mgamba I used your inline code but instead of 'onclick' I'm using 'onmouseover' Commented Jun 28, 2013 at 0:13

3 Answers 3

2

A simple way of doing this is to wrap the item you want to hide on click in <label> and use a rule like

:checked + img {
    display: none;
}

http://jsfiddle.net/kGDQq/1/

Sign up to request clarification or add additional context in comments.

1 Comment

It's not semantic and also hidden radio buttons/checkboxes have poor support in older version of IE.
1

Here's an unobtrusive example for JS:

html:

<div id="tempDiv">click me</div>

js:

document.getElementById("tempDiv").onclick = function(e) {
  e.target.style.visibility = 'hidden';
}

and a jsfiddle for it

Comments

1

In order to be semantically correct I suggest you use a JavaScript solution and don't try to do it with CSS/HTML hacks. The below method attaches a new click handler to all elements with the class .hide-on-click, simply add the class to any element you want to hide on click.

jsFiddle

HTML

<div class="hide-on-click">Test 1</div>
<div class="hide-on-click">Test 2</div>
<div class="hide-on-click">Test 3</div>
<div class="hide-on-click">Test 4</div>

JS

(function () {
    var elements = document.getElementsByClassName('hide-on-click');
    for (var i = 0; i < elements.length; i++) {
        elements[i].addEventListener('click', function () {
            this.style.display = 'none';
        });
    }
})();

If you want the the space that the image took up not to collapse then you should use the visibility property.

this.style.visibility = 'hidden';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.