【问题标题】:How to use css sprite correctly?如何正确使用 css sprite?
【发布时间】:2016-11-16 06:00:43
【问题描述】:

我正在尝试使用 css sprite 在图像的特定部分上移动一段文本。但我应用的背景位置似乎不起作用。我尝试更改背景位置,但文本部分(即 twitter、facebook)没有移动到正确的位置。

#fixedsocial {
	background:url("../img/socials/icon.png") no-repeat;          
    top:40%;    
    width:50px;
    height: 100px;
    position:fixed;
    left: 0; 
    display: block;
    z-index: 1000;   
    background-color: #eee;
    text-indent:-9999px;
}

.facebookflat {
    background-position: -200px 0;
    height:50px;
}

.facebookflat:hover {        
	cursor: pointer;
}

.twitterflat {    
	height:50px;
	background-position: -400px 0; 
}

.twitterflat:hover {    
	cursor: pointer;
}
<div id="fixedsocial">
  <div class="facebookflat" id="shareBtn"></div>
  <div class="twitterflat"> <a href="https://twitter.com/share" data-show-count="false"></a> </div>
</div>

【问题讨论】:

  • 其实我没有理解你的要求。
  • 我想显示第二排圆圈上的 Facebook 和 Twitter 图标
  • "我想显示 Facebook 和 Twitter 图标,它出现在第二排圆圈一" 这是什么意思?
  • 我上传的那张图片
  • 从那张图片中我想显示 facebook 和 twitter 图片 - 第二个 raw 上的圆圈图标

标签: html css


【解决方案1】:

它不起作用的原因是您将背景图像分配给容器 div (#fixedsocial),并尝试调整内部 div 上的定位,它没有背景定位。您需要重新考虑一下您的 CSS,因为您必须将背景分配到您真正想要使用它的位置。

这是一个小提琴:https://jsfiddle.net/j7kyhgmc/

#fixedsocial {
top:40%;    
width:50px;
height: 100px;
position:fixed;
left: 0; 
display: block;
z-index: 1000;   
background-color: #eee;
}

.facebookflat {
background: url("https://i.stack.imgur.com/LR6bK.png") no-repeat;
background-position: -168px -161px;
background-size: 1430% 1430%;
height:50px;
width: 50px;
}

 .facebookflat:hover {        
     cursor: pointer;
  }

  .twitterflat {    
background: url("https://i.stack.imgur.com/LR6bK.png") no-repeat;
background-position: -430px -161px;
background-size: 1420% 1420%;
height:50px;
width: 50px;
   }

   .twitterflat:hover {    
       cursor: pointer;
   }
<div id="fixedsocial">
     <div class="facebookflat" id="shareBtn"></div>
     <div class="twitterflat">
          <a href="https://twitter.com/share" data-show-count="false"></a>
     </div> 
</div>  

【讨论】:

  • 添加了一个例子。我不得不干预background-size,因为您50px*50px 框对于原始图像大小来说太小了。如果您希望图标看起来不错,您可以调整原始图像的大小,或者使用稍大的框(然后失去background-size 属性,并且可能不得不将图标与background-position-属性重新对齐。跨度>
  • 如果是这样,我想要一个标记的答案,如果你能这么好:)
猜你喜欢
  • 2012-08-02
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多