1

I have a CSS3 transition that resizes/positions an image inside a div on hover.

FIDDLE

This works as desired but my concern is about browsers that don't support CSS3 transitions like IE9-. Would it be possible to write this CSS code so that these browsers have a fallback?

Idealy, the fallback should display the image so it fits the div and isn't zommed (fiddle example) and with no animation on hover.

I would prefer a CSS only solution and to not to alter the markup.

Full code example :

HTML :

<div><img src="http://lorempixel.com/output/people-q-c-1000-500-7.jpg" />

CSS :

div{
    overflow:hidden; 
    width:500px; 
    height:250px;
    position:relative;
}
img{
    display:block;
    position:absolute;
    height:auto;
    width:200%;
    left:-30%;
    top:-60%;

    -webkit-transition-property: width, left, top;
    -webkit-transition-duration: .8s;
    -webkit-transition-timing-function: ease-out;
    transition-property: width, left, top;
    transition-duration: .8s;
    transition-timing-function: ease-out;
}
div:hover img{
    width:100%;
    top:0;
    left:0;
}
web-tiki
  • 99,765
  • 32
  • 217
  • 249
  • @Jason: Except support for transitions is much more widespread than `@supports`. – BoltClock Jun 19 '14 at 17:12
  • it's hard to believe that IE9 does not support transition, I've never tried my demos on IE9 but I remember that I checked about transition feature on [caniuse](http://caniuse.com), it reported that IE9 did support transition. Maybe my memory is wrong. About your question, it's unclear on the fallback result you want. Do you mean that result should be ***initially*** applied to the image or on hovering? – King King Jun 19 '14 at 17:13
  • Oh right .. I haven't actually looked at support. Nevermind then. – Jason Jun 19 '14 at 17:13
  • @Jason if some brower supports `@supports`, it would surely support transition :) – King King Jun 19 '14 at 17:14
  • @KingKing I had the same feeling when I checked canIuse but wasn't shure either. For the fallback it should not have any hover animation and display the image like in this fiddle example http://jsfiddle.net/webtiki/qXPFA/2/ – web-tiki Jun 19 '14 at 17:15
  • @web-tiki I mean that result shoud be shown as the normal state or the hover state of the image? if it's the hover state, I think your CSS would work on IE9, when it does not support transition, the hover state is applied immediately. – King King Jun 19 '14 at 17:19
  • 1
    @KingKing that is what I would like to avoid, I need the image to have no hover effect and display the image with for example `width:100%;height:auto` for browsers that don't support transitions. – web-tiki Jun 19 '14 at 17:21
  • 1
    @KingKing I just realised I had that feeling about css transitions supported by IE9 because I was comfiusing them with `transform` property witch is supported by IE9 – web-tiki Jun 20 '14 at 13:53
  • @web-tiki I've just searched an answer of mine about transition/animation. The solution is using animation, but the OP asked for a working solution in ***IE9***. I remember while finding a workaround for that requirement, I tried finding out if `transition` is supported in IE9 (via caniuse.com), looks like at that time I saw it's supported, so I went ahead with the multi-transitions solution (replacing animation). The OP seemed to check that solution and said ***yup working fine in IE9*** (the last comment). I had no doubt about that... – King King Jun 20 '14 at 14:36
  • here is the link to my answer http://stackoverflow.com/questions/24016088/css-transition-to-bottom-right-from-center/24016746#24016746 – King King Jun 20 '14 at 14:37
  • @KingKing well it seems he was happy with the no transition effect as IE9 doesn't support transitions I tested the above code in IE tester an it doesn't work IE9- – web-tiki Jun 20 '14 at 14:43
  • @web-tiki yes, I've just realized that via the *chat link* in the comment he left, I did check if transition is supported before making the workaround demo, however maybe my eyes had some problem at that time. – King King Jun 20 '14 at 14:46

3 Answers3

4

You could use Modernizr or go through the javascript feature detection route.

Both ways are detailed here: Detect support for transition with JavaScript

Community
  • 1
  • 1
aa333
  • 2,556
  • 16
  • 23
  • I just realized our answers differ by an entire second. Yes, I agree that the most reliable solution would be to use JavaScript feature detection. CSS itself just isn't well-equipped for CSS feature detection yet. – BoltClock Jun 19 '14 at 17:33
  • @BoltClock Quite cool, isn't it? I should probably get even faster internet. :) – aa333 Jun 19 '14 at 17:37
  • Thank you for answering but I would prefer not to use aditional JS on this project. – web-tiki Jun 19 '14 at 17:42
3

Generally speaking, CSS transitions (and most of CSS, really) were designed with progressive enhancement in mind. The intended fallback in browsers that don't understand transitions is quite simply to ignore the transition properties themselves. This allows the style changes to still take place, only immediately and not in a smooth transition (as implied by the name), and obviates the need for complex workarounds.

What you're looking to do however is to not have any change in state occur at all; you want your image to be fixed in the unzoomed state. That will take a bit more work.

If @supports had been implemented in the beginning, you could easily get away with

img{
    display:block;
    position:absolute;
    height:auto;
    width:100%;
    top:0;
    left:0;

    -webkit-transition-property: width, left, top;
    -webkit-transition-duration: .8s;
    -webkit-transition-timing-function: ease-out;
    transition-property: width, left, top;
    transition-duration: .8s;
    transition-timing-function: ease-out;
}

@supports (-webkit-transition-property: all) or (transition-property: all) {
    div:not(:hover) img{
        width:200%;
        left:-30%;
        top:-60%;
    }
}

But of course, not everything works that way. It's a real shame that @supports was proposed so late and implementations still haven't caught on. But I digress.

Looking at the support tables at caniuse.com, it looks like Gecko- and WebKit/Blink-based browsers are extremely well covered (except maybe Firefox 3.6 and older), which is a relief because I can't think of any pure CSS-based solution to cater to those engines (other than ugly hacks).

For other browsers, I can see some other workarounds:

  • It may be worth including the -o- prefix if you care about Presto Opera.

  • Likewise with -moz- if you care about Firefox < 16.

  • For IE, simply hiding the div:not(:hover) img rules in conditional comments is enough, since the first version of IE to support transitions and ignore conditional statements happens to be the same — version 10:

    <!--[if !IE]><!-->
    <style>
    div:not(:hover) img{
        width:200%;
        left:-30%;
        top:-60%;
    }
    </style>
    <!--<![endif]-->
    

    Note the use of div:not(:hover) here, analogous to the hypothetical @supports example above. You will need to swap the declarations with your img rule accordingly.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • I'll go for the conditional statements and add a class to the `` tag as usual. CSS can't do everything... – web-tiki Jun 19 '14 at 17:41
2

Lets not lie ourselves, the only browser we are talking about is IE9, so just go add:

width: 200%\9;
left: -30%\9;
top: -60%\9;

and IE9 will use it. We can just hope in 2017 there will be no more need for CSS hacks.

skobaljic
  • 9,379
  • 1
  • 25
  • 51