Is there a way to set multiple background-position properties for a single element? Depending on the state of a link (pressed/hover/default), I need to both change the X and Y values of the image being displayed.
I'd like to have the Y values static in my CSS files, while the X position needs to change per each element in my nav bar (which the links go in).
Is there a way to do this? Looking online, I haven't found a way to set just the top or j开发者_如何学编程ust the left parameter for background-position.
Apparently,
[background-position] accepts one or two length values, percentages, or keywords.
If only one value is specified for background-position, the second value is assumed to be center. Where two values are used, and at least one is not a keyword, the first value represents the horizontal position, and the second represents the vertical position.
You will most likely have to rely on JavaScript to preserve those Y values.
you can use this CSS to change the background image position for a element depending on its state (regular, hover, or active)... assuming you are using a sprite for your background:
#NavElement {
background-image:url(../img/button.png);
background-repeat:no-repeat;
background-position:top center;
}
#NavElement:hover {
background-position: center; /* center is assumed as the second value */
}
#NavElement:active {
background-position:bottom center;
}
Also, you can specify the position in px rather than bottom, top, or center.
精彩评论