我打算在我的网站上应用链接样式,使外部链接旁边有一个图标,但是当应用时,页脚图片链接也发生了变化,有没有办法避免这种情况的发生?
CSS
a[href^="http://"] {
background: url( https://www.clearinghouseforsport.gov.au/__data/assets/image/0009/643176/icon_external.gif ) center right no-repeat;
padding-right: 16px;
}
<a>link</a>
但排除<a><img src='' /></a>
这是不可能的。a[href^="http://"] {
/*do your stuff*/
}
a[href^="http://"] img {
background: none; /*remove background*/
}
那应该可以工作。顺便说一下,您的选择器中缺少https://
在找到并评估了该网站之后,我得出了一个可靠的解决方案:
<head></head>
的末尾...#navigation...
开头的规则集之后这些标签仅适用于HTML页面:
<style>
#navigation a::after {
content: url(https://www.clearinghouseforsport.gov.au/__data/assets/image/0009/643176/icon_external.gif);
position: relative;
top: 5px;
left: 3px;
}
</style>
注意:替代选择器为:a.navmenuitem::after
请查看PLUNKER
显而易见的解决方案是使用类选择器。如果无法编辑HTML,请尝试使用类型选择器。在您的情况下,如果要排除位于页脚中的锚点,请尝试以下方法:
footer a { background:none !important; }
OR
footer a[href^="http://"] { background: url(https://cdn1.iconfinder.com/data/icons/web-page-and-iternet/90/Web_page_and_internet_icons-14-128.png) no-repeat 20px 20px;
a[href^="http://"] {
background: url(https://www.clearinghouseforsport.gov.au/__data/assets/image/0009/643176/icon_external.gif ) center right no-repeat;
padding-right: 16px;
margin: 10px;
}
footer a[href^="http://"] {
background: url(https://yourescape.ldara.com/_resources/images/content/icon_link.png) center right no-repeat;
padding-right: 20px;
}
<header>
<nav>
<a href="http://example.com/">NAV</a>
<a href="http://example.com/">NAV</a>
<a href="http://example.com/">NAV</a>
<a href="http://example.com/">NAV</a>
</nav>
</header>
<section>
<aside>
<a href="http://example.com/">ASIDE</a>
<a href="http://example.com/">ASIDE</a>
</aside>
</section>
<footer>
<a href="http://example.com/">FOOTER</a>
<a href="http://example.com/">FOOTER</a>
</footer>
a
标签中使用一些类或ID,这样它就只会附加到该特定元素上。 - aavrug