我可以使用本地文件系统中的图像作为HTML中的背景吗? [英] Can I use an image from my local file system as background in HTML?

查看:132
本文介绍了我可以使用本地文件系统中的图像作为HTML中的背景吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管在远程Web服务器上的HTML文档。我试图让网页上的其中一个元素使用本地文件系统中的图像文件作为其背景图像。 Chrome,Safari或Firefox没有运气(还没有试过IE)。

I've got an HTML document hosted on a remote web server. I'm trying to have one of the elements on the web page use an image file from my local file system as its background image. No luck with Chrome, Safari or Firefox (haven't tried IE).

以下是我迄今尝试过的一个例子。

Here's an example of what I've tried so far.

<!DOCTYPE html>
<html>
    <head>
        <title>Experiment</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
            html,body { width: 100%; height: 100%; }
        </style>
    </head>
    <body style="background: url('file:///Users/username/Desktop/background.png')">
    </body>
</html>

如果我使用浏览器的Web检查工具检查body元素,并选择在新标签页中打开图片形象在那里。因此,浏览器完全有能力使用给定的URL获取图像文件。

If I inspect the body element using my browser's web inspection tool, and select "Open image in new tab" the image is there. So the browser is fully capable of getting at the image file using the given URL.

是我想要尽可能拉开的,还是这是一个安全浏览器的功能试图阻止外部域访问用户的本地资源?

Is what I'm trying to pull off at all possible, or is this a security feature of the browser trying to block external domains from accessing the user's local resources?

推荐答案

似乎你可以提供本地图像名称,假设它在同一个文件夹中...

It seems you can provide just the local image name, assuming it is in the same folder...

它足够像:

It suffices like:

background-image: url("img1.png")

这篇关于我可以使用本地文件系统中的图像作为HTML中的背景吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆