I had created these images for a rollover effect and the following code did work. However it has recently broken when I tried adding a modal popup for another button on the site. I've reverted the changes to the code (removed the modal pop-up) but the sprites are still broken. Instead of just showing the light banner, and then the dark on hover, it shows both of the sprite segments and scales them in. What is causing this and how can I stop it from happening?
a, img {
border: none;
outline: none;
}
a.rolla {
display: block;
width: 200 px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-size: 200px 258px;
background-repeat: no-repeat
}
a.rollb {
display: block;
width: 200 px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-size: 200px 258px;
background-repeat: no-repeat
}
a.rollc {
display: block;
width: 200 px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-size: 200px 258px;
background-repeat: no-repeat
}
a.rolld {
display: block;
width: 200 px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-size: 200px 258px;
background-repeat: no-repeat
}
a.rollover:hover, a.rollover2:hover, a.rollover3:hover {
background-position: -213px 0;
}
a.rolla:hover, a.rollb:hover, a.rollc:hover, a.rolld:hover {
background-position: -210px 0;
}
.displace {
position: absolute;
left: -5000px;
}
body {
background: url("rpt.png");
background-repeat: repeat;
width: 1024px;
margin: 0;
z-index: -1;
}
#banner {
z-index: 0;
}
#feTable {
z-index: 1;
position: absolute;
top: 455px;
left: 0;
}
<div width="1024px">
<img id="banner" src="banner.jpg" height="512 px" width="1024px">
<table id="feTable" style="width:1024px; left:22px">
<tr>
<td align="center" width="200px">
<a target="_parent" href="" class="rollb"><span class="displace"></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rolla"><span class="displace"></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rollc"><span class="displace"></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rolld"><span class="displace"></a>
</td>
</tr>
</table>
</div>
You have two main problems with your code.
The first is that you are using background-size: 200px 258px;
. Specifying length values for background-size
will force the background to those dimensions. This is squashing you 412 x 260 image down to 200 X 258, which is why the entire image is shown.
<length>
A
<length>
value that scales the background image to the specified length in the corresponding dimension. Negative lengths are not allowed.
background-size (https://developer.mozilla.org/en-US/docs/Web/CSS/background-size)
The second is that you have incorrectly specified the width
on a.rolla
, a.rollb
, a.rollc
and a.rolld
. The space between 200
and px
means it is being ignored and is growing to the width of the containing td
(which in turn shows more of the background than is required).
To fix, make the following changes:
width: 200 px;
to width: 200px;
on a.rolla
, a.rollb
, a.rollc
and a.rolld
background-size: 200px 258px;
from a.rolla
, a.rollb
, a.rollc
and a.rolld
a,
img {
border: none;
outline: none;
}
a.rolla {
display: block;
width: 200px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-repeat: no-repeat
}
a.rollb {
display: block;
width: 200px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-repeat: no-repeat
}
a.rollc {
display: block;
width: 200px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-repeat: no-repeat
}
a.rolld {
display: block;
width: 200px;
height: 258px;
text-decoration: none;
background: url("https://i.sstatic.net/RCPyD.png");
background-repeat: no-repeat
}
a.rollover:hover,
a.rollover2:hover,
a.rollover3:hover {
background-position: -213px 0;
}
a.rolla:hover,
a.rollb:hover,
a.rollc:hover,
a.rolld:hover {
background-position: -210px 0;
}
.displace {
position: absolute;
left: -5000px;
}
body {
background: url("rpt.png");
background-repeat: repeat;
width: 1024px;
margin: 0;
z-index: -1;
}
#banner {
z-index: 0;
}
#feTable {
z-index: 1;
position: absolute;
left: 0;
}
<div width="1024px">
<table id="feTable" style="width:1024px; left:22px">
<tr>
<td align="center" width="200px">
<a target="_parent" href="" class="rollb"><span class="displace"></span></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rolla"><span class="displace"></span></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rollc"><span class="displace"></span></a>
</td>
<td align="center" width="200px">
<a target="_parent" href="" class="rolld"><span class="displace"></span></a>
</td>
</tr>
</table>
</div>