CSS 继承问题 - 标记背景位置

标签 css inheritance

我有一个社交媒体按钮链接集,我想更改它在背景图片上的位置。 这是我的代码: HTML:

<div id="connections">
    <div id="googleplus"><a target="_blank" href="">&nbsp;</a></div>
    <div id="facebook"><a target="_blank" href="">&nbsp;</a></div>
    <div id="twitter"><a target="_blank" href="">&nbsp;</a></div>
    <div id="youtube"><a target="_blank" href="">&nbsp;</a></div>
    <div id="vimeo"><a target="_blank" href="">&nbsp;</a></div>
</div>

CSS:

#connections div { display: inline; }

#connections div a {
    display: inline-block;
    height: 40px;
    width: 40px;
    border: none;
    text-decoration: none;
    font-size: 0;
    margin: 5px;
}

#connections a { background-position: top center; }

#connections a:hover { background-position: 0 -40px; }

#connections #facebook a { background: url('../images/facebook.png') no-repeat; }
#connections #twitter a { background: url('../images/twitter.png') no-repeat; }
#connections #googleplus a { background: url('../images/googleplus.png') no-repeat; }
#connections #youtube a { background: url('../images/youtube.png') no-repeat; }
#connections #vimeo a { background: url('../images/vimeo.png') no-repeat; }

可以在本页底部看到预览 -> http://www.bokehcreative.co.uk/about.php

如果我将 !important 应用到 :hover 样式或将 hover 样式应用到每个单独的 id,就可以完成。

我可能在这里漏掉了一个技巧。感谢您的帮助。

最佳答案

已编辑

将您的父 div ID 更改为类,并为 :hover 提供第二个。

<div id="connections">
    <div class="googleplus connections"><a target="_blank" href="">&nbsp;</a></div>
    <div class="facebook connections"><a target="_blank" href="">&nbsp;</a></div>
    <div class="twitter connections"><a target="_blank" href="">&nbsp;</a></div>
    <div class="youtube connections"><a target="_blank" href="">&nbsp;</a></div>
    <div class="vimeo connections"><a target="_blank" href="">&nbsp;</a></div>
</div>

然后将 CSS 应用于该类并摆脱 CSS 的双 ID 特异性,仅使用单个父类声明:

.facebook a { background: url('../images/facebook.png') no-repeat; }
.twitter a { background: url('../images/twitter.png') no-repeat; }
.googleplus a { background: url('../images/googleplus.png') no-repeat; }
.youtube a { background: url('../images/youtube.png') no-repeat; }
.vimeo a { background: url('../images/vimeo.png') no-repeat; }

.connections a:hover { background-position: 0 -40px; }

这应该可以避免任何类型的特异性问题。 jsFiddle to show what I mean (感谢开胃菜,约瑟夫)

关于CSS 继承问题 - 标记背景位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573419/

相关文章:

html - CSS flexbox : How to vertically align "flex items" to middle without losing flex item height?

css - 相对对齐具有固定宽度和高度的div中的元素

c++ - 如何优先使用虚方法覆盖和继承

java - 什么时候必须在子类中定义构造函数?

html - 如何将 div 的动态列表排列成 2 列

javascript - 当鼠标悬停在链接上时如何使子链接出现

c# - protobuf-net 继承 : derived class hides base class property

c# - 调用继承方法和基方法

html - 如何对齐元素内的一半文本?

swift - 为什么 Swift 类和结构要这样设置?