Forum Moderators: not2easy
<head>
<style>
a.testpic {display:block;width:250px;height:312px;}
a.testpic:hover {background-position:bottom;}';
</style>
</head>
<body>
<a href="" style="background:url(images/MyImage.jpg)top no-repeat;" class="testpic" ></a>
</body>
however, if I change it to:
<head>
<style>
a.testpic {display:block;width:250px;height:312px;background:url(images/MyImage.jpg)top no-repeat;}
a.testpic:hover {background-position:bottom;}';
</style>
</head>
<body>
<a href="" style="" class="testpic"></a>
</body>
...it works.
Can anyone tell me why the inline background image doesn't work and the embedded style does?
I need to have the reference to the image inline as it's part of some PHP code, otherwise I would write it all in the stylesheet.
Any help/thought/advice greatly appreciated!
EcksTen
[w3.org...]
The inline style has a higher specificity (1.0.0.0) compared to the specificity of your CSS file (0.0.2.1) and (0.0.1.1), preventing the :hover from winning.
Given the specificity information, shouldn't this then work? Or again, am I missing something with regards to how classes & pseduo classes work?
<head>
<style>
a.testpic {display:block;width:250px;height:312px;top no-repeat;}
a.testpic:hover {background-position:bottom;}';
</style>
</head>
<body>
<a href="" style="background:url(images/MyImage.jpg);" class="testpic" ></a>
</body>
the inline style
style="background:url(images/MyImage.jpg);" is set using the background shorthand property [w3.org]. When you use a shorthand property it will implicitly set the other parts of that property back to the default if they're not redefined:. So by using the background inline you are resetting the following without knowing it. background-color: transparent
background-repeat: repeat
background-attachment: scroll
background-position: 0% 0%
the :hover pseudo class selector cannot overrule the inline rule. Pseudo classes like hover can't be targeted inline, they can only be called on via a stylesheet so your hover rule can never exceed the 1,0,0,0 specificity set by the inline style. You need for it not to be overruling an inline style but adding to it ;)
so you need to get a bit more specific with your rules..
something like this should work:
CSS:
a.testpic {
display: block;
width: 250px;
height: 312px;
background-repeat: no-repeat;
/* the position 0% 0% is the default so need not be set */
}a.testpic:hover {
background-position: 0% 100%;
}
HTML:
<a href="" style="background-image: url(images/MyImage.jpg);" class="testpic" ></a>
in the testpic rules you only need to define the bits that are not the defaults, then your inline rule should only target the background-image itself, none of the other properties involved in the shorthand, so the cascading CSS is still picked up
HTH