简体   繁体   English

“..”在 html 映像目录路径中不起作用

[英]".." not working in html image directory path

File Structure:文件结构:

-css
   - home.css
-html
   - index.html
-images
   - banner.png

HTML: HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>myWebsite</title>
    <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>
<body>
    <header>
        <img id="banner" src="../images/banner.png" alt="Banner Image"/> <!-- my image won't load? -->
    </header>

    <main> 

    </main>
        
    <footer>

    </footer>
</body>
</html>

The image doesn't load.图片加载不出来。 And when I try to open the image in another tab from the browser, I get Cannot GET /images/banner.png .当我尝试从浏览器的另一个选项卡中打开图像时,我得到Cannot GET /images/banner.png I think it has to do with the .. not working, but I could be wrong.我认为这与..不工作有关,但我可能是错的。 It works perfectly fine if I put the image in the same directory as index.html .如果我将图像放在与index.html相同的目录中,它工作得非常好。 But I don't want to do that as I plan this project to be decently large.但我不想这样做,因为我计划这个项目相当大。 I also want to use local image loading.我也想使用本地图像加载。 Does anyone know why the image won't load?有谁知道为什么图片加载不出来?

If your diagram is correct.. Your images and css directories need to be directly inside the html directory.. IE如果您的图表是正确的.. 您的imagescss目录需要直接在html目录内.. IE

-html
   - index.html
   - images
       - banner.png
   - css
       - home.css

At that point.. There is no need for ..那时..不需要..

<link rel="stylesheet" type="text/css" href="/css/home.css">
<img id="banner" src="/images/banner.png" alt="Banner Image"/>

'../' go up one directory from where I am now 1.find the 'assets/' folder 2.find the 'images' folder 3.find the 'image.jpg' file. '../' go 从我现在所在的一个目录向上 1. 找到 'assets/' 文件夹 2. 找到 'images' 文件夹 3. 找到 'image.jpg' 文件。 That relative link will only work if your page is in a subfolder in该相对链接仅在您的页面位于

"http://localhost/abc/def/geh/" “http://localhost/abc/def/geh/”

If the location of your page really is如果您的页面位置确实是

"localhost/asdf/asdf/asdf/asdf/index.php" “本地主机/asdf/asdf/asdf/asdf/index.php”

(which seems ridiculous) to get to the assets folder relatively you would have to go all the way to the root. (这似乎很荒谬)要相对于资产文件夹,您必须 go 一直到根目录。

'../../../../abc/deh/geh/assets/images/image.jpg; '../../../../abc/deh/geh/assets/images/image.jpg;

Alternatively you could use a base tag in your head tag to make the URL in the actual src attribute more friendly.或者,您可以在 head 标记中使用基本标记,以使实际 src 属性中的 URL 更友好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM