Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a whole div clickable with working :active css rule in IE10

I'm designing a clickable panel for an html app which contains multiple text elements and images.

From what I understand this is generally done with a div. Something like this:

<div class="myButton">
    <h2>Text</h2>
    <h3>Some more text</h3>
    <img ...>
</div>

With a bit of styling and hooking up the click event this works fine but I am having problem with styling the active state:

.myButton {
    cursor:pointer;
}
    .myButton:active{
        -ms-transition-duration: 0.2s;
        -ms-transform: scale(0.95);
    }

In this example I'm trying to do a css animation (IE only) but this could really be anything. The problem is that the active state only works when I click on the div but doesn't work when I click on any of the children of the div.

Here is a JS Fiddle to show the scenario:

http://jsfiddle.net/S9JrH/13/

UPDATE: Thanks to David Thomas for pointing out a typo in the code and confirming that this works in Chrome.

Unfortunately, in IE10 this only works when you click on the lower part of the div, away from the text.

Does anyone know how to get this working properly in IE10?

like image 771
Patrick Klug Avatar asked May 31 '12 04:05

Patrick Klug


People also ask

How do I make a whole div a clickable link?

Create CSSSet the position to "absolute" for the inner <a> tag. Use the z-index property to place the link above all the other elements in the div.

How do you keep a link active in CSS?

A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.

How do I make a div a link?

You can't make the div a link itself, but you can make an <a> tag act as a block , the same behaviour a <div> has. You can then set the width and height on it. However, this doesn't make a 'div' into a link. It makes a link into a block element.

How do I force a div to stay in one line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.


1 Answers

Currently not possible (I think)

From what I can gather, this is currently not possible as the :active state of a child is not propagated up to the parent div. Both Internet Explorer 10 and Opera 11.64 failed to propagate the :active state up to the parent when testing with div elements.

Fiddle: http://jsfiddle.net/jonathansampson/UrN39/

Workaround

The only other solution that comes to mind would be to use event propagation in JavaScript. Fortunately the events of a mousedown will propagate up on the DOM, and onto the parent div. The following example utilizes jQuery:

$(".myButton").on("mousedown mouseup mouseleave", function(e){
    $(this).toggleClass( "active", e.type === "mousedown" );
});

Note here that I have modified the :active pseudo-class to be an actual class .active. This has been tested in IE10 and works. Given the approach, it should work without any problem in just about every major browser.

Fiddle: http://jsfiddle.net/jonathansampson/S9JrH/8/

like image 131
Sampson Avatar answered Sep 20 '22 18:09

Sampson