简体   繁体   English

CSS背景图片在文本链接翻转上的定位

[英]CSS Background image positioning on text link rollover

I have text links that I am trying to use a background image with on rollover ( link.gif is transparent, linkhover.gif is the rollover image). 我有文本链接,我想在翻转时使用背景图像( link.gif是透明的, linkhover.gif是翻转图像)。

The code I have below is working except the positioning is not. 我下面的代码工作正常,但定位不正常。

.navlink {
background:transparent url('graphics/link.gif') center top no-repeat;
height:70px;}

.navlink:hover {
background-image: url('graphics/linkhover.gif');}

You should make a sprite, put the images next to each other in one file and adjust the background-position on :hover. 您应该制作一个精灵,将图像彼此相邻放置在一个文件中,并调整:hover的背景位置。 The CSS should be like this: CSS应该是这样的:

.navlink {
    background-image: url('image');
    background-position: top left;
}

.navlink:hover {
   background-position: top right;
}

You can achieve a cool effect when adding an CSS3 transition. 添加CSS3过渡时,可以达到很酷的效果。 The image will then slide to the rollover state! 然后图像将滑动到翻转状态!

Try making the background take up the full size, like this 尝试像这样使背景占据整个尺寸

.navlink {
    background: url('graphics/link.gif');
    height:70px;
}

.navlink:hover {
    background: url('graphics/linkhover.gif');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;
}

Demo 演示版

If you have a parent element around .navlink , then you can just put height:100% and remove height:70px; 如果您在.navlink周围有一个父元素,则只需将height:100%并删除height:70px; and it will stay proportional. 它将保持比例。 If you want to disregard proportion and just have it fill the parent you can put both height:100% and width:100% 如果您想忽略比例而只填满父母,则可以同时设置height:100%width:100%

EDIT 编辑

Since I found out the navlink s are all <a> : you can't have background-attachment: fixed because it makes the parent's background change instead of the navlink 's (for what reason I don't know) 因为我发现navlink都是<a> :您无法进行background-attachment: fixed因为它使父级的背景发生变化,而不是navlink的变化(出于什么原因,我不知道)

Updated code 更新的代码

.navlink {
    text-align:center;
    background: url('graphics/link.gif');
    background-repeat: no-repeat; /* This applies to :hover as well */
    background-position: center; /* This applies to :hover as well */
    text-decoration: none; /* To remove the underline */
}
.navlink:hover {
    background: url('graphics/linkhover.gif');
}

Updated demo based on the structure of your site which you provided in the comments 根据您在评论中提供的网站结构更新了演示

Next time when writing your question you should include the relevant HTML, that would have made it much easier to help you with the problem 下次在编写问题时,您应该包括相关的HTML,这样可以更轻松地帮助您解决问题

EDIT 2 编辑2

After some playing I believe I got your site the way you want it using this: 玩了一段时间后,我相信我可以按照以下方式获得您想要的网站:

.navlink {
    padding-top:30px;
    text-align:center;
    background: url('graphics/link.gif');
    text-decoration: none;
}
.navlink:hover {
    background: url('graphics/linkhover.gif');
    background-position: center -55px;
    background-repeat:repeat-y;/*This is optional, taking it out makes it repeat*/
    text-decoration: none; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM