【发布时间】:2012-09-24 06:15:41
【问题描述】:
伪元素未显示在 div 上。我正在使用精灵图像,但我也尝试了非精灵图像,但没有。我尝试了多种策略,例如定位绝对、使用 z-index、边距等。如果我没有弄错或者显然我做错了什么,我似乎做对了。我是社区的新手,在这里和谷歌都搜索过,但没有结果显示它为什么没有显示。代码如下,这是最基本的尝试。感谢所有花时间提供帮助的人。
.test {
display:inline-block;
background:#fff;
width:60%;
}
.test:before,
.test:after {
background:url("/imgs/Sprite2.png") repeat-y;
}
.test:before,
.test:after {
position:relative;
display:inline-block;
vertical-align:top;
width:27px;
height:100%;
}
.test:before {
content:"";
background-position:0 0;
}
.test:after {
content:"";
background-position:-55px 0;
}
我现在可以正常工作了。代码如下。我可以发誓我已经试过了,但我第一次做的时候肯定做错了什么。
.test {
background:#fff;
width:60%;margin:0 0 60px 5%;
}
.test:before,
.test:after {
content:"";
background:url("/imgs/Sprite2.png") repeat-y;
position:absolute;
top:0;
width:27px;
height:100%;
}
.test:before {
right:100%;
background-position:0 0;
}
.test:after {
left:100%;
background-position:-55px 0;
}
【问题讨论】:
-
您能否在JS Fiddle 或类似地址发布一个重现您的问题(并显示您的html)的演示?
-
您在哪个浏览器中测试?
-
我正在本地设置上的所有当前浏览器上进行测试。我所有的CSS总是有效的。我刚开始尝试使用伪元素。删除图像边框的空白跨度和更清晰的标记/语义。@David Thomas 我现在要在 JS Fiddle 上试一试。
-
快速注释以避免其他人犯同样的愚蠢错误:如果外部元素将
overflow设置为hidden,则:after伪元素将不会显示...跨度> -
非常感谢。我错过了向这些元素添加内容
标签: css pseudo-element