Html 另一个文件夹中的 CSS 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15785390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:09:47  来源:igfitidea点击:

CSS Images In another Folder

htmlcss

提问by Chathula

I added the images into the folder called "images" and CSS to folder called "css", now I want to use the images in "images" folder. How do I do this? When I used this, they didn't show up:

我将图像添加到名为“images”的文件夹中,并将 CSS 添加到名为“css”的文件夹中,现在我想使用“images”文件夹中的图像。我该怎么做呢?当我使用它时,它们没有出现:

background: url('/images/bg.jpg');

I also tried these:

我也试过这些:

background: url('../images/bg.jpg');
background: url('../../images/bg.jpg');

What is the path I should be using to access my images?

我应该使用什么路径来访问我的图像?

回答by mikkelz

If you have a folder structure like follows:

如果您的文件夹结构如下:

/public_html/
    /css/
    /images/
    /index.html

Then your CSS should work.

那么你的 CSS 应该可以工作。

  • Starting with "/" returns to the root directory and starts there
  • Starting with "../" moves one directory backwards and starts there
  • Starting with "../../" moves two directories backwards and starts there (and so on...) To move forward, just start with the first
    subdirectory and keep moving forward
  • 以“/”开头返回根目录并从那里开始
  • 以“../”开始向后移动一个目录并从那里开始
  • 以“../../”开始向后移动两个目录并从那里开始(等等......)要向前移动,只需从第一个
    子目录开始并继续向前移动

Read more here: http://css-tricks.com/quick-reminder-about-file-paths/

在此处阅读更多信息:http: //css-tricks.com/quick-reminder-about-file-paths/

回答by winner_joiner

Here is the same question answerd https://stackoverflow.com/questions/940451/using-relative-url-in-css-file-what-l??ocation-is-it-relative-to.

这是回答https://stackoverflow.com/questions/940451/using-relative-url-in-css-file-what-l??ocation-is-it-relative-to的相同问题。

here is the W3C spec from the answer http://www.w3.org/TR/REC-CSS1/#url

这是答案http://www.w3.org/TR/REC-CSS1/#url 中的 W3C 规范

With other words it depences on the Folder Structur, as commented under your Post, by Sport Billy.

换句话说,它取决于Sport Billy在您的帖子下评论的文件夹结构。

i hope it helps

我希望它有帮助

回答by defau1t

When you use background-images, make sure you give width and height too for the container containing the images.

当您使用背景图像时,请确保为包含图像的容器也提供宽度和高度。