Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS background-position change on Y-Axis only

Ok...so here is the problem.

I have a CSS sprite image made up of ten(10) 25px x 25px icons laid out horizontally - thus resulting in a sprite image of 250px width.

I am using these 25x25 images as thumbnails. I'm looking to have an opacity of 30% on these images in INITIAL view and when a user hovers over them the opacity needs to be 100% (1).

So what I did was create a SECOND row of images with their opacity at 30% - so now I have a sprite image of 250px x 50px. The top 25px at 100% and the bottom 25px at 30%.

I setup HTML as follows:

<a href="largeimage1.jpg" class="thumb1"></a>
<a href="largeimage2.jpg" class="thumb1"></a>
<a href="largeimage2.jpg" class="thumb1"></a>
etc...

and the CSS:

a { display: block; float: left; width: 25px; height: 25px; background: url("250_x_50_spriteimage.jpg") 0 -25px no-repeat; }
.thumb1 { background-position: 0 0; }
.thumb2 { background-position: -25px 0; }
.thumb3 { background-position: -50px 0; }
a:hover { **background-position-y**: -25px; }

However, this doesn't appear to work unfortunately, as background-position-y is NOT supported in Firefox (or is not a standard, but is IE-specific).

The idea is that we (only) want to SHIFT the sprite image UP (along y-axis) and leave the x-axis as is (or was set in the previous classes).

If there is no simple CSS solution to this - can this opacity effect be done with JQUERY? So the thumbs would load at 30% opacity and would transition to 100% opacity when user hovers?

Many thanks,

M.

like image 311
Mo Boho Avatar asked May 03 '09 20:05

Mo Boho


People also ask

What is background-position y in CSS?

The background-position-y CSS property sets the initial vertical position for each background image. The position is relative to the position layer set by background-origin .

How do I scale a background image in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.


1 Answers

You do not need a second icon set nor the use of JavaScript to achieve the desired effect.

As Lou pointed out, use opacity to make your icons 30% or fully visible. No need to mess with background-position anymore.

Just go ahead and define your styles accordingly to the following:

a {
    opacity:0.3;  /* for standard browsers */
    filter: alpha(opacity = 30);  /* for IE */

    display: block;
    float: left;
    width: 25px;
    height: 25px;
    background: url("250_x_50_spriteimage.jpg") 0 -25px no-repeat;
}

a:hover {
    opacity:1.0
    filter: alpha(opacity = 100);
}

.thumb1 { background-position: 0 0; }
.thumb2 { background-position: -25px 0; }
.thumb3 { background-position: -50px 0; }

If you are worried about validation of your CSS code, take the IE-specific parts (which won't validate) and put them in specifically targeted CSS files via conditional comments.

Hope that helps.

like image 155
WrongAboutMostThings Avatar answered Sep 21 '22 01:09

WrongAboutMostThings