0

I'm using Twitter Bootstrap to build a website, and I want to have an image appear as grayscale until I hover over it, at which point it should become full color.

Instead of editing the Bootstrap.css, I created my own custom css: 'starter-template.css'.
Here's the code in 'starter-template.css':

.thumbnail2 {
-webkit-filter: grayscale(100%);
z-index: -9999999999999999999999999px;
-webkit-transition: all 0.9s ease-in-out;
-moz-transition: all 0.9s ease-in-out;
-o-transition: all 0.9s ease-in-out;
-ms-transition: all 0.9s ease-in-out;
transition: all 0.9s ease-in-out;
}

.thumbnail2:hover {
-webkit-filter: grayscale(0%);
z-index: -9999999999999999999999999px;
-webkit-transition: all 0.9s ease-in-out;
-moz-transition: all 0.9s ease-in-out;
-o-transition: all 0.9s ease-in-out;
-ms-transition: all 0.9s ease-in-out;
transition: all 0.9s ease-in-out;
}

And here's the html:

<!-- Custom styles for this template -->
<link href="static/starter-template.css" type = "text/css" rel="stylesheet">

....

<a href="{{my_link}}"><img class = "thumbnail2" src="{{my_string}}"  align="right" height = "200" width = "200"></a>

However, there is no hover effect--the image appears as full color when the page loads and doesn't change when I hover over it. Any suggestions?

Thanks!

3
  • 4
    z-index doesn't have a unit, so remove the px from those rules. developer.mozilla.org/en-US/docs/Web/CSS/z-index Commented Aug 27, 2014 at 3:46
  • 1
    Which browser are you using for testing? -webkit-filter applies only for chrome and safari. Also I don't see how bootstrap is related to this question. Is it possible to create a fiddle just to check that the code you have posted here works or not? Commented Aug 27, 2014 at 3:50
  • I am using Chrome. I deleted the px, but that had no effect :( Commented Aug 27, 2014 at 3:52

2 Answers 2

2

Think fixing your z-index is all you need: http://jsfiddle.net/c8wtbjfw/

.thumbnail2 {
    -webkit-filter: grayscale(100%);
    z-index: -9999999999999999999999999;
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    -o-transition: all 0.9s ease-in-out;
    -ms-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
}
.thumbnail2:hover {
    -webkit-filter: grayscale(0%);
    z-index: -9999999999999999999999999;
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    -o-transition: all 0.9s ease-in-out;
    -ms-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
}

Seems to work when I test it in Chrome (36.0.1985.143). Since that's a Webkit filter, it won't work in IE or Gecko-based browsers.

An alternative might be to transition the opacity rule, since that has better support. Here's the same CSS, but with opacity instead: http://jsfiddle.net/c8wtbjfw/1/

.thumbnail2 {
    opacity: .5;
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    -o-transition: all 0.9s ease-in-out;
    -ms-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
}
.thumbnail2:hover {
    opacity:1;
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    -o-transition: all 0.9s ease-in-out;
    -ms-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
}

I did remove your z-index, since I'm not sure what you're trying to accomplish by pushing the image "under" the rest of the page.

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

2 Comments

Thanks. Weirdly enough, when I saved the custom css under another name, it worked.
Sounds like a cache issue. A hard refresh or clearing your history would probably have worked as well.
0

Try this:

.thumbnail2 {
    filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
    filter: gray; /* IE5+ */
    -webkit-filter: grayscale(100%); /* Webkit Nightlies & Chrome Canary */
    z-index: -9999999999999999999999999;
-webkit-transition: all 0.9s ease-in-out;
-moz-transition: all 0.9s ease-in-out;
-o-transition: all 0.9s ease-in-out;
-ms-transition: all 0.9s ease-in-out;
transition: all 0.9s ease-in-out;
}

.thumbnail2:hover {
    filter: none;
    -webkit-filter: grayscale(0);
    z-index: -9999999999999999999999999;
-webkit-transition: all 0.9s ease-in-out;
-moz-transition: all 0.9s ease-in-out;
-o-transition: all 0.9s ease-in-out;
-ms-transition: all 0.9s ease-in-out;
transition: all 0.9s ease-in-out;
}

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.