使用绝对定位创建 CSS 背景图像精灵时出现问题
我有 2 张图像,想用作具有悬停效果的链接。所有图像和悬停图像都存在于单个 png 文件中。
我遇到的问题是第一个图像工作得很好,但第二个图像出现时,悬停图像从 top:0 开始,并且我想在它的正下方看到图像。如果我将 #MCD a{
更改为 #MCD{
,则图像会正确显示,但图像的所有链接功能都会消失。
HTML
<ul id="sites">
<li id="LL"><a href="" /></li>
<li id="MCD"><a href="" /></li>
</ul>
CSS
#sites{
position:absolute;
margin:0;
padding:0;
top:250px;
left:700px;
width:500px;
background:transparent;
display:block;
}
#sites li{
position:absolute;
list-style:none;
right:0;
}
#LL a{
display:block;
width:442px;
height:43px;
background:url(sitesprites.png) 0 0;
}
#LL a:hover{
background:url(sitesprites.png) 0 -44px;
}
根据我收到的回复进行编辑
#MCD a{
position:absolute;
display:block;
right:0;
top:78px;
width:384px;
height:54px;
background:url(sitesprites.png) left -88px;
}
#MCD a:hover{
background-position:left -143px;
}
这是一个 jsFiddle:http://jsfiddle.net/lipestyle/77a2z/
I have 2 images that I would like to use as links with a hoverover effect. All over the images and hoverover images exist on a single png file.
The problem I am having is that the first image works just fine, but the second image appears with both the hoverover image beginning at top:0 and the image I want to be seeing directly below it. If I change #MCD a{
to #MCD{
then the image displays correctly but all linking functionality for the image disappears.
HTML
<ul id="sites">
<li id="LL"><a href="" /></li>
<li id="MCD"><a href="" /></li>
</ul>
CSS
#sites{
position:absolute;
margin:0;
padding:0;
top:250px;
left:700px;
width:500px;
background:transparent;
display:block;
}
#sites li{
position:absolute;
list-style:none;
right:0;
}
#LL a{
display:block;
width:442px;
height:43px;
background:url(sitesprites.png) 0 0;
}
#LL a:hover{
background:url(sitesprites.png) 0 -44px;
}
Edited per the responses I have received
#MCD a{
position:absolute;
display:block;
right:0;
top:78px;
width:384px;
height:54px;
background:url(sitesprites.png) left -88px;
}
#MCD a:hover{
background-position:left -143px;
}
Here is a jsFiddle: http://jsfiddle.net/lipestyle/77a2z/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有你的 HTML,我不确定你的确切问题是什么,除了悬停无法正常工作。
正如 Pointy 所说,为了使 top 和 right 正常工作,您必须将元素声明为绝对位置(这会将其从页面流中删除。)
另外,您的 png 是如何制作的?一个图像是在另一个图像之上还是并排?背景位置的语法是先水平定位,然后垂直定位。 (有点向后。)如果您使用顶部、底部、左侧、右侧或中心,大多数浏览器将解析向后语法(
background-position: top center
;)。但使用数字时,它们的顺序必须正确。你的数字看起来有点奇怪。通常CSS精灵是垂直完成的,并定位
background-position: top;
,然后移动的高度(这将是PNG总高度的一半)向下悬停。
假设您的
的高度是 PNG 图像高度的一半,并且您的 PNG 是垂直的,请尝试这样的操作:
只是侧面不是,您应该始终尝试在上面使用 css 精灵
元素。
Without your HTML I'm not sure what you exact problem is, except the the hover is not working correctly.
As Pointy said, in order for top and right to work you must declare the element to be position absolute (which will remove it from the flow of the page.)
Also how are your pngs made? Is on image on top of the other or is it side by side? The syntax for background position is horizontal first then vertical positioning. (kind of backwards.) Most browsers will parse backwards syntax (
background-position: top center
;) if you use top, bottom, left, right, or center. But when using numbers they have to be in the right order.Your numbers look a little weird. Usually css sprites are done vertically and positioned
background-position: top;
then moved the height of the<a>
(which would be half the total height of the PNG) down on the hover.Assuming the height of your
<a>
is half the height of the PNG image and your PNGs are vertical, try something like this:just a side not, you should always try to use your css sprites on the
<a>
element.