Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html CSS背景位置在.class中不起作用_Html_Css - Fatal编程技术网

Html CSS背景位置在.class中不起作用

Html CSS背景位置在.class中不起作用,html,css,Html,Css,最近,我尝试用css和一些图标的类制作一个精灵表。发现css类不能处理背景位置,但ID现在可以,有人能解释一下为什么它会这样做吗 我的实际代码。我把问题打得太快了,犯了一些错误,对此我深表歉意 这不起作用 .male { height: 286px; width: 110px; background-position: 0px 0px; } .defender { background: url('../images/classes/human_defender

最近,我尝试用css和一些图标的类制作一个精灵表。发现css类不能处理背景位置,但ID现在可以,有人能解释一下为什么它会这样做吗

我的实际代码。我把问题打得太快了,犯了一些错误,对此我深表歉意

这不起作用

.male {
    height: 286px;
    width: 110px;
    background-position: 0px 0px;
}

.defender {
    background: url('../images/classes/human_defender.png');
}
<div class="male defender"></div>
#male {
    height: 286px;
    width: 110px;
    background-position: 0px 0px;
}

.defender {
    background: url('../images/classes/human_defender.png');
}
<div class="defender" id="male"></div>
。男{
高度:286px;
宽度:110px;
背景位置:0px 0px;
}
.防守者{
背景:url('../images/classes/human_defender.png');
}
但是当我将icon1更改为这样的id时,它确实起作用了

.male {
    height: 286px;
    width: 110px;
    background-position: 0px 0px;
}

.defender {
    background: url('../images/classes/human_defender.png');
}
<div class="male defender"></div>
#male {
    height: 286px;
    width: 110px;
    background-position: 0px 0px;
}

.defender {
    background: url('../images/classes/human_defender.png');
}
<div class="defender" id="male"></div>
#男性{
高度:286px;
宽度:110px;
背景位置:0px 0px;
}
.防守者{
背景:url('../images/classes/human_defender.png');
}

尝试在url中放入图像的路径

.class {
  background-image:url('path-to-image/image.png');
}

正如你所说,第二种选择对你有效。您能提供它们的JSFIDLE示例吗?因为我的观点也不能起作用。

在你的css
中。class
类有错误的背景属性。 请尝试将背景图像:url替换为
url

.class{
背景图像:url('http://www.hdwallpapersos.com/wp-content/uploads/2015/02/nature-wallpapers-natural-green-wallpaper-wallpaper.jpg');
}
.icon1{
背景位置:32px 0px;
宽度:100px;
高度:100px;
}
应该是

.class {background-image:url(backgroundimageurl);}

url('backgroundimageurl')
无效,应该是:
background image:url('backgroundimageurl')
您可以使用background:和background image作为图像的url,但不能直接使用url,因为它无法确定使用了什么。有点脱离主题。。。但是,防止使用名称作为类的“类”-这没有意义:)更好的是“图标”或“图标”,每个图标的名称分类为“icon1,icon2”或更好的“ico箭头”,“ico推特”等。。。这将创建更清晰的代码-为了将来的维护,我刚刚将.class作为C的一个示例,我的实际代码看起来不同,并且是正确的。将把我的问题编辑为我的实际代码看起来像什么。量化背景图像:url(“MyowImage”);仍然没有定位我的图像要设置背景的位置,您必须在“背景位置”属性中设置图像的坐标;到背景图像:url('someimage');出于某种原因,我无法编辑我之前对这个答案的评论JS Fiddle示例的背景:url('someimage');收件人:背景图像:url('someimage');我在问题中犯了一个错误很抱歉:D我有背景:url('image');但将其更改为背景图像:url(“图像”);但类{background:url('backgroundimageurl');}应该可以工作