英文:
Why isn't my background image displaying in my html file?
问题
我正在尝试制作一个基本的太空网站。我想在 body 元素中设置一个太空图片作为背景。然而,当我链接这个URL时,在我的网页上看不到它。我尝试过通过更改 h1 的颜色来确保我的 HTML 和 CSS 被正确链接,它运行得很正常。所以,我确定问题出在文件路径本身。我可能做错了什么?我已经附上了我的代码。
英文:
I am attempting to make a basic space website. I wanted to have a background image of a space picture in the body element. However, when I link the url, it doesn't show in my webpage. I tried testing to make sure my html and css are linked properly by changing the h1 color, and it works just fine. So, I'm positive it's something with the file path itself. What could I have done wrong?
I've attached my code.
答案1
得分: 2
我在你的文件结构的截图中没有看到 "images" 文件夹。如果 "space photo.jpg" 文件与 css 文件位于同一文件夹中,你的 css 行应该如下所示:
{background-image:url("./space photo.jpg")}
你可以在这里了解更多关于路径的信息:
https://www.w3schools.com/html/html_filepaths.asp
顺便说一下,在图像名称中使用连字符而不是空格被认为是良好的做法
英文:
I do not see "images" folder in your file structure in the screenshots. If the "space photo.jpg" file is in the same folder as the css file your css line should look like this:
{background-image:url("./space photo.jpg")}
You could learn a little more regarding paths here
https://www.w3schools.com/html/html_filepaths.asp
BTW it is considered good practice using hyphens instead of space in image names
(https://www.mediavine.com/image-filename-seo/#:~:text=The%20filename%20should%20be%20descriptive,you%20should%20address%20going%20forward.)
答案2
得分: 0
我看到与您的space.css在同一文件夹中的图像文件,同时我没有看到任何图像文件夹,
所以您可以尝试这样做:
/* space.css */
body {
background-image: url("space photo.jpg")
}
英文:
I see image file in the same folder as your space.css, also I don't see any images folder,
so you can just try this:
/* space.css */
body {
background-image: url("space photo.jpg")
}
答案3
得分: 0
你的错误在于图片文件夹不存在。有两种解决方案。创建一个名为“images”的文件夹并将图片移动到该文件夹中,或者在css文件中更改图片的路径为正确的路径。
如果你决定在文件中更改路径,那么请用以下内容替换该属性:
background-image: url("space photo.jpg");
我还建议你学习文件路径相关的知识,因为这非常重要。
英文:
Your error is that the images folder does not exist. There are two solutions to this problem. Create an images folder and move the image to that folder, or change the path to the image in the css file to the correct path.
If you decide to change the path in the file then replace the property with this:
background-image: url("space photo.jpg");
I also recommend that you study the file paths stuff as it's very important.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论