<p>
是块级元素。您不能将其放在内联元素 ( <a>
) 中。您可以执行以下操作:只需删除<p>
, 然后使用 CSS 将<a>
标记显示为块。
HTML:
<li id="selected"><a href="http://www.">FAQ'S</a></li>
CSS:
#menu li#selected {
padding: 0;
margin:0;
background:url(nav-tab-left.gif) bottom left no-repeat #90288d;
height: 35px;
}
#menu #selected a {
display: block;
background:url(nav-tab-right.gif) bottom right no-repeat;
height: 35px;
line-height: 35px; /* Centers the text vertically */
padding: 0 6px; /* Gives 6px of horizontal padding to the text */
margin: 0;
}
这将适用于所有浏览器,并且不会用无用的元素混淆您的标记。
悬停变化
使用此方法的另一个优点是您可以稍微修改它以允许与 IE6 兼容的 CSS 翻转图像,如下所示:
HTML:
<li id="selected"><a href="http://www."><span>FAQ'S<span></a></li>
CSS:
#menu li#selected {
padding: 0;
margin:0;
height: 35px;
}
#menu #selected a {
display: block;
background:url(nav-tab-left.gif) bottom left no-repeat #90288d;
height: 35px;
padding: 0;
margin: 0;
}
#menu #selected a span {
display: block;
background:url(nav-tab-right.gif) bottom right no-repeat;
height: 35px;
line-height: 35px; /* Centers the text vertically */
padding: 0 6px; /* Gives 6px of horizontal padding to the text */
margin: 0;
}
/* Hovered, so let's change the colors and the images */
#menu #selected a:hover {
background:url(nav-tab-left-hover.gif) bottom left no-repeat #902B27;
}
#menu #selected a:hover span {
background:url(nav-tab-right-hover.gif) bottom right no-repeat;
}
是的,它确实适用于 IE6 及更低版本(及更高版本),因为a
它是 IE6 支持:hover
伪类的唯一元素。这也是此方法需要添加额外<span>
标签的原因,因为我们无法以 IE6 理解的方式定位<li>
with :hover
。
我确实建议使用CSS 精灵而不是单独的图像来实现悬停效果,但为了使这个示例尽可能简单,我将保持原样。
有关 CSS 选择器支持的更多信息,请参阅CSS - 内容和兼容性。