0

I have a box and I want to change the style, like change the border when a user click on that box.

I know this can be done through JS or jQuery, but is this possible using only CSS?

I tried using :focus and :active, it does changes the style when you click, but on release of the click it reverts the CSS

Here is my code:

CSS:

<div class="box"></div>
.box{
    width:100px;
    height:100px;
    border:2px solid #ff0000;

}

.box:active, .box:focus {
    border: 2px solid #00afec;
}

I created the Fiddle also to try: http://jsfiddle.net/cj3LV/

Please suggest!

4
  • What is the problem with pure javascript? Commented Mar 7, 2014 at 20:31
  • 1
    No, it isnt possible ;) This is just possible with JS or jQuery Commented Mar 7, 2014 at 20:32
  • 2
    This is only possible if you add to the HTML so you can do the checkbox hack Commented Mar 7, 2014 at 20:32
  • Works fine with a text input like in this fiddle, otherwise you probably want to use jQuery selectable for any divs. Commented Mar 7, 2014 at 20:34

3 Answers 3

5

There is a Trick with a Checkbox in CSS: Its possible because of following things:

  • The Checkbox is hidden.
  • If you clicked on the Label you are checked the Checkbox. The Reason is the same value in for="toggle-1" and id="toggle-1"
  • This declartion input[type=checkbox]:checked ~ .box { } matches elements that are siblings of the given element. This will match a .box element if it's a sibling of input[type=checkbox]:checked

http://jsfiddle.net/cj3LV/3/

CSS:

.box, .label{
    width:100px;
    height:100px;
    border:2px solid #ff0000;
    text-align:center;
    line-height: 100px;
}
.label {
    position: absolute;
    display:block; 
    border:0;
}
#toggle-1 { display:none; }


/* Toggled State */
input[type=checkbox]:checked ~ .box {
   border: 2px solid #00afec;
}

HTML:

<label for="toggle-1" class="label"></label>
<input type="checkbox" id="toggle-1">
<div class="box">Click ME!!</div>

For more Information you can read here: http://css-tricks.com/the-checkbox-hack/

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

3 Comments

Fair enough, although the semantics of this is pretty unpalatable.
Could you kindly explain the principle behind this?
Interesting. I didn't know that for="something" and id="something" binds together the elements in terms of the events. Thanks for your explanation.
2

Here you go: FIDDLE

HTML

<div id='clickme'><a href="#clickme">Click Me</a></div>

CSS

#clickme{
    width:100px;
    height:100px;
    border:2px solid #ff0000;
    text-align:center;
    line-height: 100px;
}

#clickme:target
{
    background-color: red;
}

Edit: I initially deleted it because there was a better answer. But I was wrong, it can serve.

Comments

1

You might use target but you need to create a link

http://css-tricks.com/on-target/

https://developer.mozilla.org/en-US/docs/Web/CSS/:target

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.