将元素中的背景图像位置与图像精灵中的位置组合

时间:2015-05-26 21:49:46

标签: css css-position

我有一个元素(h3),我想将背景位置设置在右边,这很容易:

background: url('/images/icon-popular.png') no-repeat right center;

但是我想使用.png文件,其中有很多其他图标(Image Sprites),通常我会这样做:

background-image: url('/images/icons-info.png');
background-position: 0 -110px;

如何将文件中的背景位置与"右中心"合并?在元素中的位置?

1 个答案:

答案 0 :(得分:1)

这是类似于你的线程:Using Css Sprites and background position

但我个人要做的就是使用像h3:before之类的伪选择器来实现这种效果,将其设置为与精灵中图标的大小相匹配,并将该元素定位在{{1}的右侧}}

h3

https://jsfiddle.net/3sdj4tga/

您的方法可能出现的问题是,如果h3 { background-color: #faa; padding:5px 10px; } h3:before { float: right; display: block; content: ""; width: 35px; height: 23px; background: transparent url('https://jsfiddle.net/img/logo.png') no-repeat left top; } 占用的空间比您在精灵中分配的空间多,则图像的其他部分会显示出不良影响。

相关问题