简体   繁体   English

CSS 不在本地主机上工作

[英]CSS not working on Localhost

I am building a site and had all of my CSS, HTML working wonderfully.我正在建立一个站点,并且我的所有 CSS、HTML 都运行良好。 I wanted to add in some PHP functionality so I have created a local development environment on my mac with Apache. I then moved all of my site folders to my new Sites folder that can be accessed by localhost.我想添加一些 PHP 功能,所以我在我的 Mac 上使用 Apache 创建了一个本地开发环境。然后我将我所有的站点文件夹移动到我的新站点文件夹,可以通过本地主机访问。 When loading my website and now none of my CSS styles are loading.加载我的网站时,现在我的 CSS styles 都没有加载。 I am using Twitter Bootstrap locally.我在本地使用 Twitter Bootstrap。 This is what my css links look like in my html:这是我的 css 链接在我的 html 中的样子:

link rel="stylesheet" type="text/css" href="http://localhost/~tromph/YoointooSite/bootstrap.min.css">
link rel="stylesheet" type="text/css" href="http://localhost/~tromph/YoointooSite/main.css">

I've tried just about every other path that I can think of and nothing else seems to work.我已经尝试了几乎所有我能想到的其他方法,但似乎没有其他方法有效。

I changed my folder structure and the file path to:我将文件夹结构和文件路径更改为:

<link rel="stylesheet" type="text/css" href="/bootstrap-3.3.2-dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/bootstrap-3.3.2-dist/css/main.css" />

This fixed my problem of not having the CSS render.这解决了我没有 CSS 渲染的问题。 Now the images linked in the CSS are not showing.现在 CSS 中链接的图像没有显示。 Dev tools is telling me that I am getting an error 403 (forbidden) on each of my images.开发工具告诉我,我的每张图片都收到错误 403(禁止)。 What should I do to change this permission?我应该怎么做才能更改此权限?

I spent some time updating permissions.我花了一些时间更新权限。 Everything is working now.现在一切正常。 Thanks for your help!谢谢你的帮助!

I would recommend referencing the CSS files relatively. 我建议相对引用CSS文件。 So if you have the following folder structure. 因此,如果您具有以下文件夹结构。

myProj
  - css
    - bootstrap
      - bootstrap.min.css
  - index.html (or php)

Then you would reference the bootstrap file like so 然后您将像这样引用引导文件

<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.min.css" />

Guessing at your folder structure from your example I would then assume that all you need is 从示例中猜测您的文件夹结构,然后我假设您需要做的就是

<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="main.css" />

If however your css files are not in your project, but instead are served externally, then you would need to reference them from wherever you are serving them. 但是,如果您的CSS文件不在您的项目中,而是在外部提供,则无论您在哪里提供服务,都需要引用它们。 For example if serving them to port 8080, then you would need to reference them using that address instead. 例如,如果将它们提供给端口8080,则需要使用该地址来引用它们。

<link rel="stylesheet" type="text/css" href="http://localhost:8080/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="http://localhost:8080/main.css" />

Again, the above is only if you are serving them externally from the site that you are attempting to use them from. 同样,以上内容在您从尝试使用它们的站点外部提供它们的情况下进行。 If they are inside of the project you are working on, then reference them relatively from the file attempting to use them. 如果它们在您正在处理的项目中,请从尝试使用它们的文件中相对引用它们。

The first thing I would look at is whether or not it's a path issue with your styleheets. 我要看的第一件事是您的样式表是否存在路径问题。 Use web inspector or dev tools in your browser and look for a 404 error in your CSS files. 在浏览器中使用Web检查器或开发工具,然后在CSS文件中查找404错误。 If there are any, dev tools will show the path it's looking for and you can adjust the path to your style sheets accordingly. 如果有的话,开发人员工具将显示其正在寻找的路径,您可以相应地将路径调整为样式表。

Good luck! 祝好运!

Use Developer tools in google chrome or other browser and in the sources tab look for errors. 在Google Chrome或其他浏览器中使用开发人员工具,然后在“来源”标签中查找错误。 Two common errors are your css files not being found (see relative path answer above) or maybe your file permissions don't allow the Apache server to access the CSS files. 常见的两个错误是找不到您的css文件(请参见上面的相对路径答案),或者您的文件许可权不允许Apache服务器访问CSS文件。

Thought I'd throw my input into the works. 以为我会投入我的投入到作品中。

I named one of my CSS files style.css than later went and deleted it and created a new style.css and my wamp server kept referencing the first style.css file I made. 我后来将其中一个CSS文件命名为style.css ,然后将其删除并创建了一个新的 style.css ,我的wamp服务器一直引用我制作的第一个style.css文件。

So I renamed the second CSS file to main_style.css and everything worked perfectly. 因此,我将第二个 CSS文件重命名为main_style.css ,一切正常。

I once faced the issue of CSS not being applied when I was working with PHP.我曾经在使用 PHP 时遇到过 CSS 没有被应用的问题。

I fixed this issue by doing Empty Cache and Hard Reload (chrome).我通过执行Empty Cache and Hard Reload (chrome) 解决了这个问题。

To do that:要做到这一点:

  • Open DevTools打开开发者工具
  • Right-click on reload button(top-left)右键单击重新加载按钮(左上角)
  • Click on Empty Cache and Hard Reload单击Empty Cache and Hard Reload

After doing this, all my CSS styles were applied.这样做之后,我所有的 CSS 样式都被应用了。

I also faced same problem but later found a solution to it.我也遇到了同样的问题,但后来找到了解决办法。 Just Press 'Ctrl+F5' when the css file doesn't open on the browser window.当 css 文件无法在浏览器 window 上打开时,只需按“Ctrl+F5”即可。

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

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