无法为HTML文档的正文应用CSS

时间:2022-11-12 10:19:13

I am unable to apply a background image in my HTML document using the following code in CSS:

我无法使用CSS中的以下代码在HTML文档中应用背景图像:

body
    {
        text-align:center;
        background-image:url('C:\wamp\www\marks display\WI71.jpg');
    }

I also searched for it, but I found, the above declaration is true but unable to execute it. Why is this happening?

我也搜索了它,但我发现,上面的声明是正确的,但无法执行它。为什么会这样?

4 个解决方案

#1


7  

That's not a URL, that's a file path.

这不是一个URL,这是一个文件路径。

If the root of your site is marks display, probably you want this:

如果您的网站的根显示标记,可能您需要这样:

background-image:url('/WI71.jpg');

#2


1  

Path should not be map to a drive(file path) when publishing on web, it should be a URL.

在Web上发布时,路径不应映射到驱动器(文件路径),它应该是URL。

It should be like background-image:url('http://domainname/71.jpg'); -- Complete Url of Image

它应该像background-image:url('http://domainname/71.jpg'); - 完整的图像网址

or background-image:url('WI71.jpg'); -- Relative url

或background-image:url('WI71.jpg'); - 相对网址

#3


0  

buddy html css in reality is actually a on server thing so below is the right code:

伙计html css在现实中实际上是服务器上的东西所以下面是正确的代码:

background-image: url('c:/xyz/xyz/sample.jpg');

however if you are uploading your site on a real web server do not gives paths like that, just make it like below

但是,如果您要在真实的Web服务器上上传您的网站,请不要提供类似的路径,只需在下面进行操作即可

background-image: url('foldername_if required/imagename');

#4


0  

The string C:\wamp\www\marks display\WI71.jpg does not comply with URL syntax. To begin with, the character \ as such is not allowed in URLs; it should be replaced by the slash /. The space character should be %-encoded as %20. Finally, to refer to a file in the local system with a pathname, use a file: URL:

字符串C:\ wamp \ www \ marks display \ WI71.jpg不符合URL语法。首先,URL中不允许使用字符\;它应该用斜杠/替换。空格字符应为% - 编码为%20。最后,要使用路径名引用本地系统中的文件,请使用以下文件:URL:

background-image:url('file:///C:/wamp/www/marks%20display/WI71.jpg');

However, IE has very permissive error recovery here, so your malformed code actually works on IE, if the file exists in the place indicated with the name given. Other browsers require correct code (mostly).

但是,IE在此处具有非常宽松的错误恢复,因此如果文件存在于指定名称的位置,则您的格式错误的代码实际上可以在IE上运行。其他浏览器需要正确的代码(大多数)。

Such URLs are of very limited usefulness. They mostly work in local testing only, and even in local testing, it is better to use URLs that are relative to the location of the HTML document. This way, you can use the same code in local testing and on a web server, provided that you replicate the relevant parts of the folder structure.

这些URL的用处非常有限。它们主要用于本地测试,甚至在本地测试中,最好使用与HTML文档位置相关的URL。这样,只要复制文件夹结构的相关部分,就可以在本地测试和Web服务器上使用相同的代码。

#1


7  

That's not a URL, that's a file path.

这不是一个URL,这是一个文件路径。

If the root of your site is marks display, probably you want this:

如果您的网站的根显示标记,可能您需要这样:

background-image:url('/WI71.jpg');

#2


1  

Path should not be map to a drive(file path) when publishing on web, it should be a URL.

在Web上发布时,路径不应映射到驱动器(文件路径),它应该是URL。

It should be like background-image:url('http://domainname/71.jpg'); -- Complete Url of Image

它应该像background-image:url('http://domainname/71.jpg'); - 完整的图像网址

or background-image:url('WI71.jpg'); -- Relative url

或background-image:url('WI71.jpg'); - 相对网址

#3


0  

buddy html css in reality is actually a on server thing so below is the right code:

伙计html css在现实中实际上是服务器上的东西所以下面是正确的代码:

background-image: url('c:/xyz/xyz/sample.jpg');

however if you are uploading your site on a real web server do not gives paths like that, just make it like below

但是,如果您要在真实的Web服务器上上传您的网站,请不要提供类似的路径,只需在下面进行操作即可

background-image: url('foldername_if required/imagename');

#4


0  

The string C:\wamp\www\marks display\WI71.jpg does not comply with URL syntax. To begin with, the character \ as such is not allowed in URLs; it should be replaced by the slash /. The space character should be %-encoded as %20. Finally, to refer to a file in the local system with a pathname, use a file: URL:

字符串C:\ wamp \ www \ marks display \ WI71.jpg不符合URL语法。首先,URL中不允许使用字符\;它应该用斜杠/替换。空格字符应为% - 编码为%20。最后,要使用路径名引用本地系统中的文件,请使用以下文件:URL:

background-image:url('file:///C:/wamp/www/marks%20display/WI71.jpg');

However, IE has very permissive error recovery here, so your malformed code actually works on IE, if the file exists in the place indicated with the name given. Other browsers require correct code (mostly).

但是,IE在此处具有非常宽松的错误恢复,因此如果文件存在于指定名称的位置,则您的格式错误的代码实际上可以在IE上运行。其他浏览器需要正确的代码(大多数)。

Such URLs are of very limited usefulness. They mostly work in local testing only, and even in local testing, it is better to use URLs that are relative to the location of the HTML document. This way, you can use the same code in local testing and on a web server, provided that you replicate the relevant parts of the folder structure.

这些URL的用处非常有限。它们主要用于本地测试,甚至在本地测试中,最好使用与HTML文档位置相关的URL。这样,只要复制文件夹结构的相关部分,就可以在本地测试和Web服务器上使用相同的代码。