css - 使用 CSS 控制 DOM 中的 Sprite 大小。

标签 css css-sprites

Sprite :

我执行了以下操作,它看起来很棒但是...我不想显示 400 像素的宽度,我希望它包含在另一个 250 像素的 img 中。有没有办法(我尝试了很多方法)用 CSS 控制 Sprite 大小,或者我是否需要 js 解决方法?

.portfolio a .icon1  {
    width: 400px;
    height: 250px;
    background: url('../images/gate-sprite.png') 0 0;
    background-repeat: no-repeat;
}

.portfolio a:hover .icon1 {
    width: 400px;
    height: 250px;
    background: url('../images/gate-sprite.png') 0 -250px;
    background-repeat: no-repeat;
}

最佳答案

使用::before 或::after 伪元素来包含您的图标。这样您就可以设置图标大小,并为::before 或::after 元素指定背景图像。

例子:

.portfolio a .icon1::before  {
    content: "";
    display: inline-block;
    width: 400px;
    height: 250px;
    background: url('../images/gate-sprite.png') 0 0;
    background-repeat: no-repeat;
} 

更多关于 :before 和 :after 的信息: http://www.w3schools.com/css/css_pseudo_elements.asp

关于css - 使用 CSS 控制 DOM 中的 Sprite 大小。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899674/

相关文章:

javascript - anchor 和按钮内的垂直定位

html - SVG 的 CSS 不起作用

jquery - CSS Sprites 不会悬停在执行 Action 上

ruby-on-rails - compass Sprite 生成器有什么好的替代品吗? (滚动 Sprite 生成器)

html - 在什么情况下使用 HTTP/2 单独加载图像会比使用 la HTTP/1.1 一次加载所有图像慢?

html - 为什么我的 Webfonts 在 Windows 机器和 iPhone 上不显示?

jquery - JQuery Lightbox 代码有问题

javascript - 点击展开div不起作用

jQuery 优化对悬停事件属性的访问

jquery - 在悬停时显示 div 并保持到鼠标离开?