2016-12-27 60 views
0

我的HTML代码 -为什么这段代码没有显示背景图片?

<html> 
<head> 
<style type="text/css"> 
{ 
/*for setting background image*/ 
background-image: url('C:/Users/SONY/Desktop/CSS/images.jpg'); 
background-attachment: fixed 
} 
</style> 
</head> 
</html> 

此代码是用于设置背景图片。但它没有显示 的结果。我没有创建两个文件。就像一个用于CSS和另一个HTML文件一样。 我已经将这两个文件编译成一个。

为什么我无法看到背景图片?尽管为URL提供了正确的路径 。

+0

页面托管在服务器上,还是托管在您的计算机上?还请注意缺少选择器。 – Teemu

+0

用'html {'替换'{'' – Banzay

+0

没有class,id或元素分配给CSS。 – Goombah

回答

0

您正在将背景图像应用于任何class/id或任何html标签(如html,body等),无论您需要添加类还是标识,还是需要将内嵌样式应用于内置标签。你可以试试这个

<html> 
    <head> 
    <style type="text/css"> 
    html{ 
    /*for setting background image*/ 
    background-image: url('C:/Users/SONY/Desktop/CSS/images.jpg'); 
    background-attachment: fixed; 
    } 
    </style> 
    </head> 
    </html> 

希望这会有所帮助。

+0

感谢您的帮助..它的工作.. –

1

您需要指定要为其提供特定CSS属性的元素,如html,body,.class,#id

body { 
    /*for setting background image*/ 
    background-image: url('http://lorempixel.com/400/200/sports/1/'); 
    background-attachment: fixed 
} 

这里是链接工作fiddle

+0

没有链接,也没有代码,请检查您的答案。 –

+0

它的工作..感谢您的帮助.. –

0

我想你的代码在css文件夹下,所以行应该是: background-image:url('images.jpg');

+0

我试过这个,但它仍然不显示背景图像。 –

+0

body { /*用于设置背景图片*/ background-image:url('C:/Users/SONY/Desktop/CSS/images.jpg'); background-attachment:fixed;高度:800像素;宽度:1200像素; }可以尝试使用这些代码。 – shakil

+0

This works ..谢谢.. –

0
Use this style in your code. If want to pick image from local computer. 

<!DOCTYPE html> 
<html> 

<head> 
    <style type="text/css"> 
     html { 
      background-image: url(./images.jpg); 
      background-attachment: fixed; 
     } 
    </style> 

</head> 

<body> 

</body> 

</html>