css - 如何在CSS背景中获取中间图像?

标签 css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 9 年前

enter image description here

我只想在 div 中插入红色图像。

我想做 css sprite。问题是我有 3 张这样的图片。但 div 将是 900px。如何把中间的图片放到div中?

#a {
    background: url(../img/a.png) 0 0 no-repeat;
    background-size: 100% 100%;
    width: 100px;
    height: 100px;
}

最佳答案

要使用 sprite 技术,您需要一个单独的元素,其尺寸等于 spritesheet 上的图像。可以为此目的使用伪元素:

.container {
    position: relative;
}

.container::before {
    content: '';

    /* Vertical centering */
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;

    /* Sprite */        
    display: block;
    width: 31px;
    height: 69px;
    background: url(http://i.stack.imgur.com/BpoSb.png) -30px 0;
}

/* Put all the children in front of ::before */
.container > * {
    position: relative;
}

演示:http://jsfiddle.net/tPLXF/

关于css - 如何在CSS背景中获取中间图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125638/

上一篇:javascript - 连接第一个和最后一个列表元素以创建轮播的幻灯片中的 Jquery 列表?

下一篇:html - CSS 仅模拟一个选择框

相关文章:

javascript - 无法更改 SVG 元素的类名

Jquery ui 选项卡添加 css 类

css - 忽略垂直 flex 元素内的文本对齐

html - 为什么 &lt;input/> 占用如此大的初始空间,如何控制?

css - 绝对定位的按钮在 IE11 中不居中

html - CSS格式化表头

javascript - 简单 javascript 属性更改的一些错误

CSS ul li 如果删除 1 会使其他变大

css - 覆盖 iframe 中内容的正文样式

html - 试图放大 240 * 240 div 内的图像