简体   繁体   English

我的CSS样式表中只有一个起作用,而DIV背景图像不起作用?

[英]Only one of my CSS style sheets work and DIV background-image doesn't work?

First time coding my own website and first time uploading it. 第一次编写我自己的网站,然后第一次上传。 First time for everything basically. 基本上是第一次。 Yes, I have multiple CSS style sheets, some of my pages have different layouts. 是的,我有多个CSS样式表,我的某些页面具有不同的布局。

My website works perfectly on my local drive, but of course falls apart when I upload it to Host Gator via FileZilla. 我的网站可以在本地驱动器上正常运行,但是当我通过FileZilla将其上传到Host Gator时,它当然会崩溃。 I ensured that I followed case-sensitivity for my links/directories/images etc. 我确保我对链接/目录/图像等保持区分大小写。

I believe it's just my DIV background images on the index page that aren't loading properly and then the CSS style sheets aren't being applied to the rest of my website- yet it works for the homepage. 我相信只是索引页面上的DIV背景图像无法正确加载,然后CSS样式表未应用于我的网站的其余部分,但仍可用于主页。 I have confirmed the images are accessible when you type their url in directly. 我确认您直接输入网址时可以访问这些图片。

I have no idea why only my one css style sheet works and not the rest, and why my DIV background images aren't loading properly. 我不知道为什么只有一个css样式表可以工作,而其余的都不可以,为什么我的DIV背景图像无法正确加载。 Any advice is greatly appreciated, I just want my website to go live finally! 任何建议都将不胜感激,我只希望我的网站最终上线!

Feel free to have a look: www.alanphunt.com and let me know what you think. 随时查看:www.alanphunt.com,让我知道您的想法。 I've only taken a semester of an introductory web development course so go easy on me! 我只上了一学期的Web开发入门课程,所以轻松一点吧!

Most likely the problem is with relative and absolute path usage. 最可能的问题是相对和绝对路径使用情况。 Check if you can open the given CSS from the source code of the page. 检查是否可以从页面的源代码中打开给定的CSS。 If you cannot it means that your browser can't do it either. 如果不能,则表示您的浏览器也无法执行。 If you can, the CSS should be loaded. 如果可以,则应加载CSS。

This is a problem with connecting your CSS to HTML or paths, for sure. 毫无疑问,这是将CSS连接到HTML或路径的问题。 You should double check every path on your code, sometimes I paste things and forget to change the path, look out for that. 您应该仔细检查代码中的每个路径,有时我会粘贴一些东西而忘记更改路径,请加以注意。

As other answers have stated, the problem is the relative paths. 正如其他答案所述,问题是相对路径。

The general rules for paths 路径的一般规则

  • images/image.jpg (no leading slash) - start from the location of the current file images / image.jpg (无斜杠)-从当前文件的位置开始
  • /images/image.jpg - start from the site root regardless of where the current file is located. /images/image.jpg-从站点根目录开始,无论当前文件位于何处。 The root is the directory the web server looks in when you type in just your domain name. 根目录是您仅输入域名时Web服务器查找的目录。 On Hostgator this is called public_html (for your primary domain; it can be a little more complicated if you have multiple domains or subdomains.) Other common names are www or htdocs. 在Hostgator上,这称为public_html(对于您的主域;如果您有多个域或子域,它可能会稍微复杂一些。)其他常用名称是www或htdocs。
  • ../images/image.jpg - start one level up from the current file. ../images/image.jpg-从当前文件开始上一层。 You can also do ../../images/image.jpg to start two levels up, and so on. 您也可以执行../../images/image.jpg来启动两个级别,依此类推。 (But if you find yourself going up five or six levels, it might a sign you should start from the root.) (但是,如果您发现自己升了五个或六个级别,则可能是一个应该从根开始的迹象。)
  • http://example.com/images/image.jpg - Look for the image at this exact url. http://example.com/images/image.jpg-在此确切网址下查找图像。 It's also possible to use a protocol-less url eg //example.com/images/image.jpg 也可以使用无协议的网址,例如//example.com/images/image.jpg

As for your specific question: on the home page, the background images images/alan.jpg and images/seemennt.jpg are set in an external stylesheet. 关于您的特定问题:在主页上,背景图像images/alan.jpgimages/seemennt.jpgimages/seemennt.jpg中设置。 Because the path doesn't begin with a forward slash, they'll try to load relative to the location of the stylesheet, in this case the "css" folder. 因为路径不是以正斜杠开头,所以它们将尝试相对于样式表的位置(在本例中为“ css”文件夹)进行加载。 (You can hit F12 in your browser and look at the console to confirm this. It'll also help to diagnose exactly what isn't loading.) (您可以在浏览器中按F12键,然后在控制台上进行确认。这也将有助于准确诊断未加载的内容。)

Try changing the paths to something like ../images/alan.jpg and it should work. 尝试将路径更改为../images/alan.jpg ,它应该可以工作。

Sources 资料来源

Are you using Google Chrome? 您在使用Google Chrome吗? If you are, head to: 如果是,请前往:

  1. Homepage 主页
  2. Right click 右键点击
  3. View Page Source 查看页面源代码

Here, you will be able to view the outputted HTML that your browser is using to display your website. 在这里,您将能够查看浏览器用来显示网站的输出HTML。

After looking through the Source code of your Homepage, I can see the following URL, for your Stylesheet: 浏览完首页的源代码后,我可以看到以下URL,用于您的样式表:

css/indexcss.css?v=version2

I selected this URL and it brings up your Stylesheet. 我选择了该URL,它调出了样式表。 I then checked out the source code of your other pages ('Photography', 'Music', 'DJ Life' and 'Reach Out'), and can see that your stylesheets have been incorrectly inputted. 然后,我签出了其他页面的源代码(“摄影”,“音乐”,“ DJ Life”和“ Reach Out”),然后可以看到您的样式表输入错误。 You need to perform the following changes: 您需要执行以下更改:

  • photography.css to css/photography.css photography.csscss/photography.css
  • musiccss.css to css/musiccss.css musiccss.csscss/musiccss.css
  • djcss.css to css/djcss.css djcss.csscss/djcss.css
  • reachoutcss.css to css/reachoutcss.css reachoutcss.csscss/reachoutcss.css

This should resolve your issue. 这样可以解决您的问题。

Best of luck with your website! 祝您网站运气佳!

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

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