[英]how to set background-image to pseudo element?
this is the html 这是HTML
<div id="tab-1">
</div>
her is the css 她是css
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
How to display background-image? 如何显示背景图像? If I use background-color then it works but why not background-image?
如果我使用背景颜色,那么它可以工作,但是为什么不使用背景图像呢? And even if sometimes works in jsfiddle but not in my localhost.
即使有时在jsfiddle中工作,但在我的本地主机中却行不通。
You must take out the no-repeat
from the background-image
as you are using a short hand syntax inside background-image
property which is not valid, inorder to use short hand syntax, you need to use background
property instead 您必须从
background-image
删除no-repeat
,因为您正在使用background-image
属性中的简写语法,该语法无效。为了使用简写语法,您需要改用background
属性
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: " ";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo (Separating background-repeat
if you want to keep background-image
) 演示 (如果要保留
background-image
,请分离background-repeat
)
Demo 2 (CSS Short hand syntax using background
property) 演示2 (使用
background
属性的CSS简写语法)
Check out : http://jsfiddle.net/6nDKP/4/ 检出: http : //jsfiddle.net/6nDKP/4/
Instead of : 代替 :
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Use : 采用 :
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Separate the no-repeat
分开
no-repeat
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Fiddle: http://jsfiddle.net/6nDKP/1/ 小提琴: http : //jsfiddle.net/6nDKP/1/
The reason its not working is that your are including no-repeat in background-image style. 它不起作用的原因是您在背景图像样式中包含了不可重复的内容。 Exclude it to
background-repeat: no-repeat;
将其排除在
background-repeat: no-repeat;
. 。 Then it will work fine.
然后它将正常工作。 Here is the code:-
这是代码:-
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Change background-image with background. 用背景更改背景图像。
#tab-1:before{
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo: http://jsfiddle.net/zyuWW/ 演示: http : //jsfiddle.net/zyuWW/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.